aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Redirection" Watch "Servlet Redirection" New topic
Author

Servlet Redirection

vasant
Greenhorn

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 :
<html>
<head>
<title>
</title>
</head>
<body>
<form method=POST action="http://10.2.3.101:8080/servlet/Register">
login name:<input type="text" size=20 name="lgname">
<br>
<input type="submit" value="ok">
</form>
</body>
</html>
server 10.2.3.101 has the following code: Register.java :
--------------------------------------------------------
import java.io.*;
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 {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
res.setStatus(res.SC_MOVED_TEMPORARILY);
res.setHeader("Location","http://202.41.11.100:8080/servlet/Register");
}
}

server 202.41.11.100 has the following code: Register.java :
-----------------------------------------------------------
import java.io.*;
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
res.setContentType("text/html");
// 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)
session.getValue("register.counter");
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>
times.<p>");
out.println("<p>");
out.println("</BODY>");
out.println("</html>");
} else {
out.println("not authorised user");
out.println("</html>");
}
out.close();
}
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
Greenhorn

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

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 frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
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
vasant
Greenhorn

Joined: Apr 21, 2000
Posts: 2
Hi,
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.
Vasan
[This message has been edited by vasant (edited May 11, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Redirection
 
Similar Threads
Unusual Problem
How HttpSessionActivationListener works?
Problems with getRemoteUser()
Progress bar on IE6
How to use body background in a HTML page thru servlet