Thursday, September 15, 2005

Before doing a RequestDispatcher forward, what are the attributes set by the container in the request object?

What are the attributes set by the container before doing an include?

Also, what is the purpose of setting these? I suppose these are already accessible through the request object. For example, the forwarded resource may use request.getQueryString() instead of request.getAttribute("javax.servlet.forward.query_string").

The attributes are:
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

and for include

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


When calling RequestDispatcher.forward, we can specify new query parameters in a query string or override the existing query parameters. The getQueryString method on the forward page returns this query string. But request.getAttribute("javax.servlet.forward.query_string") returns the original query parameters.

Example:

If you call a Test servlet as:

http://localhost:8080/app/Test/?x=100

In the servlet,

RequestDispatcher rd = request.getRequestDispatcher("/Target?xyz=1");
rd.forward(request, response);

In the forwarded Target servlet,

request.getQueryString(); // prints "xyz=1"

But request.getAttribute("javax.servlet.forward.query_string"); // prints "x=100"

You can learn about this Here

0 Comments:

Post a Comment

<< Home