快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

澳门威泥斯人:在jsp中用bean和servlet联合实现用户注册、登录



一、数据库设计

用MySQL Control Center打开MySQL数据库,新建数据库sh澳门威泥斯人opping,在其下新建表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;

Statement 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;

}

}

//此措施用于获取大年夜段文本,

//将此中的回车换行调换为

//输出到html页面

public String rs_getHtmlString(String column)

{

try{

String str1 = this.rs.getString(column);

String str2 = "

";

String str3 = "

";

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_us澳门威泥斯人er 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(´

´ + 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.ne澳门威泥斯人wID +"´,´" + 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","imagebear@1澳门威泥斯人63.com"));

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,"username","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

阐明:

1、默认的jdk1.4中并没有servlet包,请至sun公司网页下载servlet.jar,放至jdk目录下的jrelib目录下,并在JCreator中设置jdk处添加servlet.jar包,如图所示:

[img]哀求空间支持[/img]

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

阐明:此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:

Shopping123

恭喜您,注册成功!

编号

会员

密码

E-mail

copyright© 2003 Shopping123

1. 在第一个类中不应加入字符串操作的措施,如replaceAll、rs_getHtmlString等

跟数据库无关的对字符串操作属“对象措施”,应加入到“对象类”中

2.

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 = "";

这些变量既然是弗成改变的,就应设为弗成改变的私有静态变量,而不是public

3. 既然是正式教程,上面这些变量就不应该写逝世在类里面,要用properties文件保存,如下:

应用properties文件可以在变动办事器地址后不从新编译

类的环境下继承应用

在bean所在目录里用纯文本建立 db.properties 文件,内容如下:

sql_driver=org.gjt.mm.mysql.Driver

sql_url=jdbc:mysql://localhost:3306

sql_DBName=shopping

user=sa

pwd=

if(sql_driver == "" || sql_url == "")

{

Properties prop = new Properties();

//连接db.properties文件

try

{

InputStream is = getClass().getResourceAsStream("db.properties");

prop.load(is);

if(is!=null)

is.close();

}

catch(IOException e)

{

System.out.println(" The file ´db.properties´ can´t open ");

return false;

}

读取各项参数

this.sql_driver= prop.getProperty("sql_driver");

this.sql_url= prop.getProperty("sql_url");

this.sql_DBName= prop.getProperty("sql_DBName");

this.user= prop.getProperty("user");

this.pwd= prop.getProperty("pwd");

}

在顶部再引入:

import java.util.*;

import java.io.*;

4. 为了避免单引号激发的非常而禁止用户输入单引号是弗成取的,应在将变量写入sql语句的时刻把该变量的值里面的所有单引号调换成一对一对的单引号

5.

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();

转换中文没有做成措施,弗成取,要尽可能地重用

6. 凡用户输入的字符串都必须在显示的时刻调换某些HTML“关键字”(先这么叫着),

如尖括号、&、双引号、空格

7. 用户注册后不应把密码显示出来,而应该按密码位数显示星号

8. 注册掉败后应显示详尽的差错信息,把所有的差错都显示出来(这此中应有一公共的差错页面)

9. JSP页面应全部被一个try包起来,掉足就转差错页面,以防未知差错使页面显示500

-摘自 http://www.54bk.com/more.asp?name=arfeng&id=12563

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: