|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object javax.servlet.GenericServlet javax.servlet.http.HttpServlet SK.gnome.dwarf.http.servlet.ReflectionServlet
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:
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 |
public ReflectionServlet()
Method Detail |
public java.lang.String getServletInfo()
public void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException
java.io.IOException
javax.servlet.ServletException
public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException
java.io.IOException
javax.servlet.ServletException
protected java.lang.String getMethodName(Request request)
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.
request
- the request
protected void handleActionNotFound(Request request, Response response) throws java.io.IOException, javax.servlet.ServletException
It sends an error message to the client informing that the servlet action was not found.
request
- the requestresponse
- the response
java.io.IOException
- in the case of an I/O error
javax.servlet.ServletException
- general servlet exceptionpublic abstract void handle(Request request, Response response) throws java.io.IOException, javax.servlet.ServletException
request
- the requestresponse
- the response
java.io.IOException
- in the case of an I/O error
javax.servlet.ServletException
- general servlet exception
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |