This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher - Need Help!!!

 
Desai Sandeep
Ranch Hand
Posts: 1157
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
maha anna
Ranch Hand
Posts: 1467
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandeep,
I think it is "/servlet/RequestDispatcherSecond" NOT "/servlets/RequestDispatcherSecond". Please note the 's' in "servlets/....."
regds
maha anna
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1467
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1157
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1157
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic