Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I redirect the browser post to a URL from within my servlet?  RSS feed

 
Ray Smilgius
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks in Advance
Ray Smilgius
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As in response.sendRedirect()? Or do you mean forward? That's done using a RequestDispatcher: getServletContext().getRequestDispatcher(path).forward(request, response).
- Peter

 
Ray Smilgius
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually what I need to do is make a post from a browser to a servlet then I need to foward that post to a simple echo web server. I have used URL but when I post to the server it is showing two post with the same time stamps.
here is the code:
//////////////////////////////////////////////////////////////
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.net.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TestServlet extends HttpServlet {
String name ="ray myself";
String str = "";
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<pwCmdXML>" +
"<Command>SubmitJob</Command>" +
"<CommandPassword>aPwd</CommandPassword>" +
"<ProjectName>TheProject</ProjectName>" +
"<ProjectType>pType</ProjectType>" +
"<ObjectClass>stuff</ObjectClass>" +
"<ObjectList>1,2,3,4,5,6,7,8,9,10,11,12,13,14,15," +
"16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31," +
"32,33,34,35,36</ObjectList>" +
"</pwCmdXML>";
private String s=URLEncoder.encode("A Test string to send to a servlet");
public void init() throws ServletException {
System.out.println("TestServlet : init");
}
public void destroy() {
System.out.println("TestServlet : destroy");
}

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
// print content

out.println("<html><head>");
out.println("<title>TestServlet ( by ");
out.println( name );
out.println(" )</title>");
out.println("<style>body, p { font-family:tahoma;");
out.println(" font-size:12pt; }</style>");
out.println("</head>");
out.println("<body>");
out.println("<p>TestServlet (");
out.println(str);
out.println(" ) :</p>");
out.println("</body></html>");
out.close();
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
res.setContentType("text/html");
///////////////////////////////////////////////////////////
try
{
// URL u = new URL("http://rsmilgius:5000/sht.xml");
//////////////////////////////////
URL url;
URLConnection urlConn;
DataOutputStream printout;
DataInputStream input;
// URL of CGI-Bin script.
url = new URL ("http://rsmilgius:5000/InScope.xml");
// URL connection channel.
urlConn = url.openConnection();
// Let the run-time system (RTS) know that we want input.
urlConn.setDoInput (true);
// Let the RTS know that we want to do output.
urlConn.setDoOutput (true);
// No caching, we want the real thing.
urlConn.setUseCaches (false);
// Specify the content type.
urlConn.setRequestProperty
("Content-Type", "application/x-www-form-urlencoded");
// Send POST output.
printout = new DataOutputStream (urlConn.getOutputStream ());
printout.writeBytes(xml);
printout.flush ();
// printout.close ();
// Get response data.
input = new DataInputStream (urlConn.getInputStream ());
while (null != ((str = input.readLine())))
{
System.out.println (str);
//textArea.appendText (str + "\n");
}
doGet(req, res);
}
catch (IOException e)
{
// e.printStackTrace(); // should do real exception handling
System.out.println(e.getMessage());
doGet(req, res);
}

}
}
/////////////////////////////////////////////////////////////////
Thanks in Advance
Please note on my servlet getMessage() displays Unexpected end of file from server...Also 2 posts are appearing in my server logs on the echo server end.....
browser(post)---->servlet---foward post----->echo server(echo back post to browser...

Thanks Ray Smilgius
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!