简体   繁体   中英

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

在一个简单的 Java EE 8 web 应用程序中,并且由于部署描述符不是强制性的,并且由于可以在使用@WebListener注释的ServletContextListener注册ServletFilter ,我如何以编程方式定义欢迎页面列表而不依赖于web.xml文件?

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:

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 .

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM