Java Servlet Pluggability

What is Servlet Pluggability?

  • Servlet 3.0 introduces the concept of pluggability.
  • In recent times the standard Java EE and third party framework has been built on top of the Servlet API.
  • Examples of such standard frameworks include JSP's and JSF, third-party frameworks include Struts, Wicket, Spring Web MVC, and several others.
  • The reason for this is to make development more structured and easier to maintain in the future.
  • Almost none Java web applications are built using the Servlet API directly. Yet it is important to know the Servlet technology which all framework is built on top of.
  • To set up a program to use one of these frameworks mentioned above can involve some configuration changes in the application web.xml deployment descriptor.
  • Some applications use more than one framework.which causes that the web.xml deployment descriptor grows and hard to maintain.
  • There is two ways to avoid having application developers modify the web.xml deployment descriptor:
    1. Use annotations instead of a web.xml to configure their servlets.
    2. To include a web-fragment.xml file as part of the JAR file to be included.
  • The fragment file web-fragment.xml is almost identical to web.xml with the main differences:
    1. The root element of a web-fragment.xml file is <web-fragment> as opposed to <web-app>.
    2. The web-fragment.xml file has an <ordering> tag for the sequence specification of filters as web.xml has an <absolute-ordering> tag for the same

Example of Servlet Pluggability using fragment.

As selected we use Netbeans IDE and Glassfish Server.

You can download this example here (needed tools can be found in the right menu on this page).

If you like to participate in the review of this example you must first create a Web project in Netbeans (the project name is ServletPluggability).

We want to include a Servlet in a java project as a fragment to the web-project. This require that we create a new java application in Netbeans , Servletfragment, with a Java EE library.

  • Servlet example to be included:
    package my.web;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @WebServlet("/")
    public class WebFragmentServlet extends HttpServlet {
      @Override
      protected void doGet(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
        System.out.println(System.currentTimeMillis() + " Request WebFragmentServlet");
        response.getOutputStream().print("<p>\n");
        response.getOutputStream().print("Using a web framework has never being this easy!");
        response.getOutputStream().print("</p>\n");
        System.out.println(System.currentTimeMillis() + " Response WebFragmentServlet");
      }
    }

    For those who participate in the review: create a Servlet in Netbeans and replace generated code for the servlet with that shown above (the servlet name is WebFragmentServlet).

  • Left click the project to add a web Fragment Deployment Descriptor (web-fragment.xml).
    web-fragment.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-fragment xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    	 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
    http://xmlns.jcp.org/xml/ns/javaee/web-fragment_3_1.xsd"
    	 version="3.1">
    
    </web-fragment>

We want to include a Filter in a java project as a fragment to the web-project. This require that we create a new java application in Netbeans , Filterfragment, with a Java EE library.

  • Servlet filter example to be included:
    package my.web;
    
    import java.io.IOException;
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.annotation.WebFilter;
    
    @WebFilter(urlPatterns = {"/"})
    public class WebfragmentFilter implements Filter {
    
      @Override
      public void init(FilterConfig filterConfig) throws ServletException {
      }
    
      @Override
      public void doFilter(ServletRequest req, ServletResponse res,
              FilterChain chain) throws IOException, ServletException {
    
        System.out.println(System.currentTimeMillis() + " Request WebfragmentFilter");
        chain.doFilter(req, res);
        System.out.println(System.currentTimeMillis() + " Response WebfragmentFilter");
      }
    
      @Override
      public void destroy() {
      }
    }

    For those who participate in the review: create a Servlet in Netbeans and replace generated code for the servlet with that shown above (the servlet name is WebfragmentFilter).

  • Left click the project to add a web Fragment Deployment Descriptor (web-fragment.xml).
  • Add the <ordering> tag as shown belove. This causes the filter to be performed prior to the servlet.
    web-fragment.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-fragment version="3.0" 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-fragment_3_0.xsd">
      <ordering>
        <before>
          <others/>
        </before>
      </ordering>
    </web-fragment>
  • You must now build the two last Java project to create .jar files in the dist directory (look in the dist directory under the Files tab).
  • Go back to the ServletPluggability project and include the .jar files from the two last project (Servletfragment and Filterfragment).

Creating Web-server Deployment descriptor.

  • The context-root (in example /ServletPluggability) for the application will in most cases be specified by a server vendor deployment descriptor.

    For those who participate in the review: create a Glassfish deployment descriptor in Netbeans.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, 
    Inc.//DTD GlassFish Application Server 3.0 Servlet 3.0//EN" 
    "http://www.sun.com/software/appserver/dtds/sun-web-app_3_0-0.dtd">
    <sun-web-app error-url="">
      <context-root>/ServletPluggability</context-root>
      <class-loader delegate="true"/>
      <jsp-config>
        <property name="keepgenerated" value="true">
          <description>
            Keep a copy of the generated servlet class' java code.
          </description>
        </property>
      </jsp-config>
    </sun-web-app>

Run the application.

  • For those who participate in the review: right click the Web-project and select Run.
  • This results in that the application will be deployed to the server and started.
  • The browser will display:

    Using a web framework has never being this easy!

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.