Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JSP and BEANS

Status
Not open for further replies.

Khanjan

Programmer
Feb 24, 2004
41
NL
Hi,

I am using jsp and Java Beans for login action.

Here are the code's:

login.jsp

<%@ page language="java" contentType="text/html" import="java.sql.*" %>


<%

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

String username = request.getParameter("username");
String password = request.getParameter("password");
String usertype = request.getParameter("usertype"); %>

<jsp:useBean id="user" class="aa.User" scope="request"/>
<jsp:setProperty name="user" property="*"/>

<%

if(user.login())
{
HttpSession s = request.getSession(true);
s.setAttribute("naam", user.getUserName());
s.setAttribute("passwordAdmin", user.getUserPassword());
s.setAttribute("usertype", user.getUserType());
s.setAttribute("loggedin", "true");

response.sendRedirect("wel.jsp");
}
else{
response.sendRedirect("niet.jsp");
}// end else

%>
.....................................................
The User bean is:
aa.User.java

package aa;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import db.*;

public class User
{
private DBConnectie db = new DBConnectie("fahim","fahim","fahim");
private String username,password,usertype;
private int adminid;
private String usernameDB, passwordDB;




public void setUserName(String username)
{
this.username=username;

}
public String getUserName()
{
return username;

}
public void setUserPassword(String password)
{
this.password=password;

}

public String getUserPassword()
{
return password;

}

public void setUserType(String usertype)
{
this.usertype=usertype;

}
public String getUserType()
{
return usertype;

}
public void setAdminId(int adminid)
{
this.adminid=adminid;

}
public int getAdminId()
{
return adminid;

}


public boolean login()
{

try
{
String sql= "SELECT * FROM admin WHERE Naam='"+username+"' AND passwordAdmin='"+password+"'";
db.connect();
ResultSet result = db.selectQuery(sql);

while(result.next())
{
usernameDB= result.getString("naam");
passwordDB= result.getString("password");
if (usernameDB.equals(username))
{
if (passwordDB.equals(password))
{
//username= result.getString("naam");
// password= result.getString("password");
usertype= result.getString("usertype");
adminid= result.getInt("adminid");

return true;
}

}
}


db.close();
}
catch(Exception e)
{
System.out.println( e.getMessage() );
}
return false;
}
}
.......................................................
And in the Wel.jsp

<%= session.getAttribute( "naam" ) %>


WHEN I AM EXCUTING THIS, I AM GETTIN "NULL", OR, IT ALWAYS GOES TO NIET.JSP, WICH IS NOTHING BUT, ERROR MESSAGE.

WHAT AM I DOING WRONG???
PLEASE HELP.....
 
Hi,

I dont see any thing wrong, in JSP and Java Bean. Put a debug statements in login() method . I am not sure what the db object is doing, I think that is causing the problem.

In the jsp page first 4 lines are not required. As java bean does it for you.

If not paste the code of db.connect() and db.selectQuery(sql);

Cheers,
Venu
 
Hi Venur,

I put the next code into login()
System.out.println("SQL |" + sql + "|");

but notting happens. All i get in <%= session.getAttribute( "naam" ) %> is "null".

I hope you or someone else know what is wrong with this code.

Thanx
 
Hi,

Try with this bean...


import java.sql.ResultSet;

/**
* User: VenuReddy
* Date: Feb 28, 2004
* Time: 3:41:19 PM
* To change this template use Options | File Templates.
*/
public class User {

// private DBConnectie db = new DBConnectie("fahim", "fahim", "fahim");
private String username = "",password = "",usertype = "";
private int adminid = 0;
private String usernameDB = "", passwordDB = "";
private boolean login = false;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getUsertype() {
return usertype;
}

public void setUsertype(String usertype) {
this.usertype = usertype;
}

public int getAdminid() {
return adminid;
}

public void setAdminid(String adminid) {
this.adminid = Integer.parseInt(adminid);
}

public String getUsernameDB() {
return usernameDB;
}

public void setUsernameDB(String usernameDB) {
this.usernameDB = usernameDB;
}

public String getPasswordDB() {
return passwordDB;
}

public void setPasswordDB(String passwordDB) {
this.passwordDB = passwordDB;
}

public boolean isLogin() {
System.out.println("Entering isLogin() |" + login + "|");
// uncomment the below statement once you test with my code
/*
try {
String sql = "SELECT * FROM admin WHERE Naam='" + username + "' AND passwordAdmin='" + password + "'";
db.connect();
ResultSet result = db.selectQuery(sql);

while (result.next()) {
usernameDB = result.getString("naam");
passwordDB = result.getString("password");
if (usernameDB.equals(username)) {
if (passwordDB.equals(password)) {
//username= result.getString("naam");
// password= result.getString("password");
usertype = result.getString("usertype");
adminid = result.getInt("adminid");

return true;
}

}
}


db.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return false;

}
*/
if (username != null || !"".equals(username)) {

username = "naam";
password = "pwd";
usertype = "test";
login = true;
}

System.out.println("Leaving isLogin() |" + login + "|");
return login;
}

}



