Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange problem - request forwarding

 
Milind Mahajan
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.
 
madhu babu
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
 
Katie McCann
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.
 
Madhav Lakkapragada
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic