File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Strange problem - request forwarding" Watch "Strange problem - request forwarding" New topic

Strange problem - request forwarding

Milind Mahajan
Ranch Hand

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

//invoked when I submit a form to this servlet.
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
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.

madhu babu

Joined: Jan 08, 2001
Posts: 6
doPost() mehtod is used for sending data from server to client.
what ever method GET or POST it is handled by doGet() method only
see small example:/* servlet****
import javax.servlet.*;
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
*****html page***
<form method=post action="servlet/post">
<input type=submit value=submit>

Milind Mahajan
Ranch Hand

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

Take a Minute, Donate an Hour, Change a Life
I agree. Here's the link:
subject: Strange problem - request forwarding
It's not a secret anymore!