Saturday, March 3, 2012

Basic Servlet Practice: Dynamic add servlet programmatically.

Sometimes we may want to dynamically add the servlet and/or change their feature,
for example, support the servlet 3 asynchronous processing.

To achieve this, we can dynamically add servlet in
ServletContextListener as below:


DynamicAddListener.java

package test.servlet.dynamicaddservlet;

import javax.servlet.*;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRegistration;
import javax.servlet.annotation.WebListener;

/**
 * This listener will add the DynamicAddedServlet if servlet version is 3.0 or later
 *
 */
@WebListener()
public class DynamicAddListener implements ServletContextListener {
    private static final long serialVersionUID = -8873939883201271898L;

    @Override
    public void contextInitialized(ServletContextEvent event) {
        // Dynamic add servlet for servlet 3.x or later
        ServletContext sc = event.getServletContext();
        if (sc.getMajorVersion() >= 3) {
            final ServletRegistration.Dynamic dn =
                    sc.addServlet("DynamicAddedServlet", DynamicAddedServlet.class);
            dn.setAsyncSupported(true);
            dn.addMapping("/added.jsp");
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        
    }
}

DynamicAddedServlet.java

package test.servlet.dynamicaddservlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * This servlet will be added by DynamicAddListener#contextInitialized
 *
 */
public class DynamicAddedServlet extends HttpServlet {

    private static final long serialVersionUID = -8873939883201271898L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        ServletContext sc = getServletContext();
        PrintWriter out = resp.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title> Servlet Added </title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h2 style=\"margin: 50px;\">The Servlet Version is 3.0 or later so<br />"
                    + "&nbsp;the Servlet is added.<br /><br />"
                    + "&nbsp;&nbsp;Servlet Version: "+sc.getMajorVersion()+"."+sc.getMinorVersion()+"</h2>");
        out.println("</body>");
        out.println("</html>");
        out.close();
    }
}


If we run this Web-App with Tomcat7 then connect to /added.jsp,
the result is:



Download:

Files at github:
https://github.com/benbai123/JSP_Servlet_Practice/tree/master/Practice/BasicServletPractice/src/test/servlet/dynamicaddservlet

Reference:

http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#addServlet(java.lang.String, java.lang.Class)

No comments:

Post a Comment