Tuesday, January 22, 2013

Servlet 3.0 ( Tidbits ) ......1

Different between RequestDispatcher  in context and request .

RequestDispatcher can be obtained either from ServletContext or from ServletRequest.

An object implementing the RequestDispatcher interface may be obtained from the ServletContext via the following methods:
■ getRequestDispatcher(String Path)
■ getNamedDispatcher(String Servletname)

The getRequestDispatcher method takes a String argument path. This path must be relative to the root of the
ServletContext and begin with a ‘/’, or be empty. The method uses the path to look up a servlet, using the servlet path matching rules,and 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.

To allow RequestDispatcher objects to be obtained using relative paths that are relative to the path of the current request (not relative to the root of the ServletContext) (if omitting the / slash ), the  getRequestDispatcher method is provided in the ServletRequest interface.
The servlet container uses information in the request object to transform the given relative path against the current servlet to a complete path. For example,
 in a context rooted at ’/’ and a request to /garden/tools.html, a request dispatcher obtained via ServletRequest.getRequestDispatcher("header.html") will behave exactly like a call to ServletContext.getRequestDispatcher("/garden/header.html").

Query Strings in Request Dispatcher Paths

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);


Parameters specified in the query string used to create the RequestDispatcher take precedence over other parameters of the same name passed to the included servlet.
The parameters associated with a RequestDispatcher are scoped to apply only for the duration of the include or forward call.

No comments: