当前位置:早雪网网络学院编程文档Java → 在jsp中用bean和servlet联合实现用户注册、登录

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

减小字体 增大字体 作者:不详  来源:supcode.com收集整理  发布时间:2005-7-22 18:45:40
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 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:\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=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>Sh

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


Tags:jsp,中用,bean,servlet,联合,实现,用户,注册,登录
[数据载入中...] [返回上一页] [打 印]