Two Laptop Bag*
The moose likes Servlets and the fly likes send and receiving string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "send and receiving string" Watch "send and receiving string" New topic
Author

send and receiving string

Manu Quartier
Greenhorn

Joined: Oct 20, 2000
Posts: 10
Hi,
I'm send a string to a servlet. After a while (usually the third time) the servlet don't receive the data I send and the application waits for datas from servlet. My application server is Allaire JRUN 3.0
Here is my code. What is wrong ?
------SERVLET-------
public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
try{

log.add("new service");

StringBuffer msgBuf = new StringBuffer();

log.add("req.getReader");
BufferedReader fromApplet = req.getReader();

String line;

log.add("reading data");
while ((line=fromApplet.readLine())!=null) {
if (msgBuf.length()>0) msgBuf.append('\n');
msgBuf.append(line);
}

log.add("sending data to app");
resp.setContentType("text/plain");
PrintWriter toApplet = resp.getWriter();
toApplet.println(msgBuf.toString());
toApplet.println("--------------");

log.add("close");
toApplet.close();
fromApplet.close();

log.add("Serv OK");
}
catch(Exception e)
{
log.add(e.getMessage());
}
}
-------application-------
private void App_click(Object source, Event e)
{
try{
log.add("New click");
log.add("setup and connection");
URL url = new URL("http://mqu/biblio/servlet/Servlet");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
log.add("send data to servlet");
PrintStream out = new PrintStream(connection.getOutputStream());
out.println("string");
out.flush();
out.close();
log.add("Receive from servlet");
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));

String inputLine;

log.add("Extracting data");
StringBuffer msgBuf = new StringBuffer();
while ((inputLine=in.readLine())!=null) {
if (msgBuf.length()>0) msgBuf.append('\n');
msgBuf.append(inputLine);
}

in.close();


txtXML2.setText(msgBuf.toString());
log.add("app OK");
}
catch( MalformedURLException err) {
err.printStackTrace();
txtXML2.setText(err.toString());
}
catch( IOException err ) {
txtXML2.setText("app error");
err.printStackTrace();
log.add(err.toString());
}

}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: send and receiving string
 
Similar Threads
How to run a java class which executes a command from JSP
java.io.IOException: Premature EOF
HttpConnection error only in devices (not emulators)
Passing cookies back and forth
how to pass parameters from a serlet to an applet