我有一个基本的 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.
我正在使用 eclipse 。我的 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
您现在可以通过以下网址访问您的 jsp:localhost:8080/myjspapp/about
关于java - web.xml导致Tomcat7失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20199412/