• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet Redirection

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
y not try res.sendRedirect(String url)
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
vasant
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic