File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java Micro Edition and the fly likes servlet response Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "servlet response" Watch "servlet response" New topic
Author

servlet response

Praveen Raja
Greenhorn

Joined: May 01, 2000
Posts: 13
Hi,
I'm trying to get a client-server architecture running. But when i post a request(from a MIDlet) to a servlet, I sometimes get a null response (even though there has to be a valid response). I have tried it with both tomcat 3.2 (i think) and jsdwk 1.0.1,
the only difference being that the frequency of the error is lesser with jsdwk. Is anyone else having the same trouble?? Or does anyone know why and/or the solution to my problem??
Eric Giguere
Ranch Hand

Joined: Oct 25, 2000
Posts: 170
What do you mean by null response? Post your code (both client & server) and we can dissect it.

------------------
Eric Giguere
author of:
Java 2 Micro Edition : A Professional Developer's Guide
PalmTM Database Programming: The Complete Developer's Guide


Eric Giguere
Author/Co-Author of: Mobile Information Device Profile for Java 2 Micro Edition: Professional Developer's Guide, Java 2 Micro Edition: Professional Developer's Guide and PalmTM Database Programming: The Complete Developer's Guide
Praveen Raja
Greenhorn

Joined: May 01, 2000
Posts: 13
The client sends in a request string. The server reads the request and sends a response based on the request string. Im pasting part of the client and server code. The client code is the method that sends the request to the server(some of the variables may not be defined in this method since they are class variables). The server code is the doPost method of the servlet.

CLIENT
private void doSend(String request,
HttpPosterListener listener)
{
HttpConnection conn = null;
InputStream in = null;
OutputStream out = null;
String responseStr = null;
String errorStr = null;
boolean wasError = false;
try
{
conn = (HttpConnection)Connector.open(url);
}
catch (IOException e)
{
wasError = true;
errorStr = "1."+e.getMessage();
}
try
{
// Set the request method and headers
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty("User-Agent",
"Profile/MIDP-1.0 Configuration/CLDC-1.0 ReverseClient");
conn.setRequestProperty("Content-Language", "en-US");
}
catch (IOException e)
{
wasError = true;
errorStr = "1."+e.getMessage();
}
try
{
// Getting the output stream may flush the headers
out = conn.openOutputStream();
int requestLength = request.length();
for (int i = 0; i < requestLength; ++i)<br /> {<br /> out.write(request.charAt(i));<br /> }<br /> }<br /> catch (IOException e)<br /> {<br /> wasError = true;<br /> errorStr = "2."+e.getMessage();<br /> } <br /> // Opening the InputStream will open the connection<br /> // and read the HTTP headers. They are stored until<br /> // requested.<br /> try<br /> {<br /> in = conn.openInputStream();<br /> if (in == null)<br /> System.out.println("NULL INPUT STREAM");<br /> }<br /> catch (Exception e)<br /> {<br /> wasError = true;<br /> errorStr = "3a."+e.getMessage();<br /> }<br /> try<br /> {<br /> // Get the length and process the data<br /> StringBuffer responseBuf;<br /> long length = conn.getLength();<br /> if (length > 0)
{
responseBuf = new StringBuffer(/*(int)length*/);
}
else
{
responseBuf = new StringBuffer(); // default length
}
int ch;
while ((ch = in.read()) != -1) // THIS IS WHERE THE PROBLEM IS I THINK
{
responseBuf.append((char)ch);
System.out.println("responseBuf="+responseBuf);
}
responseStr = responseBuf.toString();
System.out.println("responseStr="+responseStr);
// support URL rewriting for session handling
String rewrittenUrl = conn.getHeaderField("X-RewrittenURL");
if (rewrittenUrl != null)
{
url = rewrittenUrl; // use this new one in future
}
}
catch (NullPointerException npe) // I CATCH THE EXCEPTION HERE
{
wasError = true;
System.out.println("nulling"+npe.getMessage());
errorStr = "nulling"+npe.getMessage();
}
catch (Exception e)
{
wasError = true;
errorStr = "3b."+e.getMessage();
}
finally
{
if (in != null)
{
try
{
in.close();
}
catch (IOException e)
{
}
}
if (out != null)
{
try
{
out.close();
}
catch (IOException e)
{
}
}
if (conn != null)
{
try
{
conn.close();
}
catch (IOException e)
{
}
}
}
if (wasError)
{
listener.handleHttpError(errorStr);
}
else
{
listener.receiveHttpResponse(responseStr);
}
}

SERVER
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
try
{
// read request
InputStream in = request.getInputStream();
int requestLength = request.getContentLength();
if (requestLength == -1)
{
throw new IOException("Need to know request length");
}
StringBuffer buf = new StringBuffer(requestLength);
for (int i = 0; i < requestLength; ++i)
{
int ch = in.read();
if (ch == -1)
{
break;
}
buf.append((char)ch);
}
in.close();
String requestStr = buf.toString();
// process request, producing response
String responseStr;
if (requestStr.equals("TicketList"))
responseStr = "Praveen's concert\\VPR's concert";
else if (requestStr.equals("TicketTypeList"))
responseStr = "300 SEK\\500 SEK";
else if (requestStr.equals("ConfirmTicket"))
responseStr = "THE TICKET";
else
responseStr = "300 SEK\\500SEK"; // makes sure i always send a response string
// write response
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.write(responseStr);
out.close();
}
catch (IOException e)
{
e.printStackTrace();
throw e;
}
catch (Exception e)
{
e.printStackTrace();
throw new ServletException(e.getMessage());
}
}

Well thats the code. any help will be really appreciated. Thanks
Praveen.
Eric Giguere
Ranch Hand

Joined: Oct 25, 2000
Posts: 170
I suspect your biggest problem is the keep-alive part of HTTP 1.1. Send a "Connection" header with its value set to "close" as part of the request headers. If you don't do this, then you must check the value of the "Content-Length" response header and read EXACTLY the number of bytes it states have been sent. Otherwise your call to read() will block.
Eric
Praveen Raja
Greenhorn

Joined: May 01, 2000
Posts: 13
Thanks for your reply. I tried doing what you said. That is with the request I set the following header conn.setRequestProperty("Connection", "close"); But that didn't seem to help. Do you see any other problem areas? If so it would really help.
Thanks,
Praveen.
Eric Giguere
Ranch Hand

Joined: Oct 25, 2000
Posts: 170
Well, here are the problems I see with this code:
1) You're not setting the Content-Length header. The MIDP will do this for you, but in the Sun version it gets it wrong. Set it yourself.
2) Close the output stream as soon as you're done with it.
3) After closing the input stream, call getResponseCode to actually send everything to the web server. Check the response code that comes back -- if it's not 200 then that's usually an error and there's no point trying to read the input stream.
4) When reading the data, get the length first and read only that many characters. Note that an EOF (-1) will be sent only if the HTTP connection is being kept alive. If it is, then doing a read on the stream will block when you read past the number of bytes that were sent in response.
5) If possible, check up on the web server to see what's happening. Look in the web server logs.

------------------
Eric Giguere
author of:
Java 2 Micro Edition : A Professional Developer's Guide
PalmTM Database Programming: The Complete Developer's Guide
Praveen Raja
Greenhorn

Joined: May 01, 2000
Posts: 13
Thanks for ypur help. I'll check up on these suggestions and see how it goes and will keep you posted in case you are interested. Thanks again.
/Praveen.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet response