×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    3

    mobile application using J2ME

    Hello, I made a program where I enter username and password from midlet and send them to servlet which checks the username and password in a mysql database. The servlet is to send a response “valid” to the midlet if the username and password exist or “invalid” if they don’t. But when I run the program the response recieved on the midlet side that I get on the emulator is always “valid” even with wrong username and password and displays empty MainMenu.
    What is wrong with my code. Find out my problem and tell me the exact solutions. I am using netbeans 7.2 and 7.3

    here is the midlet for login:

    Code:
    import javax.microedition.lcdui.*;
    import javax.microedition.io.*;
    import java.io.*;
    public class LoginScreen implements CommandListener,Runnable,ItemCommandListener
    {
    Form login;
    static TextField un;
    TextField pwd;
    Command Submit;
    view plaincopy to clipboardprint?
    Note: Text content in the code blocks is automatically word-wrapped
    
          
    
    
    Command Cancel,Exit;
    StringItem submit,cancel;
    Alert alert;
    
    Thread thr1;
    static String name,gender;
    Image invaliduser,warning;
    
    public LoginScreen()
    {
    login=new Form("LOGIN FORM");
    un=new TextField("LOGINID","",30,TextField.ANY);
    un.setLayout(3);
    submit=new StringItem("","SUBMIT",StringItem.BUTTON);
    cancel=new StringItem("","CLEAR",StringItem.BUTTON);
    pwd=new TextField("PASSWORD","",30,TextField.PASSWORD);
    Submit=new Command("SUBMIT",Command.ITEM,1);
    Cancel=new Command("CLEAR",Command.ITEM,1);
    Exit=new Command("Exit",Command.BACK,0);
    try
    {
    invaliduser=Image.createImage("/invaliduser.png");
    warning=Image.createImage("/warning.png");
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }
    public Form loginFormMethod()
    {
    login.append(un);
    login.append(pwd);
    login.addCommand(Exit);
    login.append(submit);
    submit.setDefaultCommand(Submit);
    submit.setItemCommandListener(this);
    login.append(cancel);
    cancel.setDefaultCommand(Cancel);
    cancel.setItemCommandListener(this);
    login.setCommandListener(this);
    return login;
    }
    
    public void commandAction(Command cmd,Item item)
    {
    if(item==submit)
    {
    String sid1=un.getString();
    String password1=pwd.getString();
    if(sid1.equals(""))
    {
    alert=new Alert("confirmation");
    alert.setImage(warning);
    alert.setString("Please enter username");
    Login.dis.setCurrent(alert);
    }
    else if(password1.equals(""))
    {
    alert=new Alert("confirmation");
    alert.setImage(warning);
    alert.setString("Please enter password");
    Login.dis.setCurrent(alert);
    }
    else
    {
    thr1=new Thread(this);
    thr1.start();
    }
    }
    if(item==cancel)
    {
    un.setString("");
    pwd.setString("");
    }
    
    }
    
    public void commandAction(Command cmd,Displayable disp)
    {
    
    if(cmd==Exit)
    {
    Login.destroy.destroyMethod();
    }
    
    }
    
    public void run()
    {
    String result="";
    try
    {
    String sid=un.getString();
    System.out.println("name"+sid);
    
    String password=pwd.getString();
    System.out.println("pwd"+password);
    String url="http://localhost:4040/web/LoginServlet?un="+sid+"&pwd="+password;
    System.out.println("Query "+url);
    StreamConnection sc=(StreamConnection) Connector.open(url.replace(' ','+'));
    DataInputStream dis=sc.openDataInputStream();
    int i=dis.read();
    while(i!=-1)
    {
    //System.out.println("Value "+i);
    result=result+(char)i;
    i=dis.read();
    }
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    alert=new Alert("confirmation");
    result=result.trim();
    System.out.println(result.length());
    if(result.equals("invalid"))
    {
    System.out.println("Invalid User");
    alert.setImage(invaliduser);
    alert.setString("Invalid User");
    Login.dis.setCurrent(alert);
    }
    else
    {
    System.out.println("Valid User");
    // alert.setImage(invaliduser);
    alert.setString("Valid User");
    Login.dis.setCurrent(alert);
    
    MainMenuScreen mms=new MainMenuScreen();
    Login.dis.setCurrent(mms.mainMenuMethod());
    
    }
    
    }
    }
    And Here is the servlets of Login:
    Code:
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.sql.*;
    import java.io.*;
    public class LoginServlet extends HttpServlet
    {
    public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
    {
    try
    {
    System.out.println("Entered loginservlet");
    String loginid=req.getParameter("un");
    String password=req.getParameter("pwd");
    System.out.println("name"+loginid);
    System.out.println("pwd"+password);
    PrintWriter pw=res.getWriter();
    //pw.println("Name "+loginid);
    //pw.println("Password "+password);
    
    Class.forName("com.mysql.jdbc.Driver");
    
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mobile","root","root");
    
    Statement st=con.createStatement();
    
    ResultSet rs=st.executeQuery("select * from login where loginid='"+loginid+"' and password='"+password+"'");
    
    if(rs.next())
    {
    System.out.println(rs.getString(1));
    pw.println("Valid User");
    }
    else
    {
    pw.println("invalid");
    }
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    }

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: mobile application using J2ME

    What did you discover already?

    Does the server send "invalid" What series of bytes does the device receive? Do you at least know on which side the problem is?

    Graham.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1

    Re: mobile application using J2ME

    What are you getting from server of getting exception in your code

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    31

    Re: mobile application using J2ME

    Quote Originally Posted by endashawwolde View Post
    Hello, I made a program where I enter username and password from midlet and send them to servlet which checks the username and password in a mysql database. The servlet is to send a response “valid” to the midlet if the username and password exist or “invalid” if they don’t. But when I run the program the response recieved on the midlet side that I get on the emulator is always “valid” even with wrong username and password and displays empty MainMenu.
    What is wrong with my code. Find out my problem and tell me the exact solutions. I am using netbeans 7.2 and 7.3

    here is the midlet for login:

    Code:
    import javax.microedition.lcdui.*;
    import javax.microedition.io.*;
    import java.io.*;
    public class LoginScreen implements CommandListener,Runnable,ItemCommandListener
    {
    Form login;
    static TextField un;
    TextField pwd;
    Command Submit;
    view plaincopy to clipboardprint?
    Note: Text content in the code blocks is automatically word-wrapped
    
          
    
    
    Command Cancel,Exit;
    StringItem submit,cancel;
    Alert alert;
    
    Thread thr1;
    static String name,gender;
    Image invaliduser,warning;
    
    public LoginScreen()
    {
    login=new Form("LOGIN FORM");
    un=new TextField("LOGINID","",30,TextField.ANY);
    un.setLayout(3);
    submit=new StringItem("","SUBMIT",StringItem.BUTTON);
    cancel=new StringItem("","CLEAR",StringItem.BUTTON);
    pwd=new TextField("PASSWORD","",30,TextField.PASSWORD);
    Submit=new Command("SUBMIT",Command.ITEM,1);
    Cancel=new Command("CLEAR",Command.ITEM,1);
    Exit=new Command("Exit",Command.BACK,0);
    try
    {
    invaliduser=Image.createImage("/invaliduser.png");
    warning=Image.createImage("/warning.png");
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }
    public Form loginFormMethod()
    {
    login.append(un);
    login.append(pwd);
    login.addCommand(Exit);
    login.append(submit);
    submit.setDefaultCommand(Submit);
    submit.setItemCommandListener(this);
    login.append(cancel);
    cancel.setDefaultCommand(Cancel);
    cancel.setItemCommandListener(this);
    login.setCommandListener(this);
    return login;
    }
    
    public void commandAction(Command cmd,Item item)
    {
    if(item==submit)
    {
    String sid1=un.getString();
    String password1=pwd.getString();
    if(sid1.equals(""))
    {
    alert=new Alert("confirmation");
    alert.setImage(warning);
    alert.setString("Please enter username");
    Login.dis.setCurrent(alert);
    }
    else if(password1.equals(""))
    {
    alert=new Alert("confirmation");
    alert.setImage(warning);
    alert.setString("Please enter password");
    Login.dis.setCurrent(alert);
    }
    else
    {
    thr1=new Thread(this);
    thr1.start();
    }
    }
    if(item==cancel)
    {
    un.setString("");
    pwd.setString("");
    }
    
    }
    
    public void commandAction(Command cmd,Displayable disp)
    {
    
    if(cmd==Exit)
    {
    Login.destroy.destroyMethod();
    }
    
    }
    
    public void run()
    {
    String result="";
    try
    {
    String sid=un.getString();
    System.out.println("name"+sid);
    
    String password=pwd.getString();
    System.out.println("pwd"+password);
    String url="http://localhost:4040/web/LoginServlet?un="+sid+"&pwd="+password;
    System.out.println("Query "+url);
    StreamConnection sc=(StreamConnection) Connector.open(url.replace(' ','+'));
    DataInputStream dis=sc.openDataInputStream();
    int i=dis.read();
    while(i!=-1)
    {
    //System.out.println("Value "+i);
    result=result+(char)i;
    i=dis.read();
    }
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    alert=new Alert("confirmation");
    result=result.trim();
    System.out.println(result.length());
    if(result.equals("invalid"))
    {
    System.out.println("Invalid User");
    alert.setImage(invaliduser);
    alert.setString("Invalid User");
    Login.dis.setCurrent(alert);
    }
    else
    {
    System.out.println("Valid User");
    // alert.setImage(invaliduser);
    alert.setString("Valid User");
    Login.dis.setCurrent(alert);
    
    MainMenuScreen mms=new MainMenuScreen();
    Login.dis.setCurrent(mms.mainMenuMethod());
    
    }
    
    }
    }
    And Here is the servlets of Login:
    Code:
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.sql.*;
    import java.io.*;
    public class LoginServlet extends HttpServlet
    {
    public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
    {
    try
    {
    System.out.println("Entered loginservlet");
    String loginid=req.getParameter("un");
    String password=req.getParameter("pwd");
    System.out.println("name"+loginid);
    System.out.println("pwd"+password);
    PrintWriter pw=res.getWriter();
    //pw.println("Name "+loginid);
    //pw.println("Password "+password);
    
    Class.forName("com.mysql.jdbc.Driver");
    
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mobile","root","root");
    
    Statement st=con.createStatement();
    
    ResultSet rs=st.executeQuery("select * from login where loginid='"+loginid+"' and password='"+password+"'");
    
    if(rs.next())
    {
    System.out.println(rs.getString(1));
    pw.println("Valid User");
    }
    else
    {
    pw.println("invalid");
    }
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    }

    I see there is no problem with the web service , check your client code . why are you using streamconnection
    to access a simple webservice .use httpconnection for sample checkout the link below
    http://www.developer.nokia.com/Commu...tps_in_Java_ME
    Regards,
    Venkatesh

Similar Threads

  1. mobile application using J2ME
    By endashawwolde in forum Nokia Asha Web Apps
    Replies: 2
    Last Post: 2013-04-20, 18:15
  2. How to run J2me application in Android mobile?
    By ajitkumar26 in forum Mobile Java General
    Replies: 13
    Last Post: 2012-02-23, 12:26
  3. How to run J2me Application on windows mobile 6.1
    By rajan701v in forum Mobile Java General
    Replies: 4
    Last Post: 2011-01-17, 07:36
  4. J2ME Application for SPICE Mobile
    By rajan701v in forum Mobile Java General
    Replies: 1
    Last Post: 2010-12-18, 18:48
  5. Running Mobile application in J2ME WTK via OTA but not in Mobile
    By jayamnkl in forum Mobile Java General
    Replies: 8
    Last Post: 2009-04-21, 08:24

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •