File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Servlet Redirection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Redirection" Watch "Servlet Redirection" New topic

Servlet Redirection


Joined: Apr 21, 2000
Posts: 2
I'm trying to redirect the request from one server to other. Here one HTML page is getting the user name and using POST method to process that request. The corresponding server is getting the request and create the session and throws the html page
"You have hit this page 1 times."
The request is going to one server, that server is redirecting that request to another server. The redirection is working for GET method. But it is not working for POST method. The error is "Bad request and Servlet not supporting get method". How do I send(redirect) a request with parameters or how do i handle the post method?.
The code :
HTML code :
<form method=POST action="">
login name:<input type="text" size=20 name="lgname">
<input type="submit" value="ok">
server has the following code: :
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Register extends HttpServlet {
public void doPost(HttpServletRequest req,HttpServletResponse res)
throws IOException, ServletException {
PrintWriter out = res.getWriter();

server has the following code: :
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;

public class Register extends HttpServlet {
public void doPost (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
//Get the session object
HttpSession session = req.getSession(true);
// set content type and other response header fields first
// then write the data of the response
PrintWriter out = res.getWriter();
out.println("<HEAD><TITLE> " + "Registration Output " + "</TITLE></HEAD><BODY>");
String loginname = req.getParameter("lgname");
if(loginname.equals("vasan")) {
Integer ival = (Integer)
if (ival==null) ival = new Integer(1);
else ival = new Integer(ival.intValue() + 1);
session.putValue("register.counter", ival);
out.println("You have hit this page <b>" + ival + "</b>
} else {
out.println("not authorised user");
public String getServletInfo() {
return "A simple servlet";
[This message has been edited by vasant (edited April 21, 2000).]
[This message has been edited by vasant (edited April 21, 2000).]
Praveen Raja

Joined: May 01, 2000
Posts: 13
y not try res.sendRedirect(String url)
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
The problem is that if you ask the browser to do the redirection, it will always use "GET". Remember that the HTTP system was designed around the idea of retrieving static pages, and fanciness like "POST" was added later.
You have two possible solutions.
The first is to do the 'redirection' manually - open a socket to the server and build a POST request from your servlet data, then read the reply and send it to the servlet output.
This is the most flexible approach, but it does involve extra programming, and ties up one thread of both the servers. Client redirection is a better choice if the system needs to scale to very high loads.
The second solution is to use a "GET" request, but pass the POST data you have received as query-string parameters on the GET. Build the URL to redirect to in a StringBuffer starting with the URL of the servlet, add a '?' and the in iterate through the POST parameters URLencoding them and adding them separated by '&'.
If you only have a small amount of data this is a good solution. The normal problem of the parameters being visible on the browser URL line doesn't apply, as it is a server-to-server communication.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Tony Alicea

Joined: Jan 30, 2000
Posts: 3226
Frank, correct me if I'm wrong, but doesn't the jakarta-tomcat 3.1 server re-direct POST too? I know that until now I've gotten a "not supported" type of message when trying to re-direct POST but not with GET with Servlet 2.1. In fact the JSWDK does just that and it supports only Servlet 2.1
Servlets 2.2 can forward a POST. At least Tomcat 3.1 can do it. I was asked to check at work because we'd prefer to not include request data as part of a URL.

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD

Joined: Apr 21, 2000
Posts: 2
Thanks for your replies.
I found some solution for my question......
requestDispatcher method satisfies my demand.
This method is available in java webserver 2.0.
[This message has been edited by vasant (edited May 11, 2000).]
Don't get me started about those stupid light bulbs.
subject: Servlet Redirection
It's not a secret anymore!