aspose file tools*
The moose likes Servlets and the fly likes Strange problem - request forwarding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Strange problem - request forwarding" Watch "Strange problem - request forwarding" New topic
Author

Strange problem - request forwarding

Milind Mahajan
Ranch Hand

Joined: Oct 23, 2000
Posts: 77
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

Joined: Jan 08, 2001
Posts: 6
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

Joined: Oct 23, 2000
Posts: 77
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

Joined: Jul 24, 2000
Posts: 45
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

Joined: Jun 03, 2000
Posts: 5040

Quite strange, since reading the RequestDispatcher API
for the forward() method, doesn't say anything. Textbooks always help, don't they?
Regds.
- satya

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange problem - request forwarding