@WebServlet annotation web.xml welcome-file

∥☆過路亽.° 提交于 2019-11-30 07:26:35

In the welcome file list you must specify the URIs. But you have specified the name of the servlet.

Quote from the Java™ Servlet Specification version 3.0 (emphasis mine):

10.10 Welcome Files

Web Application developers can define an ordered list of partial URIs called welcome files in the Web application deployment descriptor. The deployment descriptor syntax for the list is described in the Web application deployment descriptor schema.

The purpose of this mechanism is to allow the deployer to specify an ordered list of partial URIs for the container to use for appending to URIs when there is a request for a URI that corresponds to a directory entry in the WAR not mapped to a Web component. This kind of request is known as a valid partial request.

The use for this facility is made clear by the following common example: A welcome file of 'index.html' can be defined so that a request to a URL like host:port/webapp/directory/, where 'directory' is an entry in the WAR that is not mapped to a servlet or JSP page, is returned to the client as 'host:port/webapp/directory/index.html'.

If a Web container receives a valid partial request, the Web container must examine the welcome file list defined in the deployment descriptor. The welcome file list is an ordered list of partial URLs with no trailing or leading /. The Web server must append each welcome file in the order specified in the deployment descriptor to the partial request and check whether a static resource in the WAR is mapped to that request URI. If no match is found, the Web server MUST again append each welcome file in the order specified in the deployment descriptor to the partial request and check if a servlet is mapped to that request URI. The Web container must send the request to the first resource in the WAR that matches. The container may send the request to the welcome resource with a forward, a redirect, or a container specific mechanism that is indistinguishable from a direct request.

If no matching welcome file is found in the manner described, the container may handle the request in a manner it finds suitable. For some configurations this may mean returning a directory listing or for others returning a 404 response.


P.S.

Also see the examples in the specification in the chapter 10.10

Thank you for your help. Here comes my solution:

If you want to set your servlet as welcome file you have to do the following:

Define a standard html as welcome-file such as index.html in your 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>index.html</welcome-file>
    </welcome-file-list>
</web-app>

Make sure this file (index.html) doesn't exist.

Define your urlPatterns in @WebServlet like this:

@WebServlet(name="Controller", urlPatterns={"/index.html", "/login", "/show_dbs"})

Now every request to http://.../PROJECT/ (root) will be redirected to http://.../PROJECT/index.html and this calls the servlet.

This is what your web.xml should be. Create an index.jsp. Use the index.jsp as your welcome file. The controller class is your sevlet. So define a servlet in your web.xml as follows. This should cause all requests to be directed to the Controller class. And you should still be able to access localhost:8080/PROJECT/, in which case it will be directed to your welcome file.

If you don't want to create index.jsp, you can use your existing jsp file, may be your login.jsp file as your welcome file. In which case, just replace the index.jsp with login.jsp

<?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.jsp</welcome-file>
    </welcome-file-list>
    <servlet>   
        <servlet-name>Controller</servlet-name>
        <servlet-class>com.company.Controller</servlet-class>   
    </servlet>
    <servlet-mapping>
        <servlet-name>Controller</servlet-name>
        <url-pattern>*</url-pattern>
    </servlet-mapping>  
</web-app>

The default welcome file is index.html, so just add the URL pattern.

@WebServlet(name="Controller", urlPatterns={"/index.html","/login", "/show_dbs"})

You can specify the url "/Controller" in urlPatterns in @WebServlet annotation. I think it will work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!