MIDP applicatyion accessing a servlet not working in series 60 emulator
HI,
I have developed an application which access a servlet in Tomcar server. The application works in j2me emulator and nokia 40 series emulator but when i try to run ot on a series 60 emulator
i am getting the following error
Nokia Series 60 MIDP Concept SDK Beta 0.2: disconnect!! httpver: HTTP/1.1
Nokia Series 60 MIDP Concept SDK Beta 0.2: disconnect!! HTTP/1.1 !!!!!!!
Please som one help me to solve the problem
Where's the Http connection thread?
Hi,
I don't see you using a separate thread for your HTTP connection in your code. You can find a sample on this forum itself if you are not sure how to do that. In your case the InvokeServet class should extend Thread or implement Runnable interface.
[N]/Forum Nokia
Re: MIDP applicatyion accessing a servlet not working in series 60 emulator
I am newbie trying to develop a mobile application to access a web application through a Servlet but the program hands up middle way. The code for the classes I am using are:-
[B]Sample1.java[/B]
[CODE][COLOR="Blue"]
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class Sample1 extends MIDlet implements CommandListener {
/*
* the default value for the URL string is
* http://www.webyu.com/servlets/webyu/wirelessdevnetsample1
*/
private static String defaultURL =
"http://www.webyu.com/servlets/webyu/wirelessdevnetsample1";
// GUI component for user to enter String
private Display myDisplay = null;
private Form mainScreen;
private TextField requestField;
// GUI component for displaying header information
private Form resultScreen;
private StringItem resultField;
// the "SEND" button used on the mainScreen
Command sendCommand = new Command("SEND", Command.OK, 1);
// the "BACK" button used on the resultScreen
Command backCommand = new Command("BACK", Command.OK, 1);
public Sample1(){
// initializing the GUI components for entering Web URL
myDisplay = Display.getDisplay(this);
mainScreen = new Form("Type in a string:");
requestField =
new TextField(null, "GREAT ARTICLE", 100, TextField.ANY);
mainScreen.append(requestField);
mainScreen.addCommand(sendCommand);
mainScreen.setCommandListener(this);
}
public void startApp() {
myDisplay.setCurrent(mainScreen);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == sendCommand) {
// retrieving the String that user entered
String requeststring = requestField.getString();
// sending a POST request to Web server
String resultstring = sendPostRequest(requeststring);
// displaying the response back from Web server
resultScreen = new Form("Result String:");
resultField = new StringItem(null, resultstring);
resultScreen.append(resultField);
resultScreen.addCommand(backCommand);
resultScreen.setCommandListener(this);
myDisplay.setCurrent(resultScreen);
} else if (c == backCommand) {
// do it all over again
requestField.setString("SOMETHING GOOD");
myDisplay.setCurrent(mainScreen);
}
}
// send a POST request to Web server
public String sendPostRequest(String requeststring) {
HttpConnection hc = null;
DataInputStream dis = null;
DataOutputStream dos = null;
StringBuffer messagebuffer = new StringBuffer();
try {
// Open up a http connection with the Web server
// for both send and receive operations
hc = (HttpConnection)
Connector.open(defaultURL, Connector.READ_WRITE);
// Set the request method to POST
hc.setRequestMethod(HttpConnection.POST);
// Send the string entered by user byte by byte
dos = hc.openDataOutputStream();
byte[] request_body = requeststring.getBytes();
for (int i = 0; i <request_body.length; i++) {
dos.writeByte(request_body[i]);
}
dos.flush();
dos.close();
// Retrieve the response back from the servlet
dis = new DataInputStream(hc.openInputStream());
int ch;
// Check the Content-Length first
long len = hc.getLength();
if(len!=-1) {
for(int i = 0;i<len;i++)
if((ch = dis.read())!= -1)
messagebuffer.append((char)ch);
} else {
// if the content-length is not available
while ((ch = dis.read()) != -1)
messagebuffer.append((char) ch);
}
dis.close();
}
catch (IOException ioe) {
messagebuffer = new StringBuffer("ERROR!");
} finally {
// Free up i/o streams and http connection
try {
if (hc != null) hc.close();
} catch (IOException ignored) {}
try {
if (dis != null) dis.close();
} catch (IOException ignored) {}
try {
if (dos != null) dos.close();
} catch (IOException ignored) {}
}
return messagebuffer.toString();
}
}[/COLOR][/CODE]
[B]EnterpriseServletExample.java[/B]
[CODE][COLOR="Blue"]import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class EnterpriseServletExample extends HttpServlet {
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
System.out.println(
"Accepting data from a J2ME client. IP="+request.getRemoteAddr());
System.out.println("Request Method...:"+request.getMethod());
printParameterInfo(request);
printHeaderInfo(request);
printCookieInfo(request);
processCounter(request);
String data = processDataInput(request);
String responseString =
"Echo \"" + data + "\" sent via " + request.getMethod();
processResponse(response, responseString);
}
private void printParameterInfo(HttpServletRequest request){
System.out.println("Parameter Info:");
Enumeration e = request.getParameterNames();
while (e.hasMoreElements()){
String name = (String)e.nextElement();
System.out.println("Parameter........:"+name+"="+request.getParameter(name));
}
}
private void printHeaderInfo(HttpServletRequest request){
System.out.println("Header Info:");
Enumeration e = request.getHeaderNames();
while (e.hasMoreElements()){
String name = (String)e.nextElement();
System.out.println("Header...........:"+name+"="+request.getHeader(name));
}
}
private void printCookieInfo(HttpServletRequest request){
System.out.println("Cookies:");
Cookie[] cookies = request.getCookies();
for (int ccnt=0; ccnt < cookies.length; ccnt++){
System.out.println(
" cookie...........:"+cookies[ccnt]);
System.out.println(
" cookie name......:"+cookies[ccnt].getName());
System.out.println(
" cookie value.....:"+cookies[ccnt].getValue());
System.out.println(
" cookie max age...:"+cookies[ccnt].getMaxAge());
}
}
private void processCounter(HttpServletRequest request){
HttpSession session = request.getSession(true);
Integer sessionCounter = null;
int tempCounter = 0;
Object obj = session.getAttribute("hit_counter");
if (obj == null){
sessionCounter = new Integer(1);
} else{
sessionCounter = (Integer)obj;
}
tempCounter = sessionCounter.intValue();
System.out.println("Hit Counter........:"+tempCounter);
tempCounter++;
sessionCounter = new Integer(tempCounter);
session.setAttribute("hit_counter", sessionCounter);
}
private String processDataInput(HttpServletRequest request)
throws IOException {
int len = request.getContentLength()+2;
String s = "";
System.out.println("Request Content Length = "+len);
if (len > 2) {
System.out.println("Reading data from request:");
BufferedReader reader = request.getReader();
char[] buffer = new char[len];
int i = reader.read(buffer, 0, buffer.length);
s = new String(buffer);
s = s.substring(0, s.length()-2);
System.out.print(" Data.............:");
System.out.println(s);
System.out.println(" Data Length .....:"+i);
}
return s;
}
private void processResponse(HttpServletResponse response,
String data)
throws IOException {
System.out.println("Responding...");
response.setContentType("text/plain");
response.setContentLength(data.length()+2);
PrintWriter writer = response.getWriter();
writer.println(data);
System.out.println("Response Sent");
}
}[/COLOR][/CODE]
Could anyone with a clue tell me what's wrong with the code. The code were obtained from a book sample but I don't understand it too well.