大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> JSP专区 -> 在jsp中用bean和servlet联合实现用户注册、登录

在jsp中用bean和servlet联合实现用户注册、登录

时间: 2021-07-31 作者:daque

证明:作家原创,版权一切。一经受权,不得连载 在jsp中用bean和servlet联合实现用户注册、登录

作家:imagebear版权:imagebear

本例须要的软硬件和运转情况:1、windows2000 server操纵体例2、jdk1.43、jcreator2.5(java源码编纂调节和测试器,呕血引荐!)4、macromedia jrun mx5、macromedia dreamweaver mx(非必定)6、mysql数据库(最佳安置mysql control center)

一、数据库安排用mysql control center翻开mysql数据库,兴建数据库shopping,在其下兴建表tbl_user,个中各字段树立如次:

二、编写贯穿数据库bean:dbconn.java

//dbconn.java

//include required classesimport 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;  } }

                //此本领用来获得大段文本,                //将个中的回车换行替代为<br>                //输入到html页面 public string rs_gethtmlstring(string column) {  try{   string str1 = this.rs.getstring(column);   string str2 = "\r\n";   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 classesimport 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","imagebear@163.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 classesimport java.io.*;import javax.servlet.*;import javax.servlet.http.*;import java.sql.*;

//class loginpublic 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<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目次下的jre\lib\目次下,并在jcreator中树立jdk处增添servlet.jar包 

2、本servlet用来检查用户名和暗号,若精确则将用户名写入cookie,实行后将暂时页重定向到index.jsp页

 

五、编写检验和测定用户能否仍旧登岸的bean:checklogin.java

//checklogin.java

//import required classesimport java.io.*;import javax.servlet.*;import javax.servlet.http.*;

//class checkloginpublic 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文献夹下,路途为:

在jsp中用bean和servlet联合实现用户注册、登录图1

c:\jrun4\servers\shopping\default-ear\default-war\web-inf\classes\

七、创造jsp文献运用dw,在c:\jrun4\servers\shopping\default-ear\default-war\目次下兴建如次的jsp文献:index.jsp:

<%@ page contenttype="text/html;charset=utf8" pageencoding="gb2312" %><html><head><title>shopping123</title><meta http-equiv="content-type" content="text/html; charset=utf8"><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=utf8" pageencoding="gb2312" %><html><head><title>shopping123</title><meta http-equiv="content-type" content="text/html; charset=utf8"><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=utf8" pageencoding="gb2312" %><%@ page import="reg"%><html><head><title>shopping123</title><meta http-equiv="content-type" content="text/html; charset=utf8"><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> 至此,咱们仍旧实行了一个用户备案、登录的体例。 由于这是自己本人边学边做实行的,以是代码确定有很多不完备的场合,欢送大师品评教正。 之上一切代码均经自己尝试经过。

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.