• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

authenticating/validating user name and password

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ();
}
 
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Everyone is a villain in someone else's story. Especially this devious tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic