This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes doubt in forwarding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "doubt in forwarding " Watch "doubt in forwarding " New topic
Author

doubt in forwarding

raja ram
Ranch Hand

Joined: Mar 02, 2008
Posts: 169
Hi,

I have a jsp file with one text field and two servlets. when i click submit button on the jsp file it goes to first servlet which will forward the request to second servlet.

my doubt is as below.

after submitting the jsp page in my first servlet when i call
req.getRequestURL() and req.getQueryString() it displays correct information

but when i forward the control to second servlet from first servlet and when print the value of req.getRequestURL() and req.getQueryString() it displays same information that was displyed for first servlet.

1)my doubt is for first servlet if it displays http://localhost/scwcd/test as the output of req.getRequestURL() it should display http://localhost/scwcd/processparam for second servlet because in my second servlet i am getting the req.getRequestDispatcher("processParam"); and forwarding so now the container will understand that it should build the URL some thing like http://localhost/scwcd/processparam this and display the servlet. As per this it should display http://localhost/scwcd/processparam as out put for
req.getRequestURL() but it displays the http://localhost/scwcd/test as the request url which was built by container when jsp called my first servlet test how can i get http://localhost/scwcd/processparam as the out put for requesturl methode in my second servlet?

2)in chapter 5 Q12 of HFSJ it is said that we can access the original query string when the request gets forwarded to other servlet and for this we should use req.getAttribute("Javax.servlet.forward.query_String"); and we cant use req.getQueryString() to get original Query String.

But when i say req.getQueryString() i am getting same QueryString as that of in first servlet and when i say req.getAttribute("Javax.servlet.forward.query_String"); it displays null what is actually happening here am i making any mistakes.

3)and at last in my web.xml the mapping i have given is as <url-pattern>/test/*</url-pattern> when the url is http://localhost:8080/test it displays corect out put but when i make the request using this url http://localhost:8080/test/1/2 it throws internal server error. but after /test/ we have "*" that means it should display servlet test for /test/,/test/1 or /test/1/2/3 with out any problem am i correct but i am getting errors. is my understanding wrong ?

Thanks
[ April 06, 2008: Message edited by: raja ram ]
Nitin Vashishtha
Ranch Hand

Joined: Mar 26, 2008
Posts: 31
Answer 1:
Because you are using req.getRequestDispatcher(),its the same request which you have internally asked to some othe resoirce to server,the browser is not aware of it at all.What you are expecting will happen if you use response.sendRedirect(),in that case the browser will get a 301 code with a new url location ,and then it will redirect the client to the new location and you will get http://localhost/scwcd/processparam as the out put for requesturl methode in second servlet.

Answer 2:
No,3 answers are correct for this question in HFSJ,which includes req.getQueryString() as a correct option.

Answer 3:What error is it throwing?It should be 404 only if it does not find the resource you are requesting.


Nitin
SCJP 91%, TOFAG 9 certified 89%
raja ram
Ranch Hand

Joined: Mar 02, 2008
Posts: 169
Hi,

Thanks for the reply.

1.i understood the first answer explanation.
2.for second question req.getAttribute("Javax.servlet.forward.query_String"); displays null what could be the reason.
3.for third question it throws stack over flow excetion and not 404 error what changes i should do so that it takes /test/1/2 also as correct URL pattern.

Thanks
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

2) The parameter must be lowercase : javax.servlet.forward.query_string

3) It's difficult to say without seeing what you do in the mapped servlet. Maybe you are forwarding to a url which will go back to the original servlet, and it will loop infinitely. That's why you should avoid using "*" for your mappings.


[My Blog]
All roads lead to JavaRanch
raja ram
Ranch Hand

Joined: Mar 02, 2008
Posts: 169
Hi,

I have used lower case string but while typing here i made mistakes any how i am putting my code below.

in web.xml i have this code.

<servlet>
<servlet-name>contextparam</servlet-name>
<servlet-class>com.scwcd.test</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet>
<servlet-name>processparam</servlet-name>
<servlet-class>com.scwcd.testparam </servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>contextparam</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>processparam</servlet-name>
<url-pattern>/processparam/*</url-pattern>
</servlet-mapping>

and in my first servlet

RequestDispatcher rds=request.getRequestDispatcher("/processparam");
rds.forward(request,response);
and in my second servlet

out.println("After forwardingvalue of req parameter " + request.getAttribute("javax.servlet.forward.query_string") ); --out put is null
out.println("After forwarding value of req parameter " + request.getQueryString() ); -- this work fine

I think this will clear picture about what i am doing.

Thanks
[ April 08, 2008: Message edited by: raja ram ]
raja ram
Ranch Hand

Joined: Mar 02, 2008
Posts: 169
Hi,

What is the problem with this line
out.println("After forwardingvalue of req parameter " + request.getAttribute("javax.servlet.forward.query_string") ); --out put is null

it is printing null i am forwarding and not include it should give me original Query Parameters.

Thanks
Nitin Vashishtha
Ranch Hand

Joined: Mar 26, 2008
Posts: 31
Originally posted by raja ram:
out.println("After forwardingvalue of req parameter " + request.getAttribute("javax.servlet.forward.query_string") ); --out put is null

Thanks


what happens if you put req.getQueryString() in place of request.getAttribute("javax.servlet.forward.query_string") ?
Nitin Vashishtha
Ranch Hand

Joined: Mar 26, 2008
Posts: 31
Changes were made to HttpServletRequest.getQueryString() to be in compliance with the J2EE Servlet Specification, sections SRV.8.4 and SRV.8.4.1:


The HttpServletRequest.getQueryString is one of the path elements used to acquire a RequestDispatcher; therefore, this value must be updated to reflect what was used to obtain the RequestDispatcher when a forward keyword is used. If there is no query string when obtaining the requestDispatcher, getQueryString() returns null.

Previous to these changes, the original query string from a client passed throughout the request even after it was forwarded.
raja ram
Ranch Hand

Joined: Mar 02, 2008
Posts: 169
what happens if you put req.getQueryString() in place of request.getAttribute("javax.servlet.forward.query_string") ?


it prints the Original Query string but with getAttribute() it prints null.

Thanks
 
GeeCON Prague 2014
 
subject: doubt in forwarding