2013-11-25 152 views
0

我有一個基本的servlet。Web.xml導致Tomcat7失敗

我使用tomcat 7來運行它。

服務器包含3個文件:

  1. file.jsp - 打印日期

  2. WebController - 該servlet。

  3. web.xml - 配置文件。

的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
    <servlet> 
      <servlet-name>file.jsp</servlet-name> 
      <jsp-file>file.jsp</jsp-file> 
    </servlet> 
    <servlet-mapping> 
      <servlet-name>file.jsp</servlet-name> 
      <url-pattern>/about</url-pattern> 
    </servlet-mapping> 
</web-app> 

當我從web.xml文件中刪除<servlet><servlet-mapping>,該servlet運行良好。

web.xml是如以上所述,我發現了以下錯誤消息:

'Staring Tomcat v7.0 Server at localhost' has encountered a problem. 

Server Tomcat v7.0 Server at localhost failed to start. 

我使用蝕。我的web.xml文件有什麼問題?提前致謝!

編輯:

這是我的項目:

enter image description here

更新:

當前版本的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
    <servlet> 
      <servlet-name>file.jsp</servlet-name> 
      <jsp-file>/file.jsp</jsp-file> 
    </servlet> 

    <servlet> 
     <servlet-name>WebController</servlet-name> 
     <servlet-class>WebController</servlet-class> 
    </servlet> 

    <servlet-mapping> 
      <servlet-name>file.jsp</servlet-name> 
      <url-pattern>/about</url-pattern> 
    </servlet-mapping> 
</web-app> 
+0

可能重複:http://stackoverflow.com/questions/6642745/web-xml-ignoring-main-jsp-file 你在哪裏放jsp文件 – phury

+0

@ph。在WEB-INF文件夾中,jsp文件與'web.xml'位於同一個文件夾中。 – Billie

回答

0

一個在servlet映射可能格式web.xml文件看起來像這樣:

<servlet> 
    <servlet-name>com.example.file_jsp</servlet-name> 
    <servlet-class>com.example.file_jsp</servlet-class> 
</servlet> 

<servlet-mapping> 
    <servlet-name>com.example.file_jsp</servlet-name> 
    <url-pattern>/file.jsp</url-pattern> 
</servlet-mapping> 

類的名稱來自於JSP文件的名字和你的build.xml的這一部分:

<jasper2 
    validateXml="false" 
    uriroot="web" 
    package="com.example" 
    webXmlFragment="build/web/WEB-INF/generated_web.xml" 
    outputDir="build/src" /> 
+0

OP沒有映射'Servlet',他正在映射'JSP'文件。 – Masudul

+0

我一直在預編譯我的JSP很長一段時間,我真的忘了還有另一種方式:P(我刪除了「正確的方式」,但留下了答案,以防萬一它有幫助) –

2

你的JSP文件不能在WEB-INF。 把它放在你的項目

更新web.xml根如下:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
    <servlet> 
     <servlet-name>file.jsp</servlet-name> 
     <jsp-file>/file.jsp</jsp-file> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>file.jsp</servlet-name> 
     <url-pattern>/about</url-pattern> 
    </servlet-mapping> 
</web-app> 

這裏是你的項目應該如何看起來像:

/myjspapp 
    /file.jsp 
    /WEB-INF 
     /web.xml 

,你現在可以在訪問你的jsp以下網址:localhost:8080/myjspapp/about

+0

現在它不被識別,HTTP狀態500 - 缺少jspFile:[/file.jsp] – Billie

+0

我已經更新了我的文章,並附上了我的項目的圖片,請告訴我還有什麼問題? 我有和你一樣的'xml.web',但是當訪問'http:// localhost:8080/WebCoupons/about'時,我得到'HTTP Status 500 - missing jspFile:[/file.jsp]' – Billie

+0

你在'web.xml'中有' /file.jsp'(帶斜槓)嗎? – phury

0

這個答案與ph相似,但正如您所說的那樣在它給你的狀態500所以,爲你解決問題...

嘗試將JSP文件放在webcontent文件夾(僅適用於eclipse),然後按照你的例子。

我已經在你的例子,它給了我適當的輸出。

下面是我的項目結構,日食: -

enter image description here

下面是配置web.xml文件: - enter image description here

運行的服務器,並請求URL後模式「/ about」我得到了我的JSP輸出。

請嘗試,讓我知道...希望這有助於