<%@ page language="java" contentType="text/html" import="java.sql.*" %>

<jsp:useBean id="user" class="User" scope="request"/>
<jsp:setProperty name="user" property="*"/>

<%

if(user.isLogin())
{
HttpSession s = request.getSession(true);
s.setAttribute("naam", user.getUsername());
s.setAttribute("passwordAdmin", user.getPassword());
s.setAttribute("usertype", user.getUsertype());
s.setAttribute("loggedin", "true");

response.sendRedirect("wel.jsp");
}
else{
response.sendRedirect("niet.jsp");
}// end else

%>

And then uncomment your statements.

Cheers
Venu
 
Venu Hi

When i am compling your code i get ....username , password and usertype that you have defined. At this point i have not uncommented mine own code. In debug screen, it says:
Entering isLogin():false:
Leaving isLogin():true:

When i am uncommenting mine own code, it says something about the if statment of yours. Should i leave your if statment in mine own code, or put it away.

If i put your if statment between the comment line, i can complie succesfuly, but when i am running the JSP, i get nothing in session, because if(user.isLogin()) is not true or somthing.

Please tell me what to do?
 
Hi, i forgot somthing,

When i have succesfuly complied the bean, without the if statment of yours if (username != null || !"".equals(username)), with my own code. I get in debug screen: Entring isLogin :false:
""Null"

And i am still not able to pic anyting form session. because JSP page, goes directly to else statment.

If you can help, i would appreciet that,
thanx
 
Hi,

Could you copy the Java Bean so that I will hlep us. I am sure its something to do with your db Object. The result set might be retuning a null.

Cheers
Venu

 
Sorry i did not understand you, What do you mean by JAVA bean? Wich Java bean, the USer.java you can see above, and the db class is here:
package db;

import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
//import org.gjt.mm.mysql.*;
import com.mysql.jdbc.*;

public class DBConnectie{

String DBName, DBUser, DBPass, SQLQuery;
Connection conn;
Statement stmt;
ResultSet result;

public DBConnectie(String DBName, String DBUser, String DBPass){
this.DBName = DBName;
this.DBUser = DBUser;
this.DBPass = DBPass;
try {
Class.forName("'com.mysql.jdbc.Driver'").newInstance();
}catch(Exception e){}
}

public void connect() {
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/" + DBName + "?user=" + DBUser + "&password=" + DBPass);
}catch(Exception e){}
}

public ResultSet selectQuery(String SQLQuery){
this.SQLQuery = SQLQuery;

try {
stmt = conn.createStatement();
result = stmt.executeQuery( SQLQuery );
}
catch( Exception e ){}
return result;
}

public void query(String SQLQuery){
this.SQLQuery = SQLQuery;
try {
stmt = conn.createStatement();
stmt.executeQuery( SQLQuery );
}
catch( Exception e ){}
}

public void close(){
try {
stmt.close();
conn.close();
}
catch(Exception e){}
}
}

Note:: This db class works properly with other JSP and java Beans.
 
Hi,

I dont see any problem with JavaBean and DB object. Put as many debug statements in isLogin(). May the query is not returning any value. Just make sure the query is returning the result.

public boolean isLogin()
{

try
{
String sql= "SELECT * FROM admin WHERE Naam='"+username+"' AND passwordAdmin='"+password+"'";
db.connect();
ResultSet result = db.selectQuery(sql);
boolean check= result.next();
if(check){
do{
usernameDB= result.getString("naam");
passwordDB= result.getString("password");
if (usernameDB.equals(username))
{
if (passwordDB.equals(password))
{
//username= result.getString("naam");
// password= result.getString("password");
usertype= result.getString("usertype");
adminid= result.getInt("adminid");

}// end if
}// end if

}while(result.next());
login = true;
}else{
login = false;
}

db.close();
}
catch(Exception e)
{
System.out.println( e.getMessage() );
}
return login;
}


Cheers,
Venu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top