• 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Trying to post from servlet directly to server see code

Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>" +
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 {


PrintWriter out = res.getWriter();

// print content

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("<p>TestServlet (");
out.println(" ) :</p>");


public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {

// URL u = new URL("http://rsmilgius:5000/sht.xml");


// 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.
("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.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
doGet(req, res);

Thanks in Advance
Ray Smilgius
My email is softwaredeveloper@excite.com
Ray Smilgius
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic