JavaWeb 小試牛刀(一)
阿新 • • 發佈:2018-08-14
san nts 技術 兩個 username rda public review ger
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) { 11this.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>
JavaWeb 小試牛刀(一)