Web.xml导致Tomcat7失败

时间:2013-11-25 17:29:46

标签: java xml jsp tomcat servlets

我有一个基本的servlet。

我使用tomcat 7来运行它。

服务器包含3个文件:

  1. file.jsp - 打印日期

  2. WebController - servlet。

  3. web.xml - 配置文件。

  4. 的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>
    

    当我从<servlet>文件中删除<servlet-mapping>web.xml时,servlet运行良好。

    web.xml如上所述时,我收到以下错误消息:

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

    我正在使用eclipse。我的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>
    

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

web.xml文件中servlet映射的一种可能格式如下所示:

<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" />

答案 2 :(得分:0)

这个答案类似于ph,但正如你所说它给你的状态500所以,为你解决问题......

尝试将JSP文件放在webcontent文件夹中(仅限eclipse),然后按照您的示例进行操作。

我已经研究了你的例子,它给了我正确的输出。

以下是我在eclipse中的项目结构: -

enter image description here

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

运行服务器并请求URL模式“/ about”后,我得到了JSP的输出。

请尝试让我知道...希望这有帮助