• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher

 
L. Wei
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One question of RequestDispatcher in mock,

Which statements about RequestDispatcher are true(where applicable, assume the RequestDispatcher was not obtained via a call to getNamedDispatcher())?

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

Why D is incorrect in the answer? I did a test by forwarding request and response to a RequestDispatcher servlet. In the servlet, I used request.getQueryString(). I got the right QueryString. Why the answer says D is incorrect because "this method returns the query string on the URL pattern from the RequestDispatcher"?

Please clarify it for me. What is the URL pattern from the RequestDispatcher? Isn't it as same as the request.getQueryString()?

[ July 26, 2008: Message edited by: David L. Wei ]
[ July 26, 2008: Message edited by: David L. Wei ]
 
L. Wei
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did 2 tests. The query string after the html form is submitted is "...?test=1"

1st test, I wrote

I got "Query String: test=1".

2nd test, I wrote

I got "Query String: screwit=123".

Does screwit=123 the URL pattern from the RequestDispatcher? Is it the reason for this mock question? Why the 1st test returns "Query String: test=1" instead of "Query String: "? There is no query string while forwarding request/response to "view.jsp"...
[ July 26, 2008: Message edited by: David L. Wei ]
 
Antonio Tercero
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The original query string is retreived by calling
request.getAttribute("javax.servlet.forward.query_string")
 
L. Wei
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I think I got it.
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

When I use request.getAttribute("javax.servlet.forward.query_string") in your example in view.jsp its returning null. Can you tell me the difference between getQueryString() and getAttribute("javax.servlet.forward.query_string").

Thanks
Srividhya
[ July 28, 2008: Message edited by: Srividhya Kiran ]
 
Milind Patil
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say for instance your browser request URL is like this,
http://localhostmyserver.com/mywebapp/testservlet?myQueryString=123456

and in TestServlet, you have written this,


Then in view.jsp, you can get these two query string like this,
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Milind

Thanks for your reply. Does the behaviour is different if we use <jsp:forward> standard action instead of RequestDispatcher.

MyServlet.java:
RequestDispatcher view=request.getRequestDispatcher("result.jsp?name=sri");view.forward(request, response);

result.jsp:
<jsp:forward page="view.jsp"/> didnt pass any query string to view.jsp

view.jsp:
<%
out.println(request.getQueryString()); //line 1
out.println(request.getAttribute("javax.servlet.forward.query_string")); //line 2
%>
then in view.jsp getQueryString() should return null right? Instead I get

line 1 --> name=sri
line 2 --> null

Thanks
Srividhya
[ July 28, 2008: Message edited by: Srividhya Kiran ]
 
Milind Patil
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srividhya,

"javax.servlet.forward.query_string" means the original query string you get from client/browser and in subsequent forwards it is like a constant. It means it doesn't change even if you forward the same request to many jsps/servlets.

While request.getQueryString() is a query string which is appended by the the forwarder. The forwarder could be either RequestDispatcher or <jsp:forward> tag and in subsequent forwards, it is replaced by forwarded QueryString if present any.

In your case, the output is legal because original query string, you might have typed in the browser might be empty/null. So "javax.servlet.forward.query_string" will always return null.

But in your servlet, since you are using query string as "...?name=sri". The
forwarded QueryString became "name=sri" so whenever you will access it usign request.getQueryString(), you will get "name=sri", provided that you haven;t changed it in subsequent forwards.

My explanation is bit confusing but thats true. Just think on it and you will get what is exactly happening during forwards..

[ July 28, 2008: Message edited by: Milind Patil ]

[ July 28, 2008: Message edited by: Milind Patil ]
[ July 28, 2008: Message edited by: Milind Patil ]
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Milind thanks for your detailed explanation. I got it. Is the behaviour same for RequestDispatcher include() and <jsp:include>?

I read that javax.servlet.include.query_string returns the query string given in the included jsp call i.e.,

result.jsp:
<jsp:include page="view.jsp?age=10"/>

view.jsp:
out.print("javax.servlet.include.query_string")-->age=10 and what does
request.getQueryString() will return?

Correct me If I am wrong.

Thanks
Srividhya
 
Milind Patil
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same is true for include() call as well.
See Servlet specification, following points,
1. SRV.8.3.1 - Included Request Parameters
2. SRV.8.4.2 - Forwarded Request Parameters

Link for Servlet JSR: Servlet 2.4 Specification
 
Amruth Puppala
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Milind Patil,

You mean
for jsp forward: javax.servlet.forward.query_string
for jsp include: javax.servlet.include.query_string

both also return same value i.e original query string / parameter list.

I'm I right?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic