dcsimg
How to pass data from jsp to midlet?
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   xing_xing
Posted On:   Thursday, March 20, 2003 08:13 AM

I'm doing a j2me application where after the user login, the login data is pass to a LoginServlet.java and finally to a LoginOK.jsp. This jsp sends back a "welcome " back to the midlet. In the emulator screen, it shows "welcome " and a 'exit' command. I would like to ask how to continue my midlet code as like the 'exit' command becomes a 'continue' command for the users to go to the next part of the midlet codes. Where should the codes be place in? HelloSIA.java (midlet) import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.io.Connector; import javax.microedition.io.HttpConnection; import javax.mic   More>>

I'm doing a j2me application where after the user login, the login data is pass to a LoginServlet.java and finally to a LoginOK.jsp. This jsp sends back a "welcome " back to the midlet. In the emulator screen, it shows "welcome " and a 'exit' command. I would like to ask how to continue my midlet code as like the 'exit' command becomes a 'continue' command for the users to go to the next part of the midlet codes. Where should the codes be place in?


HelloSIA.java (midlet)


import javax.microedition.midlet.*;


import javax.microedition.lcdui.*;


import javax.microedition.io.Connector;


import javax.microedition.io.HttpConnection;


import javax.microedition.io.StreamConnection;


import java.io.DataInputStream;


import java.io.DataOutputStream;


import java.io.InputStream;


import java.io.IOException;


import java.io.OutputStream;



public class HelloSIA extends MIDlet implements CommandListener {


TextField username;


TextField password;


Form welcomeForm;


Form loginForm;


Form introForm;


Form outputForm;


Form tForm;


StringItem response;



boolean commandAvailable;


String idPassword;


CommandThread commandThread;


Command exitCommand;


Command loginCommand;


private Command introCommand;


private Command okCommand;


private Command backCommand;


private Display display;


String a="Welcome";


String b="Welcome1";



public HelloSIA() {


display = Display.getDisplay(this);


exitCommand = new Command("Exit", Command.SCREEN, 2);


introCommand = new Command("Introduction",Command.SCREEN, 2);


loginCommand = new Command("Sign-in", Command.SCREEN, 2);

}


public void startApp() {


welcomeForm= new Form("LINES");


Image sia = null;



try


{


sia = Image.createImage

( "/Images/sia.png");


}


catch(java.io.IOException e)


{


}


Alert z = new Alert("","",sia,null);


z.setTimeout(1000);


welcomeForm.append(a);


welcomeForm.addCommand(exitCommand);


welcomeForm.addCommand(introCommand);


welcomeForm.addCommand(loginCommand);


welcomeForm.setCommandListener(this);


display.setCurrent(z,welcomeForm);


}


public void pauseApp() { }


public void destroyApp(boolean unconditional) { }


public void commandAction(Command c, Displayable s) {


if (c == exitCommand) {


destroyApp(false);


notifyDestroyed();


}


else if (c == loginCommand){


loginForm= new Form("Sign In:");


username = new TextField("User",null,10,TextField.ANY);


password = new TextField

("Password",null,10,TextField.PASSWORD);


loginForm.append(username);


loginForm.append(password);


okCommand=new Command("Submit",Command.SCREEN,1);


loginForm.addCommand(okCommand);


loginForm.addCommand(exitCommand);


loginForm.setCommandListener(this);



outputForm = new Form("test:");


response = new StringItem(null,null);


outputForm.append(response);


outputForm.addCommand(exitCommand);


outputForm.setCommandListener(this);


commandAvailable = false;


commandThread = new CommandThread(this);


commandThread.start();


display.setCurrent(loginForm);


}


else if (c == introCommand){


introForm= new Form("Introduction");


introForm.append(b);


introForm.addCommand(loginCommand);


introForm.addCommand(exitCommand);


introForm.setCommandListener(this);


display.setCurrent(introForm);


}



else if (c == okCommand)


synchronized (this) {


commandAvailable = true;


notify();


}


}


class CommandThread extends Thread {


MIDlet parent;


boolean exit = false;


public CommandThread(MIDlet parent) {


this.parent = parent;


}


public void run() {


while (true) {


synchronized(parent) {


while(!commandAvailable) {


try {


parent.wait();


}


catch (InterruptedException e) {


}


}


commandAvailable = false;


}


performLogin();


}


}


public void performLogin() {


HttpConnection conn = null;


InputStream is = null;


OutputStream os = null;


byte[] receivedData = null;


try {


String url =


"http://localhost:8080/InfoTech/servlet/LoginServlet";


//Open the http connection


conn = (HttpConnection)Connector.open(url);


conn.setRequestMethod(HttpConnection.POST);


conn.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");


conn.setRequestProperty("Content-type","application/x-www-form-urlencoded");


byte[] postData = createPostData();


os = conn.openOutputStream();


os.write(postData);


//Obtain an input stream for the connection


is = conn.openInputStream();


String contentType = conn.getType();


int len = (int)conn.getLength();


if (len > 0) {


receivedData = new byte[len];


int nb = is.read(receivedData);


}


else {


receivedData = new byte[1024];


//Read a line at time from the input

stream
int ch;


len = 0;


while ((ch = is.read()) != -1) {


receivedData[len++] = (byte)ch;


}


}


response.setText(new String(receivedData,0,len));


display.setCurrent(outputForm);


}


catch (IOException e) {


System.out.println(e.getMessage());


e.printStackTrace();


}


finally {


try {


if (is != null) {


is.close();


}


if (os != null) {


os.close();


}


if (conn != null) {


conn.close();


}


}


catch (IOException e) {


}


//tForm= new Form("again:");


//tForm.append(a);


//display.setCurrent(tForm);


}


}


public byte[] createPostData() {


StringBuffer sb = new StringBuffer();


sb.append("username=");


sb.append(username.getString());


sb.append("&password=");


sb.append(password.getString());


System.out.println("sb = <" + sb.toString() + ">");


return sb.toString().getBytes();


}


}


}




