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.
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 likehost: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