SK.gnome.dwarf.http.servlet
Class ReflectionServlet

java.lang.Object
  extended byjavax.servlet.GenericServlet
      extended byjavax.servlet.http.HttpServlet
          extended bySK.gnome.dwarf.http.servlet.ReflectionServlet
All Implemented Interfaces:
java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig
Direct Known Subclasses:
LoginServlet

public abstract class ReflectionServlet
extends javax.servlet.http.HttpServlet

This servlet uses Java reflection to choose the desired servlet action.

The particular action is specified either via the request path info or via the query string parameter. A corresponding method with the following signature must be declared by the extending class for each particular servlet action:

   public void myAction(Request request, Response response) throws IOException, ServletException
 

The abstract handle(Request, Response) method defines the default servlet action, i.e. when no particular action is required.

Let's say that we need to implement a GreetingServlet with two special actions: hello and goodbye. Let's the hello action be the default servlet action. We need then to subclass the ReflectionServlet as follows:

 public class GreetingServlet extends ReflectionServlet
 {
   public void hello(Request request, Response response) throws IOException, ServletException
   { PrintWriter out = response.getWriter();
     response.setContentType("text/plain");
     out.println("Hello world!");
   }

   public void goodbye(Request request, Response response) throws IOException, ServletException
   { PrintWriter out = response.getWriter();
     response.setContentType("text/plain");
     out.println("Goodbye world!");
   }

   public void handle(Request request, Response response) throws IOException, ServletException
   { hello(request, response);
   }
 }
 

Let's assume that the servlet is mapped to the "/greeting" path. The following request URI paths are then possible:

The action specified via the request path info takes priority over the action specified via the request parameter.

See Also:
Serialized Form

Constructor Summary
ReflectionServlet()
           
 
Method Summary
 void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
           
 void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
           
protected  java.lang.String getMethodName(Request request)
          Lookups the action method name.
 java.lang.String getServletInfo()
           
abstract  void handle(Request request, Response response)
          The default servlet action.
protected  void handleActionNotFound(Request request, Response response)
          Handles the request when the desired action was not found.
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doOptions, doPut, doTrace, getLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletName, init, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionServlet

public ReflectionServlet()
Method Detail

getServletInfo

public java.lang.String getServletInfo()

doGet

public void doGet(javax.servlet.http.HttpServletRequest request,
                  javax.servlet.http.HttpServletResponse response)
           throws java.io.IOException,
                  javax.servlet.ServletException
Throws:
java.io.IOException
javax.servlet.ServletException

doPost

public void doPost(javax.servlet.http.HttpServletRequest request,
                   javax.servlet.http.HttpServletResponse response)
            throws java.io.IOException,
                   javax.servlet.ServletException
Throws:
java.io.IOException
javax.servlet.ServletException

getMethodName

protected java.lang.String getMethodName(Request request)
Lookups the action method name.

It si called by the servlet to do the actual method lookup according to the given request. Can be overriden in order to implement a different lookup strategies.

This method first tries to obtain the method name from the path info part of the request URL, and if not found, it tries to obtain it from the action parameter of the query string. If both attempts fail, it returns the name of the default method, i.e. the "handle" string.

Parameters:
request - the request
Returns:
the method name

handleActionNotFound

protected void handleActionNotFound(Request request,
                                    Response response)
                             throws java.io.IOException,
                                    javax.servlet.ServletException
Handles the request when the desired action was not found.

It sends an error message to the client informing that the servlet action was not found.

Parameters:
request - the request
response - the response
Throws:
java.io.IOException - in the case of an I/O error
javax.servlet.ServletException - general servlet exception

handle

public abstract void handle(Request request,
                            Response response)
                     throws java.io.IOException,
                            javax.servlet.ServletException
The default servlet action.

Parameters:
request - the request
response - the response
Throws:
java.io.IOException - in the case of an I/O error
javax.servlet.ServletException - general servlet exception


Copyright (c) 1999-2005, Gnome Ltd. All rights reserved.