您现在的位置: 军旅同心-旅游自驾-军旅文学 >> 读书赏析 >> 学习园地 >> 电脑网络 >> 技术文章 >> 正文
在jsp中用bean和servlet联合实现用户注册、登录
作者:采集员 文章来源:来源于网络 点击数: 更新时间:2005-9-10 14:29:09
册的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","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 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<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()))

上一页  [1] [2] [3] [4] 下一页


更多
免责声明:作品版权归所属媒体与作者所有!!本站刊载此文不代表同意其说法或描述,仅为提供更多信息。如果您认为我们侵犯了您的版权,请告知!本站立即删除。有异议请联系我们。
文章录入:烟灰缸    责任编辑:烟灰缸 
网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
| 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 网站地图 | 版权申明 | 网站公告 | 管理登录 |