aspose file tools*
The moose likes Servlets and the fly likes Trying to post from servlet directly to server see code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Trying to post from servlet directly to server see code" Watch "Trying to post from servlet directly to server see code" New topic
Author

Trying to post from servlet directly to server see code

Ray Smilgius
Ranch Hand

Joined: Jan 29, 2001
Posts: 120
I had posted previously, and Bill gave me great advice. I am trying to conduct a post from the browser to a servlet and direct a post from the servlet to the remote IPWorks echo server example.
browser--post-->servlet--post-->IPWorks echo server...
I am getting an exception with getMessage: Unexpected end of file
from server this is my exception in the servlet I am catching. Although when I do a post from the browser directly to the server bypassing the servlet it returns the contents of the post back to the browser without any problems...
Sometimes it works but only some of the string is returned to the servlet....I have also kept the connection opened and also didnt flush the stream just in case...
heres 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 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(xml);
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");

//////////////////////////////////
URLurl;
URLConnectionurlConn;
DataOutputStreamprintout;
DataInputStreaminput;

// 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 ());

String content = "name=" + URLEncoder.encode ("Buford Early") +
"&email=" + URLEncoder.encode ("buford@known-space.com");

printout.writeBytes(xml);
printout.flush ();
// printout.close ();

// Get response data.
input = new DataInputStream (urlConn.getInputStream ());

String str;
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
Ray Smilgius
My email is softwaredeveloper@excite.com


SCJO, SCJD, SCWCD, I-Net+, A+, Network+, MCSD, MCDBA, MCP, MCT
Ray Smilgius
Ranch Hand

Joined: Jan 29, 2001
Posts: 120
Also my echo servers log is showing 2 posts with the same time stamp as when I do it with the broswer directly to the echo server it only logs one post and returns the contents to the browser without no problems..
Thanks Again
Ray Smilgius
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trying to post from servlet directly to server see code