• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Strange problem - request forwarding  RSS feed

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a problem in request forwarding which I find strange.
I am forwarding request to a HTML page.When I request a servlet from browser directly (which invokes doGet(..) method of the servlet) the HTML page to which I am forwarding the request is served to browser.
But when I created a form which invokes doPost(..) method of the same servlet, the code(same code which works in doGet(..))to forward request to HTML page is not working. Then I tried to call doGet(..) from doPost(..) so that the code which works when I directly call servlet from browser is invoked. Again, it is not working. The code is as follows :
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class TrialServlet extends HttpServlet {

//invoked when this servlet is directly launched from browser.

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
{
RequestDispatcher rd = getServletContext().getRequestDispatcher
("/Thanks.html");
rd.forward(req,res);

}
//invoked when I submit a form to this servlet.
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
{
doGet(req,res);
}
}
Thanks.html is in server root directory and servlet is in servlets directory of server root.

I wish somebody helps me. This is important for the project I am doing. I am posting the code below. I hope I have been able to explain the problem clearly.
Thanks a lot.

Milind.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai,
doPost() mehtod is used for sending data from server to client.
what ever method GET or POST it is handled by doGet() method only
executed.
see small example:/* servlet****
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
public class post extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
{
PrintWriter out=res.getWriter();
out.println("this is madhu");
}
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
{
doPost(req,res);
}
}
*/
*****html page***
<html>
<body>
<form method=post action="servlet/post">
<input type=submit value=submit>
</form>
</body>
</html>

 
Milind Mahajan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for the response Madhu. I agree with you. But my doubt about request forwarding in doGet(..) and doPost(..) persists. Can somebody help me please?
Thanks ,
Milind
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rename the .html file you are forwarding to to "Thanks.jsp". I think this will solve your problem. Post requests can't be forwarded to plain HTML pages, and the solution is to rename it with a .jsp extension. If you own the "Core Servlets..." book see page 355 for an explanation.
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Quite strange, since reading the RequestDispatcher API
for the forward() method, doesn't say anything. Textbooks always help, don't they?
Regds.
- satya
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!