Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question regarding RequestDispatcher forward (not in HFSJ)

 
B.Sathish
Ranch Hand
Posts: 372
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am asking this question because this is one area not properly covered in HFSJ, but present in the exam.

Before doing a RequestDispatcher forward, what are the attributes set by the container in the request object? I know javax.servlet.forward.query_string is one of them. What are the others?

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").

Please let me know your answers
 
Paolo Metafune
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

And i think that is the same to read these attributes or to use the corrispondent methods of HttpServletRequest.

Bye
 
Gouri Bargi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And i think that is the same to read these attributes or to use the corrispondent methods of HttpServletRequest.

/QB]


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. the request.getAttribute("javax.servlet.forward.query_string") returns the original query parameters.

For example, if we have an original request like

http://localhost:8080/search/searchentry?key=abc

Assume that we have a servlet SearchEntry (url-pattern searchentry). In this servlet, we perform the search, and find out the relevant records, set these as an attribute for the request, and forward it as follows:
[CODE}
RequestDispatcher dispatcher = req.getRequestDispatcher(
"searchresult.jsp" + "?result=success");
dispatcher.forward(req, res);
[/CODE]

In this scenario, in searchresult.jsp,

will return result=success.


will return the original query string, ie key=abc.

- Gouri
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wonderful reply Gouri Bargi.
I had the same doubt for a long time.

We already had a question regarding this topic.

http://www.coderanch.com/t/170378/java-Web-Component-SCWCD/certification/HFSJ-Ch-Mock-Exam

As Edmund has mentioned Question 12 on page 219 of HFSJ has a wrong option (OR) option has to be modified.

Given Option:
The servlet to which a request is forwarded may access the original query string by calling getQueryString() on the servletRequest.

This option has to be modified as

The servlet to which a request is forwarded may access the original query string by calling getAttribute("javax.servlet.forward.query_string") on the servletRequest.

OR

The servlet to which a request is forwarded may access the query string by calling getQueryString() on the servletRequest.

I had send a mail to errata section regarding the same.
 
Gouri Bargi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vishnu. What I have found out is, you learn much more by trying to reply the posts on this forum and try out different problems.
 
B.Sathish
Ranch Hand
Posts: 372
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gouri Bargi,
I feel this a little confusing. Don't you think getAttribute("javax.servlet.forward.query_string") should return the overriden query string as specified in the Request Dispatcher. (Logically speaking, it should as it has the word "forward" in it) and that request.getQueryString() should return the original query string? Do you think the spec developers reversed it?
 
Gouri Bargi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Satish,

I also thought so. 'javax.servlet.forward.query_string' seems to stand for "query string in the forward call".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic