File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java Micro Edition and the fly likes authenticating/validating user name and password Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "authenticating/validating user name and password " Watch "authenticating/validating user name and password " New topic
Author

authenticating/validating user name and password

sachin sav
Greenhorn

Joined: Mar 26, 2004
Posts: 9
Hi! I would appreciate any help :-) I am relatively new to J2ME. I have midlet which allows the user to enter a user name and password. How do I authenticate this so that if the user name and password match the database they are permitted access if not then it displays a message to register. The database is a simple MS Access database.
This is all i have sofar

import javax.microedition.lcdui.*;
public class FormSubclass extends Form implements CommandListener {

private TextField passWord;
private TextField userName;
private Command cmdBack;
private MainScreen midlet;

public FormSubclass (String title, MainScreen midlet ){

super (title);
this.midlet = midlet;
passWord = new TextField("Password", "",8,TextField.PASSWORD);
userName = new TextField ("User Name", "",8,TextField.ANY);
cmdBack = new Command ("Back", Command.BACK, 1);
append (userName);
append (passWord);
addCommand (cmdBack);
setCommandListener (this);
}

public void commandAction (Command c, Displayable s) {

if (c == cmdBack)
midlet.displayMainForm ();
}
Pavel Kubal
Ranch Hand

Joined: Mar 13, 2004
Posts: 356
Use HTTP connection if you are developing for midp1.0. You send pass and username to server via ota (GET, POST) and server returns you result and this result you'll show in midlet.
Of course, you should have some script on your server, which connects to db and returns result.
I hope this helps
sachin sav
Greenhorn

Joined: Mar 26, 2004
Posts: 9
I am a bit confused on the POST and GET. Would the midlet in which the user enters their password and username perform a POST and a servlet which retrieves the password/usernane from the midlet perform GET. Being new to this what is ota?
Really appreciate the help.
a sanjuan
Ranch Hand

Joined: Jun 12, 2002
Posts: 164
sachin:
i'm assuming the ms database is residing on a server and not on your phone ;-)
in which case, the midlet will have to send the username/password to a J2EE webapp (connected to the database) running on the server. the midlet will send this using httpconnection of generic framework. here's a quick note, although i suggest getting a good midp book for comprehensiveness sake:
http://63.240.93.131/articles/article.asp?p=131116&seqnum=7
the webapp will use something like jdbc/odbc to connect to the database and query whether the username and password are correct, then send back a response to the midlet.
the midlet will then parse the response for the yes or no and proceed accordingly.
again, for comprehensiveness sake, getting a good book on midp (and J2ee on the serverside) would be useful.
sachin sav
Greenhorn

Joined: Mar 26, 2004
Posts: 9
I have bought the core j2me sun series book and have adapted some code however the code does not seem to work. The error I get is
unreported exception java.io.IOException; must be caught or declared to be thrown
postInformation();
What does this mean?
Is this to do with the way i have written the URL "http://localhost/8000/midp/loginserv";
The servlet and database are both hosted on the same machine. i have compiled and seployed the servlet. I will appreciate any help. Thank you.
MIDLET CODE
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;


public void commandAction (Command c, Displayable s) {

if (c== cmdSelect){
postInformation();

}else if (c == cmdBack){
midlet.displayMainForm ();
}

}

private void postInformation() throws IOException {
HttpConnection http = null;
OutputStream oStrm = null;
InputStream iStrm = null;
boolean ret = false;

String url = "http://localhost/8000/midp/loginserv";

try {
http = (HttpConnection) Connector.open (url);
oStrm = http.openOutputStream ();
http.setRequestMethod (HttpConnection.POST);
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
//http.setRequestProperty("Connection", "close");
byte data [] = ("username=" + userName.getString()).getBytes();
oStrm.write (data);
data = ("&password=" + passWord.getString()).getBytes();
oStrm.write(data);
oStrm.flush();

iStrm = http.openInputStream();
ret = processServerResponse(http, iStrm);
}
finally
{
if (iStrm != null)
iStrm.close();
if (oStrm != null)
oStrm.close();
if (http != null)
http.close ();

}if (ret == false)
System.out.println("There is something wrong");
}

private boolean processServerResponse (HttpConnection http, InputStream iStrm) throws IOException {

errorMsg = null;

if (http.getResponseCode()==HttpConnection.HTTP_OK)
{
int length = (int) http.getLength ();
String str;
if (length != -1)
{
byte servletData[] = new byte[length];
iStrm.read (servletData);
str = new String(servletData);

}
else
{
ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
int ch;

while ((ch= iStrm.read()) != -1)
bStrm.write(ch);
str = new String (bStrm.toByteArray());
bStrm.close();
}

return true;

}
else
errorMsg = new String (http.getResponseMessage());
return false;
}
}
SERVLET CODE
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class LoginServlet extends HttpServlet
{

protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{

String username = req.getParameter("username"),
password = req.getParameter("password");
String balance = accountLookup(username, password);
if (balance == null)
{
res.sendError(res.SC_BAD_REQUEST, "Unable to locate account.");
return;
}

res.setContentType("text/plain");
PrintWriter out = res.getWriter();
out.print(balance);
out.close();
}
/*--------------------------------------------------
* Lookup bank account balance in database
*-------------------------------------------------*/
private String accountLookup(String username, String password)
{
Connection con = null;
Statement st = null;
StringBuffer msgb = new StringBuffer("");
try
{
// These will vary depending on your server/database
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc dbc:testdb");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select members from testdb where username = " +username + "and password = '" + password + "'");

if (rs.next())
return rs.getString(1);
else
return null;
}
catch (Exception e)
{
return e.toString();
}
}

}
a sanjuan
Ranch Hand

Joined: Jun 12, 2002
Posts: 164
if you're getting that during compilation it means there is some code that must be enclosed within a try catch block and is not.
btw, is your webapp's name "8000" or are you mistyping that URL? perhaps it should be (with a colon):
http://localhost:8000/midp/loginserv
you wanna send me the entire code? i'll take a look at it and get back to you. email it me if you want:
kalim1998(at)yahoo.com
[ March 27, 2004: Message edited by: a sanjuan ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: authenticating/validating user name and password