aspose file tools*
The moose likes Java Micro Edition and the fly likes the problem abt communication between serlvet & midlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "the problem abt communication between serlvet & midlet" Watch "the problem abt communication between serlvet & midlet" New topic
Author

the problem abt communication between serlvet & midlet

Wai Le
Greenhorn

Joined: Mar 17, 2003
Posts: 8
Hi all,
I wrote a simple midlet which just pass the parameter to the servlet and the servlet will return the parameter to the midlet.
I am sure that the coding of midlet is correct. The coding below is the servlet I modified. Can someone tell me that is the mistake
of it?
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
Public class RequestServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException{
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
String user = (String)request.getParameter("user");
out.print("Rec: "+user);
}

}
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

change your out.print to out.println
if you want you can also add
response.setContentLength(int length);
however you would need to declare a String with the results then pass the String to the out.println();

for instance

But setContentLength is not necessary.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Wai Le
Greenhorn

Joined: Mar 17, 2003
Posts: 8
Hi Mark,
I replace a part of code with the code you provided of the previous post, but it cannot still work, it will return the null value me.
Any suggestions??
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

try putting some System.out.println in you servlet to make sure that you are receiving the request from the PDA correctly.
The code I supplied works fine, so it has to be what is being submitted to the servlet. Maybe you aren't sending a parameter called "user".
What is you MIDlet code that calls the Servlet?
Mark
a sanjuan
Ranch Hand

Joined: Jun 12, 2002
Posts: 164
(1) check that the servlet is fine by hitting it with a browser (use a post form since you're using doPost)...you should receive the resp if it is ok
(2) what exactly are you receiving in your midlet? if it is rec:null, then you did not pass a parameter user to the servlet (and btw, you do not need to explicitly cast the request parameter in the servlet).
(3) you might decide to post the relevant midlet code, since that would help.
Wai Le
Greenhorn

Joined: Mar 17, 2003
Posts: 8
import javax.microedition.rms.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import java.io.*;
import java.util.Vector;
public class SecondMidletServlet extends MIDlet implements CommandListener {
Display display = null;
List menu = null;
TextBox input = null;
String user = null;
String url = "http://localhost:8080/examples/servlet/RequestServlet";
static final Command backCommand = new Command("Back", Command.BACK, 0);
static final Command submitCommand = new Command("Submit", Command.OK, 2);
static final Command exitCommand = new Command("Exit", Command.STOP, 3);
String currentMenu = null;
public SecondMidletServlet() {
}
public void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
menu = new List("Invoke Servlet", Choice.IMPLICIT);
menu.append("Add a user", null);
menu.addCommand(exitCommand);
menu.setCommandListener(this);
mainMenu();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
void mainMenu() {
display.setCurrent(menu);
}
public void addName() {
input = new TextBox("Enter first name:", "", 5, TextField.ANY);
input.addCommand(submitCommand);
input.addCommand(backCommand);
input.setCommandListener(this);
input.setString("");
display.setCurrent(input);
}
void invokeServlet(String url) throws IOException {
HttpConnection c = null;
InputStream is = null;
OutputStream os = null;
StringBuffer b = new StringBuffer();
TextBox t = null;
try {
c = (HttpConnection)Connector.open(url);
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("IF-Modified-Since", "20 Jan 2001 16:19:14 GMT");
c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language", "en-CA");

os = c.openOutputStream();
os.write(("name="+user).getBytes());
System.out.println("Length: "+("name="+user).getBytes());

// or you can easily do:
// os.write(("name="+user).getBytes());
os.flush();
is = c.openDataInputStream();
int ch;
while ((ch = is.read()) != -1) {
b.append((char) ch);
System.out.print((char)ch);
}
t = new TextBox("Second Servlet", b.toString(), 1024, 0);
t.addCommand(backCommand);
t.setCommandListener(this);
} finally {
if(is!= null) {
is.close();
}
if(os != null) {
os.close();
}
if(c != null) {
c.close();
}
}
display.setCurrent(t);
}
public void commandAction(Command c, Displayable d) {
String label = c.getLabel();
if (label.equals("Exit")) {
destroyApp(true);
} else if (label.equals("Back")) {
mainMenu();
} else if (label.equals("Submit")) {
user = input.getString();
try {
invokeServlet(url);
}catch(IOException e) {}
} else {
addName();
}
}
}
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

In your commandAction method, after you assign user the value in the input TextBox, type in a System.out.println(user) and see if it shows a value in the Toolkit, if you are using the Toolkit. This will make sure that it is getting assigned.
Now, it is easier if you change your "post" to be a "Get" That way you can append your parameters to the URL. But that was a suggestion I got from Jonathan Knudsen's book.
You can also check to see if the Command passed is the same command using Object equality, rather than the getLabel() method.
example

I really would suggest you use the Get instead of the Post method for sending the information to the Servelt, it is just so much easier.
In your servlet you can even simply have the doGet() call doPost() passing it the Request and Response objects.
Mark
a sanjuan
Ranch Hand

Joined: Jun 12, 2002
Posts: 164
your name value pair in the MIDlet is name=username, but your servlet is trying to find a name called "user" in the query string.
in the servlet use:
String user = request.getParameter("name");
this looks like a textbook example...i suggest giving the name-value pairs less confusing names...e.g.
"customer="
String user = request.getParameter("customer")
[ March 18, 2003: Message edited by: a sanjuan ]
Wai Le
Greenhorn

Joined: Mar 17, 2003
Posts: 8
Actually do you have any examples which is similar to my coding?
a sanjuan
Ranch Hand

Joined: Jun 12, 2002
Posts: 164
not exactly like that....probably should present the response as a stringitem instead of in a textbox though.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: the problem abt communication between serlvet & midlet