Hi,
Sorry guys, I'm starting JSP only. So I hope you could bear w/ me & not loose patience. I have tomcat5.5 and oracle xe. What I doing is to have a login page for all users before using my app. Here's the code for my Users.class:
Code:
/*
* Users.java
*
* Created on June 26, 2007, 3:27 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package biz;
import java.util.Hashtable;
import java.util.ArrayList;
import java.util.Enumeration;
import java.sql.*;
/**
*
* @author Me
*/
public class Users {
public int Id = 0;
public int groupId = 0;
public String userName = null;
public String password = null;
public static String dbOraDriver = "com.sun.sql.jdbcx.oracle.OracleDataSource";
public static String dbUrl = "jdbc:oracle:thin:oradb/******@localhost:1521:xe";
/** Creates a new instance of Users */
public Connection connect() {
Connection conn = null;
try {
Class.forName(dbOraDriver);
conn = DriverManager.getConnection(dbUrl);
}
catch(Exception e) {}
return conn;
}
public boolean verifyLogin(String UserName, String Password) {
boolean loginOk = false;
Connection connect = null;
try {
Statement stmt = connect().createStatement();
String sql = "SELECT count(*) AS cnt FROM users WHERE (username = '" + UserName + "' AND password = '" + Password + "')";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
int cnt = rs.getInt("cnt");
if (cnt > 0) { loginOk = true; }
}
connect.close();
}
catch (Exception e) {}
return loginOk;
}
}
and for my index.jsp
Code:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
--%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="Java" import="javax.naming.*,javax.sql.*,java.sql.*,java.util.*"%>
<jsp:useBean id="Users" scope = "page" class ="biz.Users" />
<jsp:setProperty name ="Users" property = "*" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Customer Management System</title>
</head>
<body>
<form name="form1" method="post" action="login.jsp">
<table border="0" cellspacing="0" cellpadding="2">
<tr>
<td>Username</td>
<td><input name="userName" type="text" size="20" maxlength="20"></td>
</tr>
<tr>
<td>Password</td>
<td><input name="password" type="password" size="20" maxlength="15"></td>
</tr>
<tr>
<td> </td>
<td><input name="btnSubmit" type="submit" id="btnSubmit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
and my login.jsp to be called by index.jsp once the Submit button is click is:
Code:
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>CRMS - Login</title>
</head>
<body>
<jsp:useBean id="Users" scope="page" class="biz.Users" />
<jsp:setProperty name="Users" property="userName" />
<jsp:setProperty name="Users" property="password" />
<form>
<input type='hidden' name='userName' value='<jsp:getProperty name="Users" property="userName" />'/>
</form>
</body>
</html>
Thanks guys! I really appreciate all your help!