jump to navigation

Redirecting and Dispatching Requests August 13, 2005

Posted by Coolguy in Servlets/Jsp.
trackback
  • Options are redirect and dispatch

Redirect

  • Servlet redirect makes browser do the work
  • Response code 301 is returned with the Location header set to the URL of new location
  • Eg: response.sendRedirect(http://www.xyz.com);
  • A relative URL could be used in sendRedirect(). It takes only a string.
  • A forward slash in the begining means relative to the root of this webapp
  • You cannot do a sendRedirect() after writing to response.
  • This will cause illegalStateException

RequestDispatcher

  • Work is done on the server side
  • RequestDispatchers have two methods:
    forward();
    include();
  • Both methods take request and reponse objects
  • forward() is most popular
  • RequestDispatcher view=request.getRequestDispatcher(“results.jsp”);
    view.forward(request,response);
  • You cannot forward the request if you’ve already commited a response
  • The RequestDispatcher interface provides a mechanism to forward processing of a request to another servlet, or to include the output of another servlet in the response.
  • An object implementing the RequestDispatcher interface may be obtained from the ServletContext via the following methods:
    • getRequestDispatcher
    • getNamedDispatcher
  • The getRequestDispatcher method takes a String argument describing a path within the scope of the ServletContext. This path must be relative to the root of the ServletContext and begin with a ‘/’. The method uses the path to look up a servlet, using the servlet path matching rules, wraps it with a RequestDispatcher object, and returns the resulting object. If no servlet can be resolved based on the given path, a RequestDispatcher is provided that returns the content for that path.
  • The getNamedDispatcher method takes a String argument indicating the name of a servlet known to the ServletContext. If a servlet is found, it is wrapped with a RequestDispatcher object and the object is returned. If no servlet is associated with the given name, the method must return null.
  • There is a getRequestDispatcher method is provided in the
    ServletRequest interface also.
  • The behavior of this method is similar to the method of the same name in the ServletContext
  • The ServletContext and ServletRequest methods that create RequestDispatcher objects using path information allow the optional attachment of query string information to the path.
  • For example, a Developer may obtain a RequestDispatcher
    by using the following code:
    String path =”/raisins.jsp?orderno=5 “;
    RequestDispatcher rd =context.getRequestDispatcher(path);
    rd.include(request,response);
  • To use a request dispatcher, a servlet calls either the include method or forward method of the RequestDispatcher interface.
  • The parameters to these methods can be either the request and response arguments that were passed in via the service method of the javax.servlet interface, or instances of subclasses of the request and response wrapper classes
  • The Container Provider should ensure that the dispatch of the request to a
    target servlet occurs in the same thread of the same JVM as the original request.
  • The include method of the RequestDispatcher interface may be called at any time. The target servlet of the include method has access to all aspects of the request object, but its use of the response object is more limited.
  • It cannot set headers or call any method that affects the headers of the response. Any attempt to do so must be ignored.
  • It can only write information to the ServletOutputStream or Writer of the
    response object and commit a response by writing content past the end of the response buffer, or by explicitly calling the flushBuffer method of the
    ServletResponse interface.
  • A servlet that has been invoked by another servlet using the include method of RequestDispatcher has access to the path by which it was invoked. The following request attributes must be set:
    javax.servlet.include.request_uri
    javax.servlet.include.context_path
    javax.servlet.include.servlet_path
    javax.servlet.include.path_info
    javax.servlet.include.query_string
  • If the included servlet was obtained by using the getNamedDispatcher
    method, these attributes must not be set.
  • The forward method of the RequestDispatcher interface may be called by the calling servlet only when no output has been committed to the client.
  • If output data exists in the response buffer that has not been committed, the content must be cleared before the target servlet’s service method is called. If the response has been committed, an IllegalStateException must be thrown.
  • A servlet that has been invoked by another servlet using the forward method of RequestDispatcher has access to the path of the original request. The following request attributes must be set:
    javax.servlet.forward.request_uri
    javax.servlet.forward.context_path
    javax.servlet.forward.servlet_path
    javax.servlet.forward.path_info
    javax.servlet.forward.query_string
  • If the forwarded servlet was obtained by using the getNamedDispatcher
    method, these attributes must not be set.
  • If the servlet that is the target of a request dispatcher throws a runtime exception or a checked exception of type ServletException or IOException , it should be propagated to the calling servlet.
  • All other exceptions should be wrapped as ServletExceptions and the root cause of the exception set to the original exception, as it should not be propagated.

Gotchas

  • Only <listener-class> element is required sub-element of <listener> element
  • getValue() returns old value
Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: