Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@WebServlet annotation web.xml welcome-file

I would like to set the welcome-file of my JSP/JavaBeans project. I have a servlet named 'Controller.java' with the following @WebServlet annotation:

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

and I hava a web.xml file with the following content:

<?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>

Almost all things are going well, I can open http://localhost:8080/PROJECT/login and http://localhost:8080/PROJECT/show_dbs and I come to Controller.java. But when I open http://localhost:8080/PROJECT/ I get a 404 error.

I'm using Eclipse with a 'Dynamic Web Project', the Controller.java file is located under /src (default package) and the web.xml file is under /WebContent/WEB-INF.

I hope you have a tip for me.

like image 736
konze Avatar asked Mar 19 '13 18:03

konze


2 Answers

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.

like image 83
konze Avatar answered Nov 05 '22 23:11

konze


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

like image 30
informatik01 Avatar answered Nov 06 '22 01:11

informatik01