@WebServlet annotation web.xml welcome-file
Asked Answered
E

5

8

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.

Electioneer answered 19/3, 2013 at 18:47 Comment(3)
I could be wrong but I don't think you can put a java class in the welcome file list. Create a jsp file like index.jsp and use that instead. <welcome-file>index.jsp</welcome-file>. This way when yo access localhost:8080/PROJECT, you should get forwarded to index.jspRoque
Thank you for your answer. But it has to be 'Controller.java' because it handles the request depending on the session.Electioneer
See this #13450544Shush
K
8

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

Kippy answered 20/3, 2013 at 20:18 Comment(0)
E
9

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.

Electioneer answered 21/3, 2013 at 14:29 Comment(1)
As a side note, with servlets what you define with url-pattern is just a logical / virtual path for the servlet. Again, it is NOT a real path to the physical location of the servlet class, but a logical path - you make it up as you wish. So it could be pretty much whatever you like. I mean, not necessarily index.html, it could be just /index or foo or foo.bar or whatever. P.S. Congrats, now you have enogh points to upvote ))Kippy
K
8

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

Kippy answered 20/3, 2013 at 20:18 Comment(0)
R
2

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>
Roque answered 19/3, 2013 at 19:56 Comment(0)
R
1

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

Randyranee answered 18/3, 2015 at 13:40 Comment(0)
A
1

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

@WebServlet(name="Controller", urlPatterns={"/index.html","/login", "/show_dbs"})
Antoinette answered 12/4, 2016 at 22:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.