我想设置 JSP/JavaBeans 项目的欢迎文件。我有一个名为“Controller.java”的 servlet,带有以下 @WebServlet 注释:
@WebServlet(name="Controller", urlPatterns={"/login", "/show_dbs"})
我有一个包含以下内容的 web.xml 文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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_3_0.xsd" version="3.0">
<welcome-file-list>
<welcome-file>Controller</welcome-file>
</welcome-file-list>
</web-app>
几乎一切都很顺利,我可以打开http://localhost:8080/PROJECT/login
and http://localhost:8080/PROJECT/show_dbs
我来到Controller.java。但当我打开http://localhost:8080/PROJECT/
我收到 404 错误。
我将 Eclipse 与“动态 Web 项目”一起使用,Controller.java 文件位于 /src(默认包)下,web.xml 文件位于 /WebContent/WEB-INF 下。
我希望你能给我一个建议。
感谢您的帮助。这是我的解决方案:
如果您想将 servlet 设置为欢迎文件,您必须执行以下操作:
将标准 html 定义为欢迎文件,例如 web.xml 中的 index.html:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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_3_0.xsd" version="3.0">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
确保这个文件(index.html)不存在.
在 @WebServlet 中定义 urlPatterns,如下所示:
@WebServlet(name="Controller", urlPatterns={"/index.html", "/login", "/show_dbs"})
现在每个请求http://.../PROJECT/
(root) 将被重定向到http://.../PROJECT/index.html
这会调用 servlet。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)