聲明:作者原創,版權所有。未經授權,不得轉載在jsp中以bean和servlet聯合實現用戶註冊、登入
作者:imagebear
版權:imagebear
本例所需的軟體和運作環境:
1、Windows2000 Server作業系統
2、jdk1.4
3、JCreator2.5(java原始碼編輯調試器,吐血推薦!)
4、Macromedia JRun MX
5、Macromedia Dreamweaver MX(非必要)
6、MySQL資料庫(最好安裝MySQL Control Center)
一、資料庫設計用MySQL Control Center開啟MySQL資料庫,新資料庫shopping,在其下方新建表tbl_user,其中各欄位設定如下:
二、編寫連接資料庫bean:DBConn.java
//DBConn.java
//include required classes
import java.sql.*;
//========================================= =
// Define Class DBConn
//==========================================
public class DBConn
{
public String sql_driver = "org.gjt.mm.mysql.Driver";
public String sql_url = "jdbc:mysql://localhost:3306";
public String sql_DBName = "shopping";
public String user = "sa";
public String pwd = "";
Connection conn = null;
州 stmt = null;
ResultSet rs = null;
public boolean setDriver(String drv)
{
this.sql_driver = drv;
return true;
}
public String getDriver()
{
return this.sql_driver;
}
public boolean setUrl(String url)
{
this.sql_url = url;
return true;
}
public boolean setDBName(String dbname)
{
this.sql_DBName = dbname;
return true;
}
public String getDBName()
{
return this.sql_DBName;
}
public boolean setUser(String user)
{
this.user = user;
return true;
}
public String getUser()
{
return this.user;
}
public boolean setPwd(String pwd)
{
this.pwd = pwd;
return true;
}
public String getPwd()
{
return this.pwd;
}
public DBConn()
{
try{
Class.forName(sql_driver);//載入資料庫驅動程式
this.conn = DriverManager.getConnection(sql_url + "/" + sql_DBName + "?user=" + user + "&password=" + pwd + "&useUnicode=true&characterEncoding=gb2312");
this.stmt = this.conn.createStatement();
}catch(Exception e){
System.out.println(e.toString());
}
}
//執行查詢動作
public ResultSet executeQuery(String strSql)
{
try{
this.rs = stmt.executeQuery(strSql);
return this.rs;
}catch(SQLException e){
System.out.println(e.toString());
return null;
}catch(NullPointerException e){
System.out.println(e.toString());
return null;
}
}
//執行資料的插入、刪除、修改操作
public boolean execute(String strSql)
{
try{
if(this.stmt.executeUpdate(strSql) == 0)
return false;
else
return true;
}catch(SQLException e){
System.out.println(e.toString());
return false;
}catch(NullPointerException e){
System.out.println(e.toString());
return false;
}
}
//結果集指標跳到某一行
public boolean rs_absolute(int row)
{
try{
this.rs.absolute(row);
return true;
}catch(SQLException e){
System.out.println(e.toString());
return false;
}
}
public void rs_afterLast()
{
try{
this.rs.afterLast();
}catch(SQLException e){
System.out.println(e.toString());
}
}
public void rs_beforeFirst()
{
try{
this.rs.beforeFirst();
}catch(SQLException e){
System.out.print(e.toString());
}
}
public void rs_close()
{
try{
this.rs.close();
}catch(SQLException e){
System.out.print(e.toString());
}
}
public void rs_deleteRow()
{
try{
this.rs.deleteRow();
}catch(SQLException e){
System.out.print(e.toString());
}
}
public boolean rs_first()
{
try{
this.rs.first();
return true;
}catch(SQLException e){
System.out.print(e.toString());
return false;
}
}
public String rs_getString(String column)
{
try{
return this.rs.getString(column);
}catch(SQLException e){
System.out.println(e.toString());
return null;
}
}
//此方法用於取得大段文本,
//將其中的回車換行替換為<br>
//輸出到html頁面
public String rs_getHtmlString(String column)
{
try{
String str1 = this.rs.getString(column);
String str2 = "rn";
String str3 = "<br>";
return this.replaceAll(str1,str2,str3);
}catch(SQLException e){
System.out.println(e.toString());
return null;
}
}
//把str1字串中的str2字串替換為str3字串
private static String replaceAll(String str1,String str2,String str3)
{
StringBuffer strBuf = new StringBuffer(str1);
int index=0;
while(str1.indexOf(str2,index)!=-1)
{
index=str1.indexOf(str2,index);
strBuf.replace(str1.indexOf(str2,index),str1.indexOf(str2,index)+str2.length(),str3);
index=index+str3.length();
str1=strBuf.toString();
}
return strBuf.toString();
}
public int rs_getInt(String column)
{
try{
return this.rs.getInt(column);
}catch(SQLException e){
System.out.println(e.toString());
return -1;
}
}
public int rs_getInt(int column)
{
try{
return this.rs.getInt(column);
}catch(SQLException e){
System.out.println(e.toString());
return -1;
}
}
public boolean rs_next()
{
try{
return this.rs.next();
}catch(SQLException e){
System.out.println(e.toString());
return false;
}
}
//判斷結果集中是否有數據
public boolean hasData()
{
try{
boolean has_Data = this.rs.first();
this.rs.beforeFirst();
return has_Data;
}catch(SQLException e){
System.out.println(e.toString());
return false;
}
}
public boolean rs_last()
{
try{
return this.rs.last();
}catch(SQLException e){
System.out.println(e.toString());
return false;
}
}
public boolean rs_previous()
{
try{
return this.rs.previous();
}catch(Exception e){
System.out.println(e.toString());
return false;
}
}
//main方法,除錯用
public static void main(String args[])
{
try{
DBConn myconn = new DBConn();
//myconn.setDBName("shopping");
//myconn.DBConn();
//myconn.execute("Insert Into tbl_test(id,name) values('10','shandaer')");
//myconn.execute("Update tbl_test set name='yyyyyyyyyyyy' where id=10");
//myconn.execute("Delete from tbl_test where id=1");
ResultSet rs = myconn.executeQuery("select * from tbl_user order by id desc limit 1");
//boolean hasData = myconn.hasData();
//System.out.println("has data:" + hasData);
//rs.first();
while (myconn.rs.next())
{
int id = myconn.rs_getInt("id") + 1;
System.out.print(id);
System.out.println(myconn.rs_getInt("id") + myconn.rs_getString("name"));
//System.out.println('n' + myconn.rs_getHtmlString("name"));
//System.out.println(myconn.rs.getString("name") + myconn.rs_getInt(1));
}
}catch(Exception e){
System.err.println(e.toString());
}
}
}
宣告:因為使用的是MySQL資料庫,所以需要MySQL資料庫的驅動程式下載後請將org包放到DBConn.java所在目錄下以確保該bean能正常運作
三、編寫使用者註冊的bean:reg.java
//reg.java
//import required classes
import java.sql.*;
public class reg
{
public int newID = 0;
public boolean result = false;
public boolean reg(String username,String password,String confirm,String email)
{
try{
if(!this.checkUser(username))
return false;
if(!this.checkPwd(password))
return false;
if(!this.verifyPwd(password,confirm))
return false;
if(!this.checkEmail(email))
return false;
if(!this.userNotExit(username))
return false;
this.getNewID();
this.result = this.register(username,password,confirm,email);
return this.result;
}catch(Exception e){
System.out.println(e.toString());
return false;
}
}//End boolean reg
public boolean checkUser(String user)
{
try{
if(user.indexOf("'")!=-1)
{
System.out.println("姓名中含有非法字元!");
return false;
}else
return true;
}catch(Exception e){
System.out.println(e.toString());
return false;
}
}
public boolean checkPwd(String pwd)
{
try{
if(pwd.indexOf("'")!=-1)
{
System.out.println("密碼中含有非法字元!");
return false;
}else
return true;
}catch(Exception e){
System.out.println(e.toString());
return false;
}
}
public boolean verifyPwd(String pwd,String confirm)
{
try{
if(!pwd.equals(confirm))
{
System.out.println("兩次輸入的密碼不一致!");
return false;
}else
return true;
}catch(Exception e){
System.out.println(e.toString());
return false;
}
}
public boolean checkEmail(String email)
{
try{
if(email.indexOf("'")!=-1)
{
System.out.println("E-mail中含有非法字元!");
return false;
}else
return true;
}catch(Exception e){
System.out.println(e.toString());
return false;
}
}
public boolean userNotExit(String user)
{
try{
DBConn userDBConn = new DBConn();
userDBConn.executeQuery("select * from tbl_user where name='" + user + "'");
if(userDBConn.rs_next())
{
System.out.println("使用者名稱已存在,請選擇其它的使用者名稱!");
return false;
}else
return true;
}catch(Exception e){
System.out.println(e.toString());
return false;
}
}
public int getNewID()
{
try{
DBConn newIDDBConn = new DBConn();
newIDDBConn.executeQuery("select * from tbl_user order by id desc limit 1");
if(newIDDBConn.rs_next())
{
this.newID = newIDDBConn.rs_getInt("id") + 1;
System.out.println(this.newID);
}else{
this.newID = 1;
}
return this.newID;
}catch(Exception e){
System.out.println(e.toString());
return -1;
}
}
public int getID()
{
return this.newID;
}
public boolean register(String username,String password,String confirm,String email)
{
try{
DBConn regDBConn = new DBConn();
String strSQL = "insert into tbl_user(id,name,pwd,email) values('" + this.newID +"','" + username + "','" + password + "','" + email + " ')";
regDBConn.execute(strSQL);
return true;
}catch(Exception e){
System.out.println(e.toString());
return false;
}
}
public static void main(String args[])
{
try{
reg newreg = new reg();
System.out.println(newreg.reg("sssssssss","ssssss","ssssss"," [email protected] "));
DBConn myconn = new DBConn();
myconn.executeQuery("select * from tbl_user");
while(myconn.rs_next())
{
System.out.println(myconn.rs_getInt("id") + " " + myconn.rs_getString("name") + " " + myconn.rs_getString("pwd") + " " + myconn.rs_getString("email") );
}
System.out.println(newreg.getID());
}catch(Exception e){
System.err.println(e.toString());
}
}
};
說明:
1、該bean檔案應及上文所述DBConn.class檔案放於同一目錄下
2.本例主要研究註冊的過程,其中的Email檢測等方法並不完善,若要應用請自行設計方法
四、編寫使用者登陸的Servlet:login.java
//login.java
//import required classes
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
//class login
public class login extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws IOException,ServletException
{
String username = req.getParameter("username");
String password = req.getParameter("password");
if(this.checklogin(username,password))
{
Cookie mylogin = new Cookie("username",username);
mylogin.setVersion(1);
mylogin.setPath("/");
mylogin.setComment("Your login username");
res.addCookie(mylogin);
}
//Cookie[] myCookies = req.getCookies();
//String nameValue = this.getCookieValue(myCookies,"用戶name","not found");
//PrintWriter out = res.getWriter();
//out.println("username" + ":" + nameValue);
//out.println("Test Cookie Success!");
res.sendRedirect("/index.jsp");
}
public void doPost(HttpServletRequest req,HttpServletResponse res)
throws IOException,ServletException
{
doGet(req,res);
}
public static String getCookieValue(Cookie[] cookies,String cookieName,String defaultValue)
{
for(int i=0;i<cookies.length;i++) {
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName()))
return(cookie.getValue());
}
return(defaultValue);
}
public boolean checklogin(String username,String password)
{
try{
DBConn loginConn = new DBConn();
loginConn.executeQuery("select * from tbl_user where name='" + username + "'");
if(loginConn.rs_next())
{
System.out.println("Connection created!");
if(loginConn.rs_getString("pwd").trim().equals(password))
{
System.out.println(loginConn.rs_getString("name"));
return true;
}
else
{
return false;
}
}
System.out.println("Test Login Success!");
return false;
}catch(Exception e){
System.out.println(e.toString());
return false;
}
}
public static void main(String args[])
{
login mylogin = new login();
System.out.println(mylogin.checklogin("shandong","shandong"));
}
}
說明:
1.預設的jdk1.4中並沒有servlet包,請至sun公司網頁下載servlet.jar,放至jdk目錄下的jrelib目錄下,並在JCreator中設置jdk處添加servlet.jar包
2、本Servlet用於檢驗使用者名稱和密碼,若正確則將使用者名稱寫入Cookie,完成後將目前頁重新導向index.jsp頁
五、編寫偵測使用者是否已經登陸的bean:checkLogin.java
//checkLogin.java
//import required classes
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
//class checkLogin
public class checkLogin
{
public String username = "";
public boolean check(HttpServletRequest req,HttpServletResponse res)
throws IOException,ServletException
{
String cookieName = "username";
Cookie[] myCookies = req.getCookies();
this.username = this.getCookieValue(myCookies,cookieName,"not found");
PrintWriter out = res.getWriter();
if(this.username != null)
{
//out.println("早安," + this.username + "!");
return true;
}else{
out.println("登陸失敗!");
return false;
}
}
public String getUserName()
{
return this.username;
}
public static String getCookieValue(Cookie[] cookies,String cookieName,String defaultValue)
{
for(int i=0;i<cookies.length;i++) {
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName()))
return(cookie.getValue());
}
return(defaultValue);
}
}
說明:此bean偵測cookie中的username,若不為空則說明已登錄,反之說明沒有登入。方法不夠完善,您可以自行擴充。
六、在JRun中建立shopping伺服器開啟JRun Administrator,新建shopping伺服器,這裡連接埠為8101。
將上文所述所有編譯後的class檔案連同org包拷至JRun的shopping伺服器所在目錄中的classes資料夾下,路徑為:
C:JRun4serversshoppingdefault-eardefault-warWEB-INFclasses
七、建立jsp檔案應用DW,在C:JRun4serversshoppingdefault-eardefault-war目錄下新建如下的jsp檔:
index.jsp:
<%@ page contentType="text/html;charset=gb2312" pageEncoding="gb2312" %>
<html>
<head>
<title>Shopping123</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="styles/shoppingstyle.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0">
<jsp:useBean id="checklogin" class="checkLogin" scope="page"/>
<%
boolean login = checklogin.check(request,response);
%>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr bgcolor="#990000">
<td height="80" colspan="5"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="120"> </td>
<td class="caption">Shopping123</td>
<td width="200"> </td>
</tr>
</table></td>
</tr>
<tr>
<td width="200" align="center" valign="top"><table width="100%" height="20" border="0" cellpadding="0" cellspacing="0">
<tr>
<td> </td>
</tr>
</table>
<%
if(!login){
%>
<table width="90%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<form name="form1" method="post" action="/servlet/login">
<tr align="center" bgcolor="#CCCCCC">
<td height="30" colspan="2" class="deepred">賣場入口</td>
</tr>
<tr>
<td width="50%" height="24" align="center" bgcolor="#FFFFFF">會員</td>
<td align="center" bgcolor="#FFFFFF"><input name="username" type="text" id="username" size="10"></td>
</tr>
<tr>
<td height="24" align="center" bgcolor="#FFFFFF">密碼</td>
<td align="center" bgcolor="#FFFFFF"><input name="password" type="text" id="password" size="10"></td>
</tr>
<tr>
<td height="24" align="center" bgcolor="#FFFFFF"><a href="reg.jsp" target="_blank" class="red">註冊</a></td>
<td align="center" bgcolor="#FFFFFF"><input type="submit" name="Submit" value="進入"></td>
</tr>
</form>
</table>
<%
}
else
{
out.println("您好," + checklogin.getUserName() + "!");
}
%>
</td>
<td width="1" valign="top" bgcolor="#CCCCCC"></td>
<td width="400"> </td>
<td width="1" valign="top" bgcolor="#CCCCCC"></td>
<td width="200"> </td>
</tr>
<tr align="center" bgcolor="#990000">
<td height="60" colspan="5" class="white">copyright© 2003 Shopping123</td>
</tr>
</table>
</body>
</html>
reg.jsp<%@ page contentType="text/html;charset=gb2312" pageEncoding="gb2312" %>
<html>
<head>
<title>Shopping123</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="styles/shoppingstyle.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0">
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr bgcolor="#990000">
<td height="80" colspan="5"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="120"> </td>
<td class="caption">Shopping123</td>
<td width="200"> </td>
</tr>
</table></td>
</tr>
<tr>
<td width="100" align="center" valign="top"> </td>
<td width="1" valign="top"></td>
<td width="400" align="center" valign="top"><table width="100%" height="20" border="0" cellpadding="0" cellspacing="0">
<tr>
<td> </td>
</tr>
</table>
<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<form action="regpost.jsp" method="post" name="form1">
<tr align="center">
<td height="30" colspan="2" bgcolor="#CCCCCC" class="deepred">會員註冊</td>
</tr>
<tr>
<td width="50%" height="24" align="center" bgcolor="#FFFFFF">會員</td>
<td align="center" bgcolor="#FFFFFF"><input name="username" type="text" id="username" size="16"></td>
</tr>
<tr>
<td width="50%" height="24" align="center" bgcolor="#FFFFFF">密碼</td>
<td align="center" bgcolor="#FFFFFF"><input name="password" type="password" id="password" size="16"></td>
</tr>
<tr>
<td width="50%" height="24" align="center" bgcolor="#FFFFFF">驗證密碼</td>
<td align="center" bgcolor="#FFFFFF"><input name="confirm" type="password" id="confirm" size="16"></td>
</tr>
<tr>
<td width="50%" height="24" align="center" bgcolor="#FFFFFF">E-mail</td>
<td align="center" bgcolor="#FFFFFF"><input name="email" type="text" id="email" size="16"></td>
</tr>
<tr>
<td width="50%" height="24" align="center" bgcolor="#FFFFFF"><input type="submit" name="Submit" value="重寫"></td>
<td align="center" bgcolor="#FFFFFF"><input type="submit" name="Submit2" value="註冊"></td>
</tr>
</form>
</table></td>
<td width="1" valign="top"></td>
<td width="100"> </td>
</tr>
<tr align="center" bgcolor="#990000">
<td height="60" colspan="5" class="white">copyright© 2003 Shopping123</td>
</tr>
</table>
</body>
</html>
regpost.jsp:註冊表單提交頁面<%@ page contentType="text/html;charset=gb2312" pageEncoding="gb2312" %>
<%@ page import="reg"%>
<html>
<head>
<title>Shopping123</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="styles/shoppingstyle.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0">
<%
String username = new String(request.getParameter("username").getBytes("ISO8859_1")).trim();
String password = new String(request.getParameter("password").getBytes("ISO8859_1")).trim();
String confirm = new String(request.getParameter("confirm").getBytes("ISO8859_1")).trim();
String email = new String(request.getParameter("email").getBytes("ISO8859_1")).trim();
%>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr bgcolor="#990000">
<td height="80" colspan="5"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="120"> </td>
<td class="caption">Shopping123</td>
<td width="200"> </td>
</tr>
</table></td>
</tr>
<tr>
<td width="100" align="center" valign="top"> </td>
<td width="1" valign="top"></td>
<td width="400" align="center" valign="top">
<table width="100%" height="20" border="0" cellpadding="0" cellspacing="0">
<tr>
<td> </td>
</tr>
</table>
<jsp:useBean id="regID" class="reg" scope="session"/>
<%
if(regID.reg(username,password,confirm,email))
{
out.print("ok");
String newID = regID.getID() + "";
%>
<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr align="center">
<td height="30" colspan="2" bgcolor="#CCCCCC" class="deepred">恭喜您,註冊成功! </td>
</tr>
<tr>
<td width="50%" height="24" align="center" bgcolor="#FFFFFF">編號</td>
<td align="center" bgcolor="#FFFFFF"><%=newID%></td>
</tr>
<tr>
<td width="50%" height="24" align="center" bgcolor="#FFFFFF">會員</td>
<td align="center" bgcolor="#FFFFFF"><%=username%></td>
</tr>
<tr>
<td width="50%" height="24" align="center" bgcolor="#FFFFFF">密碼</td>
<td align="center" bgcolor="#FFFFFF"><%=password%></td>
</tr>
<tr>
<td width="50%" height="24" align="center" bgcolor="#FFFFFF">E-mail</td>
<td align="center" bgcolor="#FFFFFF"><%=email%></td>
</tr>
</table>
<%
out.print("<br>");
out.print("<a href=javascript:window.close()>關閉</a>");
}else{
out.print("註冊失敗!<br>");
out.print("該使用者名稱已有人使用,請使用另外的使用者名稱!");
out.print("<a href=javascript:history.go(-1)>回傳</a>");
}
%>
</td>
<td width="1" valign="top"></td>
<td width="100"> </td>
</tr>
<tr align="center" bgcolor="#990000">
<td height="60" colspan="5" class="white">copyright© 2003 Shopping123</td>
</tr>
</table>
</body>
</html>
至此,我們已經完成了一個使用者註冊、登入的系統。 因為這是本人邊學邊做完成的,所以程式碼一定有很多不完美的地方,歡迎大家批評指正。 以上所有程式碼均經本人測試通過。