How to define Welcome Page in Java EE without a deployment descriptor

867 Views Asked by At

In a simple Java EE 8 web application, and since the deployment descriptor is not mandatory, and since a Servlet and a Filter can be registered in a ServletContextListener annotated with @WebListener, how can I define the list of welcome page programmatically without relying on a web.xml file?

3

There are 3 best solutions below

1
On

Use the @WebServlet annotation on the servlet class.

You can use it as @WebServlet("/Path") to make the servlet be at your.domain/context-root/Path.

If you want to have the servlet appear at multiple URLs then you can use @WebServlet(urlPatterns={"/Path/*", "/APath", "/"}.

For more details of the annotation see http://www.codejava.net/java-ee/servlet/webservlet-annotation-examples.

0
On

You may add a Servlet Filter which is mapped to URL "/" only and forward to your desired welcome file:

import java.io.IOException;

import javax.inject.Inject;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

@WebFilter(filterName = "WelcomeFilter", urlPatterns = { "/" })
public class WelcomeFilter extends Object implements Filter {

    @Inject
    ServletContext context;

    public WelcomeFilter() {
        super();
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        context.getRequestDispatcher("/welcome-file.html").forward(req, resp);

        chain.doFilter(req, resp);
    }
}
0
On

It's tough to prove a negative, but I think that the answer is that you still need a deployment descriptor for welcome files with EE 8, which equates to Servlet 4.0.

I can't definitively prove that, but it is strongly suggested by careful reading of section 10.10 Welcome Files of JSR-369, the Servlet 4.0 Specification, which goes into considerable detail on how the welcome list works:

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.

In particular, note the first and final sentences; the specification makes no mention of alternative possible approaches available to the developer. In the absence of any match being found in the welcome list, the container itself decides what to do.

So while the deployment descriptor is not always mandatory, it is mandatory if you want to have welcome files, even with EE 8. Section 10.13 Inclusion of a web.xml Deployment Descriptor in the specification briefly discusses when you need a deployment descriptor:

A web application is NOT required to contain a web.xml if it does NOT contain any Servlet, Filter, or Listener components or is using annotations to declare the same. In other words an application containing only static files or JSP pages does not require a web.xml to be present.

These SO posts regarding welcome lists with Servlet 3.0 may also be of interest: