wood burning stoves 2.0*
The moose likes Servlets and the fly likes RequestDispatcher - Need Help!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "RequestDispatcher - Need Help!!!" Watch "RequestDispatcher - Need Help!!!" New topic
Author

RequestDispatcher - Need Help!!!

Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi,
I am trying to call one servlet from another using RequestDispatcher.forward().But it is not able to transfer the control to another servlet.
Codes for your reference:
RequestDispatcherFirst.java

RequestDispatcherSecond.java

Why is it not able to call RequestDispatcherSecond servlet?
If I try using response.sendRedirect(), it works!!
BTW I am using JDeveloper 3.1.2, and Web-to-Server for hosting the servlets.
Regards,

------------------
Sandeep Desai
vgdesai@vsnl.com

  1. Sun Certified Java Programmer Scored 93 per cent
  2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
  3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
  4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56


<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Sandeep,
I think it is "/servlet/RequestDispatcherSecond" NOT "/servlets/RequestDispatcherSecond". Please note the 's' in "servlets/....."
regds
maha anna
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi,
Thanks for your response.
As I had mentioned, I am using JDeveloper to test the servlets.The URL is http://localhost:7070/servlets/servlet, i.e. the path is servlets/servlet and NOT servlet/servlet
Also, while I am using response.sendRedirect(), servlets/servlet, is working fine.
Is this problem typical to Jdeveloper, or there is something wrong with my code.
Regards,

------------------
Sandeep Desai
vgdesai@vsnl.com

  1. Sun Certified Java Programmer Scored 93 per cent
  2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
  3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
  4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Sandeep,
Sorry about that. I didn't know it is 'servlets/servlet/ in JDeveloper.
Try urlrewriting using 'response.encodeURL(address)' like following and check.

Related discussion at http://www.javaranch.com/ubb/Forum7/HTML/000666.html
regds
maha anna
nirmal baba
Greenhorn

Joined: Apr 06, 2001
Posts: 10
Hello,
....I dont know whether this piece of information will help u.....RequestDispatcher object can be obtained using the getRequestDispatcher(String path) methods of ServletContext and
ServletRequest...But one important difference has to be kept in mind...The method of ServletContext accepts the ABSOLUTE PATH(ie u have to specify path relative to the root context) as the argument while the method of ServletRequest can accept ABSOLUTE/RELATIVE path.
.........I have never worked on JBuilder and dont know the directory structure used there.Kindly check that u are using the correct path as the argument(relative to the root context in ur case since u r using the method of ServletContext).
.....Also ,if possible kindly keep me informed about the exact cause of the problem when it gets solved!!!
All The Best!!
Nirmal
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi all,
I am through with RequestDispatcher problem.Furnishing the details of some interesting facts that I had observed while playing around with response.sendRedirect(String url) and getServletContext().getRequestDispatcher(String url).forward(request,response)
Using response.sendRedirect(String url)
The API doc says :

This method can accept relative URLs; the servlet container will convert the relative URL to an absolute URL before sending the response to the client.

To explain this with an example :
Consider 2 servlets, viz. RedirectServletFirst and RedirectServletSecond, and we want to redirect the request from the RedirectServletFirst to RedirectServletSecond
This is the way to do it :
RedirectServletFirst


RedirectServletSecond

As per the above quote from the API doc, the sendRedirect() argument could be a relative path as the above example, which gets converted to the absolute url, i.e. http://localhost:7070/servlets/RedirectServletSecond?name=sandeep, in my case.
Alternatively, you could paste the absolute URL as the sendRedirect() argument.
Valid URL's for this case :


Invalid URL's for this case

This would resolve to http://localhost:7070/RedirectServletSecond?name=sandeep, which is invalid

This would resolve to http://localhost:7070/servlets/servlets/RedirectServletSecond?name=sandeep, which is again invalid.

Using getServletContext().getRequestDispatcher(String url).forward(request,response)

The API doc says :

The pathname must begin with a "/" and is interpreted as relative to the current context root.

The above quote says it all.
Consider 2 servlets, viz. RedirectServletFirst and RedirectServletSecond, and we want to redirect the request from the RedirectServletFirst to RedirectServletSecond
This is the way to do it :
RequestDispatcherFirst

As you might have guessed it correctly, the URL is interpreted as http://localhost:7070/servlets/RequestDispatcherSecond?name=sandeep
RequestDispatcherSecond

Unfortunately, since JDeveloper 3.1.2 supports JSDK Version 2.0, I couldnot experiment with request.getRequestDispatcher(String) and servletContext.getNamedDispatcher(String)
I would request the group to post their findings here, if they have used these new methods of Servlet Version 2.0+.
Hope you find the above description useful.
Regards,
------------------
Sandeep Desai
vgdesai@vsnl.com

  1. Sun Certified Java Programmer Scored 93 per cent
  2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
  3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
  4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi,
I think, I missed out on something critical as regards to servletContext.getRequestDispatcher(String urlPath).forward(request,response)
Note that, as per the API doc, the path is interpreted relative to the current context root.
So if your first servlet is located by a URL http://localhost:7070/servlets/RequestDispatcherFirst, then doing a forward() to the RequestDispatcherSecond, would have ONLY ONE VALID WAY to code the urlPath
It is shown in the following code snipplet

This is because the current context is http://localhost:7070/servlets.With the above urlPath, the forward() would be to the URL http://localhost:7070/servlets/RequestDispatcherSecond?name=sandeep", which is OK

On the other hand, the following code snipplet, would be invalid

This is because the forward() would be for the URL http://localhost:7070/servlets/servlets/RequestDispatcherSecond?name=sandeep", which is an invalid URL.
So to sum up the whole thing :
1. response.sendRedirect(), would allow urlPath as relative and absolute paths.The relative paths are converted to absolute paths before sending the response to the client
2. response.getRequestDispatcher() must have the urlPath,beginning with "/", and it is interpreted relative to the current context root.
That is it from me.Hope you would find this information useful.
Regards,

------------------
Sandeep Desai
vgdesai@vsnl.com

  1. Sun Certified Java Programmer Scored 93 per cent
  2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
  3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
  4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RequestDispatcher - Need Help!!!