Expanding the simple JSTL test application 

Joined:
08/13/2009
Posts:
164

April 02, 2010 21:45:47    Last update: July 17, 2010 02:55:06
This is built upon the simple test application for JSTL, which contained a single servlet and a single JSP page. If I want to use it to test all available JSTL tags, the servlet and JSP page would be too complicated. Instead, I want to group the JSTL tags into separate JSP pages and display each group based on the requested URL. For example, if the URL ends with /CoreBasic, I'll display a page that contains the basic core tags; if the URL ends with /I18N, I'll display a page that contains the internationalization tags (e.g., <fmt:message>). Furthermore, I want to delegate the handling of each group of tags to separate Java classes.

This is the application I'll use for the rest of this tutorial.

Make these changes to the simple application:
  1. Create an interface (DemoHandler.java)
    package jstl.demo;
    
    import java.io.IOException;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public interface DemoHandler {
        public void handleRequest(HttpServletRequest req,
    			      HttpServletResponse resp)
    		throws IOException, ServletException;
    }
    

  2. Update the servlet (JSTLDemoServlet.java)
    package jstl.demo;
    
    import java.io.IOException;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public class JSTLDemoServlet extends HttpServlet {
        private String packageName;
    
        public void init(ServletConfig cfg) throws ServletException {
    	this.packageName = cfg.getInitParameter("handlerPackageName");
        }
    
        public void service(HttpServletRequest req,
    		        HttpServletResponse resp) 
    		throws IOException, ServletException {
    	String pathInfo = req.getPathInfo();
    	if ((pathInfo == null) || pathInfo.equals("/")) { 
    	    req.setAttribute("world", "JSTL Demo");
    	    RequestDispatcher d = req.getRequestDispatcher("/home.jsp");
    	    d.forward(req, resp);
    	    return;
    	}
    
    	// instantiate the handler based on path info
    	int idx = pathInfo.indexOf("/", 1);
    	String className = idx > 0 ?
    			   pathInfo.substring(1, idx) :
    			   pathInfo.substring(1);
    
    	try {
    	    Class c = Class.forName(packageName + "." + className);
    	    DemoHandler h = (DemoHandler) c.newInstance();
    	    h.handleRequest(req, resp);
    	}
    	catch (ClassNotFoundException e) {
    	    req.setAttribute("pathInfo", pathInfo);
    	    RequestDispatcher d = req.getRequestDispatcher("/nohandler.jsp");
    	    d.forward(req, resp);
    	}
    	catch (InstantiationException e) {
    	    throw new ServletException(e);
    	}
    	catch (IllegalAccessException e) {
    	    throw new ServletException(e);
    	}
        }
    }
    

  3. Add a JSP (nohandler.jsp)
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <html>
    <head>
    <title>No Handler</title>
    </head>
    
    <body>
    <h2>No Handler</h2>
    <p>No handler found for <c:out value="${pathInfo}"/></p>
    </body>
    </html>
    

  4. Update web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app 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-app_2_5.xsd"
    	 version="2.5">
        <display-name>Simple JSTL Demo Application</display-name>
        <servlet>
            <servlet-name>jstldemo</servlet-name>
            <servlet-class>jstl.demo.JSTLDemoServlet</servlet-class>
    
    	<!-- package name for handler classes --> 
            <init-param>
    	    <param-name>handlerPackageName</param-name>
    	    <param-value>jstl.demo.handler</param-value>
            </init-param>
    
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>jstldemo</servlet-name>
            <url-pattern>/demo/*</url-pattern>
        </servlet-mapping>
    </web-app>
    

  5. The final directory structure should look like this:
    ./pom.xml
    ./src
    ./src/main
    ./src/main/java
    ./src/main/java/jstl
    ./src/main/java/jstl/demo
    ./src/main/java/jstl/demo/DemoHandler.java
    ./src/main/java/jstl/demo/JSTLDemoServlet.java
    ./src/main/resources
    ./src/main/webapp
    ./src/main/webapp/home.jsp
    ./src/main/webapp/index.jsp
    ./src/main/webapp/nohandler.jsp
    ./src/main/webapp/WEB-INF
    ./src/main/webapp/WEB-INF/web.xml
    



Currently there are no handlers, all URLs except that of the home page will lead you to the "No Handler" page. We'll add the handlers one by one in the following sections.
Share |
| Comment  | Tags