Mohammad Abed wrote:
>
>
> If I have a Servlet that controls the access to a web site. Should I
> have one RequestDispactcher to handle any request, or should I have a
> RequestDispatcher for each request. "callingPage" is a hidden field in
> each html page
>
> RequestDispatcher pageRD = null;
>
> System.out.println("This is ControlServlet....Please reload
> it....");
> String prevPage = request.getParameter("callingPage");
> System.out.println("prevPage==========="+ prevPage);
> System.out.println("pathInfo==========="+ request.getPathInfo());
>
> if (prevPage == null) {
> System.out.println("************Calling index.jsp");
> //directPageTo(request,response, pageRD, "index.jsp");
> pageRD = this.getServletContext().
> getRequestDispatcher("/index.jsp");
> pageRD.forward(request, response);
> }
> else if (prevPage.equals("registration.html")){
> System.out.println("**********calling agreement.jsp");
> pageRD = this.getServletContext().
> getRequestDispatcher("/register/agreement.jsp");
> pageRD.forward(request, response);
> }else if (prevPage.equals("agreement.jsp")){
> System.out.println("**********in registration.html");
> if (request.getParameter("radiobutton").equals("parent")) {
> System.out.println("in parent" +
> request.getParameter("radiobutton"));
> pageRD = this.getServletContext().
> getRequestDispatcher("/register/register_parent.html");
> pageRD.forward(request, response);
> }
For doing an "if ... else if" chain like this, you're doing it OK. Only
one RequestDispatcher will actually get created -- the one for which
your "previous page" test returns true instead of false. You might find
it more maintainable, however, to follow a different strategy like this:
* In the init() method of your servlet, build a Hashtable
or Properties object where the key is the "previous page"
value, and the value is the URL to which you should
dispatch to next.
* Rather than hard coding them, configure this mapping
from a properties file, so that you only have to add one
new line (in the properties file) for a newly added mapping.
No recompilation of the servlet is required -- just a restart.
* Because the mapping table is read-only once it has been
initialized, you can store it in an instance variable in the
servlet with no multithreading concerns.
* In the request processing part of your servlet, you would
replace the "if .. else if" chain by something like this, where
"mapping" is the name of the Hashtable:
String prevPage = request.getParameter("callingPage");
String nextPage = (String) mapping.get(prevPage);
if (nextPage != null) {
RequestDispatcher rd =
getServletContext().getRequestDispatcher(nextPage);
if (rd != null) {
rd.forward(request, response);
} else {
... deal with invalid next page problem ...
}
} else {
... deal with no previous page problem ...
}
This approach still creates only one new RequestDispatcher object, but
it's easier to maintain because you don't have to add processing logic
every time you add a new forwarding requirement.
You might also consider looking up the value of the HTTP "Referer"
header:
String referer = request.getHeader("Referer");
which gives you back the URL of the previous page (i.e. the page from
which the submit was done), instead of having to use a hidden variable
every single time.
Craig McClanahan
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html