如何使Eclipse提供默认的servlet URL?

时间:2018-08-13 01:51:04

标签: java servlets

我的servlet代码中有此行(在动态Web项目中):

@WebServlet("/login")

但是当我右键单击项目,运行方式,在服务器上运行时,它就起作用了

http://localhost:8090/LoginServletApp/

其结果为404。我必须手动将网址更改为

http://localhost:8090/LoginServletApp/login

如何默认提供该网址(即以“ / login”结尾)?我已经四处查看了,提到了一个名为web.xml的文件,可以在每个项目的基础上创建该文件,但这似乎适用于物理文件。我要求eclipse生成一个部署描述符,它为我创建了这个文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
  <display-name>LoginServletApp</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

在此先感谢您,如果在此之前被问过(很可能已经提出,但我找不到它或解决方案),我们深表歉意。

编辑:我将以下行添加到web.xml文件中,但是按预期无法正常工作:

<welcome-file>login.html</welcome-file>

1 个答案:

答案 0 :(得分:2)

根据welcome-file-list中的条目,容器按顺序查找文件。因此,如果您首先将登录名添加到列表中,则会首先提供该登录名,并且不会显示404。

<welcome-file-list>
    <welcome-file>login</welcome-file>
    ..
</welcome-file-list>