import java.io.*;


import java.text.*;


import java.util.*;


import javax.servlet.*;


import javax.servlet.http.*;


import java. sql.*;



public class LoginServlet extends HttpServlet {


static final String dbURL =


"jdbc:mysql://localhost/"+"users";


public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {



Connection conn = null;


String nextJSP = null;


try {


Class.forName("org.gjt.mm.mysql.Driver");


//System.out.println("Check Point 0");


}


catch (ClassNotFoundException e) {


throw new ServletException("Unable to load JDBC driver");


}


try {


String username = (String)request.getParameter("username");


String password = (String)request.getParameter("password");


conn = DriverManager.getConnection(dbURL);


//System.out.println("Check Point 1");


Statement stmt = conn.createStatement();


String query = "SELECT firstname, lastname " +
"FROM users " + "WHERE username = '"+
username + "' AND password ='" +
password + "'";


ResultSet rs = stmt.executeQuery(query);


if (rs.next()) {


//System.out.println("Check Point 2");


StringBuffer fullName = new StringBuffer();


fullName.append(rs.getString(1));


fullName.append(" ");


fullName.append(rs.getString(2));


request.setAttribute ("fullName", fullName.toString());


nextJSP = "/LoginOK.jsp";


}


else {


nextJSP = "/LoginFailed.jsp";


}


conn.close();


ServletConfig config = getServletConfig();


ServletContext context = config.getServletContext();


RequestDispatcher rd = context.getRequestDispatcher(nextJSP);


rd.forward(request, response);


}


catch (SQLException e) {


throw new ServletException("SQL call Failed");


}


catch (Exception e) {


throw new ServletException (e.getMessage());


}


finally {


if (conn !=null) {


try {


conn.close();


}


catch (SQLException e) {


throw new ServletException("connection close failed");


}


}


}


}



public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
doPost(request, response);


}


}





<%response.setContentType("text/plain");%>


Welcome~ <%= (request.getAttribute("fullName"))%>

   <<Less
About | Sitemap | Contact