Det er godt nok tomcat, som mange ikke bryder sig om, men den virker sammen med den servlet jeg har lavet.
Hvis jeg kalder den i browseren og giver den parametre med, svarer den fin tilbage.
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MinServlet extends HttpServlet
{
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
// Read the parameters sent from MIDlet
String acct = req.getParameter("account"),
pwd = req.getParameter("password");
if (acct == null || pwd == null)
{
res.sendError(res.SC_BAD_REQUEST, "Unable to lase parameters");
return;
}
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
out.print("acct:" + acct + "\npwd:" + pwd);
out.close();
}
/*--------------------------------------------------
* Information about servlet
*-------------------------------------------------*/
public String getServletInfo()
{
return "";
}
}
Desværre virker midleten ikke;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class CallServletGET extends MIDlet implements CommandListener
{
private Display display; // Reference to display object
private Form fmMain; // The main form
private TextField tfAcct; // Get account number
private TextField tfPwd; // Get password
private Command cmCall; // Command to call the servlet
private Command cmExit; // Command to exit
public CallServletGET()
{
display = Display.getDisplay(this);
// Textfields
tfAcct = new TextField("Account:", "", 5, TextField.NUMERIC);
tfPwd = new TextField("Password:", "", 10, TextField.ANY | TextField.PASSWORD);
// Define commands
cmCall = new Command("Call", Command.SCREEN, 2);
cmExit = new Command("Exit", Command.EXIT, 1);
// Create the form, add components and commands
fmMain = new Form("Data from servlet");
fmMain.append(tfAcct);
fmMain.append(tfPwd);
fmMain.addCommand(cmExit);
fmMain.addCommand(cmCall);
// Capture events
fmMain.setCommandListener(this);
}
// Called by application manager to start the MIDlet.
public void startApp()
{
display.setCurrent(fmMain);
}
public void pauseApp()
{ }
public void destroyApp(boolean unconditional)
{ }
private void callServlet() throws IOException
{
HttpConnection http = null;
InputStream iStrm = null;
// Data is passed at the end of url for GET
String url = "
http://localhost:8080/para/MinS" + "?" +
"account=" + tfAcct.getString() + "&" +
"password=" + tfPwd.getString();
try
{
http = (HttpConnection) Connector.open(url);
//----------------
// Client Request
//----------------
// 1) Send request method
http.setRequestMethod(HttpConnection.GET);
// 2) Send header information - none
// 3) Send body/data - data is at the end of URL
//----------------
// Server Response
//----------------
if (http.getResponseCode() == HttpConnection.HTTP_OK)
{
iStrm = http.openInputStream();
// 2) Get header information - none
// 3) Get body (data)
int length = (int) http.getLength();
if (length > 0)
{
byte servletData[] = new byte[length];
iStrm.read(servletData);
// Update the string item on the display
fmMain.append("You passed to the servlet: \n" + new String(servletData));
}
else
fmMain.append("Unable to read data");
}
}
catch (Exception e)
{
fmMain.append("Network error");
System.out.println(e.toString());
}
finally
{
// Clean up
if (iStrm != null)
iStrm.close();
if (http != null)
http.close();
}
}
public void commandAction(Command c, Displayable s)
{
if (c == cmCall)
{
try
{
callServlet();
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
else if (c == cmExit)
{
destroyApp(false);
notifyDestroyed();
}
}
}
Et bud på hvad der er galt???
QD::