You must have Javascript enabled to experience this site.

Start and Stop JSP Applications

Posted & filed under Coding Tips.

This is a technique for administering JSP Servlets.

Java Server Pages (JSP) is a technology to quickly develop dynamic web pages and is based on Java Servlets. In many ways JSP could be considered a macro language for creating Servlets.

When the web application container (usually Apache Tomcat) receives a request for a page it compiles the JSP page into a Servlet which then handles requests for this page until the page source is changed and it is recompiled; or the container is shutdown. While this is an easy way to develop JSP applications and even for deploying small applications, it lacks the finesse to manage large scale systems.

The first issue is that there is no way of coordinating startup and shutdown with other components. For example, if the application depends on a database this must be running before the servlet yet the servlet does not start until the page is requested, so the database must be running whenever the web container is running just in case the page is requested.

Secondly the only way to stop the servlet is to shutdown the web container, which if it is being used for multiple applications will be disruptive to other unrelated systems. The solution is to create a Servlet to run the JSP that can then be deployed to the web container and administered just like any other web application.

For example:

package au.id.paulshipley;

import java.io.IOException;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Servlet implementation class example from
 * http://www.caucho.com/resin-3.0/servlet/servlet.xtp
 *
 * copyright Paul Shipley, Australia, 2011
 *
 * @author Paul Shipley (pshipley @ melbpc.org.au)
 * @version $Id: JSPServlet.java 24 2011-07-15 11:51:04Z paul $
 */
public class JSPServlet extends HttpServlet{
   
    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = 1L;
   
    /** The log. */
    private Log log = LogFactory.getLog(getClass());

    /* (non-Javadoc)
     * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        log.info("Init");
    }

    /* (non-Javadoc)
     * @see javax.servlet.GenericServlet#destroy()
     */
    public void destroy() {
        log.info("Destroy");
    }

    /* (non-Javadoc)
     * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        log.info("Service");
        request.getRequestDispatcher("/index.jsp").include(request, response);
    }
}

 

Tomcat Admin page

 

Servlet example

 

Download the complete source here JSPServlet.war

 

References

Java Servlet http://en.wikipedia.org/wiki/Java_Servlet

JavaServer Pages http://en.wikipedia.org/wiki/JavaServer_Pages

 

 

 

0 views