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").
And i think that is the same to read these attributes or to use the corrispondent methods of HttpServletRequest.
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.
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.
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.
posted 13 years ago
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?
posted 13 years ago
I also thought so. 'javax.servlet.forward.query_string' seems to stand for "query string in the forward call".