• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question regarding RequestDispatcher forward (not in HFSJ)

 
Ranch Hand
Posts: 372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

https://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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
reply
    Bookmark Topic Watch Topic
  • New Topic