• 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

help with posting data to server from servlet premature end of file error

 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 test = "This is a test";
byte[] b = new byte[100];
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( test);
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(b);// print contents of response from remote server
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 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", "text/html");
// Send POST output.
printout = new DataOutputStream (urlConn.getOutputStream ());
printout.writeBytes(test);
printout.flush ();
// printout.close ();// commented this out to make sure i get all file
// Get response data. //*******this is where it is bombing****
input = new DataInputStream (urlConn.getInputStream ());
while( input.read(b) > 0) {}
printout.write(b); //send the original content back
System.out.println (b);
doGet(req, res);
}
catch (IOException e)
{
System.out.println(e.getMessage());
doGet(req, res);
}

}
}
Thanks in advance
When I post to the server no data is getting there from the post I even tested it on a simple asp page that returns the results sent..Niether the ASP nor the echo web server is recieving the posted data. Funny enough the echo web server is getting the correct content-length of the data but not the data itself. I have been struggling on this for a week. Any help will be greatly appreciated.
Ray Smilgius
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic