have built up a midlet that need to connect to the
servlet which is hosted using
Tomcat ...
When i click the submit button in the mobile emulator ... i received the following message in the console of
Java Wireless ToolKit .... What is the problem actually ? .... Can anyone give me some guides ?
Any Helps will be appreciated
"<html><head><title>Apache Tomcat/5.0.12 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 404 - Servlet RequestServlet is not available</h1><HR size="1" noshade><p><b>type</b> Status report</p><p><b>message</b> <u>Servlet RequestServlet is not available</u></p><p><b>description</b> <u>The requested resource (Servlet RequestServlet is not available) is not available.</u></p><HR size="1" noshade><h3>Apache Tomcat/5.0.12</h3></body></html>"
The following is my midlet :
public class SecondMidletServlet extends MIDlet implements CommandListener, Runnable {
Display display = null;
List menu = null;
TextBox input = null;
String user = null;
String url = "http://pup.no-ip.net:8001/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);
StringBuffer b = new StringBuffer();
String currentMenu = null;
Thread thread;
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);
if(c == null)
System.out.println("PKPK");
else
System.out.println("PSPS");
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();
String str = "name="+user;
byte postmsg[] = str.getBytes();
System.out.println("Length: "+str.getBytes());
for(int i=0;i<postmsg.length;i++) {
os.write(postmsg[i]);
}
// 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);
}
} finally {
if(is!= null) {
is.close();
}
if(os != null) {
os.close();
}
if(c != null) {
c.close();
}
}
//display.setCurrent(t);
}
public void run()
{
try
{
invokeServlet(url);
TextBox t = new TextBox("Second Servlet", b.toString(), 1024, 0);
t.addCommand(backCommand);
t.setCommandListener(this);
display.setCurrent(t);
}
catch(Exception ex){}
}
public void refreshScreen(StringBuffer b)
{
TextBox t = new TextBox("Second Servlet", b.toString(), 1024, 0);
t.addCommand(backCommand);
t.setCommandListener(this);
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);
thread = new Thread(this);
thread.start();
// }
// catch(IOException e) {}
}
else {
addName();
}
}
}
//Servlet side
public class RequestServlet extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
BufferedReader br = request.getReader();
String buf = br.readLine();
System.out.println("LSLS");
out.print("Rec: "+buf);
}
}