标签:add password pass import jsb pat getname red turn
2)使用Javabean来处理
1、建立user类,封装数据。
1 package com.po; 2 //用户类 3 public class users { 4 private String username; 5 private String password; 6 public users(){} 7 public String getUsername() { 8 return username; 9 } 10 public void setUsername(String username) { 11 this.username = username; 12 } 13 public String getPassword() { 14 return password; 15 } 16 public void setPassword(String password) { 17 this.password = password; 18 } 19 20 }
2、建立userdao类,实现对用户登录的逻辑判断
1 package com.dao; 2 3 import com.po.users; 4 5 //用户的业务逻辑类 6 public class usersdao { 7 public boolean userslogin (users u){ 8 if (u.getUsername().equals("admin")&&u.getPassword().equals("admin")){ 9 return true; 10 }else 11 return false; 12 } 13 }
3、在login界面中,建立Javabean两个,设置表单属性匹配完成后,对原来的if语句进行修改。
1 <body> 2 <jsp:useBean id="loginuser" class="com.po.users" scope="page"></jsp:useBean> 3 <jsp:useBean id="usersdao" class="com.dao.usersdao" scope="page"></jsp:useBean> 4 <jsp:setProperty property="*" name="loginuser"/> 5 6 <% 7 request.setCharacterEncoding("utf-8"); 8 9 if (usersdao.userslogin(loginuser)){ 10 String submit=request.getParameter("submit"); 11 if (submit!=null) 12 { 13 session.setAttribute("sessionname",loginuser.getUsername()); 14 } 15 //request.getRequestDispatcher("success.jsp").forward(request, response); 16 response.sendRedirect("success.jsp"); 17 18 19 } 20 else{ 21 response.sendRedirect("failure.jsp"); 22 23 } 24 %> 25 </body>
3)cookie实现登录和保存用户名
1 <body> 2 <% 3 Cookie a[]=request.getCookies(); 4 for (int i=0;i<a.length;i++){ 5 String user=a[i].getName(); 6 if (user.equals("cookieid")){ 7 String name=a[i].getValue(); 8 session.setAttribute("sessionname", name); 9 response.sendRedirect("success.jsp"); 10 } 11 } 12 %> 13 <Center> 14 15 <form method="post" action="login.jsp"> 16 <label for="username">用户名:</label> 17 <input type="text" name="username"><br><br> 18 <label for="password">密码:</label> 19 <input type="password" name="password"><br><br> 20 不保存用户名:<input type="radio" name="myrad" value="0"> 21 保存用户名:<input type="radio" name="myrad" value="60">一分钟 22 <input type="radio" name="myrad" value="3600">一小时 23 <input type="radio" name="myrad" value="86400">一天 24 <input type="radio" name="myrad" value="604800">一周 25 <input type="radio" name="myrad" value="2592000">一月 26 <input type="radio" name="myrad" value="31536000">一年<br><br> 27 <input type="submit" name="submit" value="提交"> 28 29 </form> 30 31 </Center> 32 </body> 33 </html> 34 <body> 35 <% 36 request.setCharacterEncoding("utf-8"); 37 String name=request.getParameter("username"); 38 String pw=request.getParameter("password"); 39 if (name!=null&&pw!=null&&name.equals("admin")&&pw.equals("123456")){ 40 session.setAttribute("sessionname", name); 41 String ch=request.getParameter("myrad"); 42 if (ch!=null){//如果选择了单选button 43 int time=Integer.parseInt(ch); 44 if (time>0){//如果选择的是保存用户名 45 //存入cookie 46 Cookie cc=new Cookie("cookieid",name);//把name存入cookie 47 cc.setMaxAge(time); 48 response.addCookie(cc); 49 } 50 } 51 response.sendRedirect("success.jsp"); 52 } 53 else 54 response.sendRedirect("index.jsp"); 55 %> 56 </body>
标签:add password pass import jsb pat getname red turn
原文地址:https://www.cnblogs.com/twomeng/p/9476390.html