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

//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("https://www.jb51.net/");
   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");
   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:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzxdxg.html