我有一个基本的servlet。

我使用tomcat 7来运行它。

服务器包含3个文件:


file.jsp-打印日期
WebController-Servlet。
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>


当我从<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.


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

最佳答案

您的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


您现在可以通过以下URL访问jsp:localhost:8080/myjspapp/about

09-25 16:47
查看更多