estore商城案例(二)------登錄&添加商品&商品列表(上)


今天繼續寫一下第二個功能:登錄&添加商品&商品列表

1、登錄頁面,很簡單做個表單提交一下就可以了,記住登錄的功能也先簡單寫一下,明天寫filter過濾器會完善自動登錄的功能,以及權限管理.

登錄頁面代碼:

 1 <html>
 2   <head>
 3     
 4     <title>My JSP 'login.jsp' starting page</title>
 5     
 6     <script type="text/javascript">
 7     function validateForm()
 8     {
 9         var username=document.getElementById("username").value;
10         var password=document.getElementById("password").value;
11         
12         if(username==""||password=="")
13             {
14             alert("不能有空");
15             return false;
16             }
17     }
18     </script>
19   </head>
20   
21   <body>
22   <h3>登錄的頁面</h3>
23 <font color="red">${msg }</font>//選擇自動登錄后,下次直接從session中獲取username和pwd
24     <form action="${pageContext.request.contextPath }/login" onsubmit="return validateForm();" method="post">
25         <table>
26             <tr>
27                 <td>用戶名</td>
28                 <td><input type="text" name="username" id="username"></td>
29                 <td id="username_msg"></td>
30             </tr>
31             <tr>
32                 <td>密碼</td>
33                 <td><input type="password" name="password" id="password">
34                 </td>
35                 <td id="password_msg"></td>
36             </tr>
37             <tr>
38                 <td>自動登陸</td>
39                 <td><input type="checkbox" name="autologin" value="on"><!--自動登錄-->
40                 </td>
41             </tr>
42             <tr>
43                 <td></td>
44                 <td><input type="submit" value="登錄"></td>
45             </tr>
46         </table>
47     </form>
48   </body>
49 </html>
View Code

登錄的servlet:

1 public void doPost(HttpServletRequest request, HttpServletResponse response)
 2             throws ServletException, IOException {
 3         String username=request.getParameter("username");
 4         String password=request.getParameter("password");
 5         if(username!=null&&!"".equals(username)&&password!=null&&!"".equals(username))
 6         {
 7             UserService userService=new UserServiceImpl();
 8             User u=new User();
 9             u.setUsername(username);
10             u.setPassword(MD5.getMD5(password));//如果在dao里md5對密碼處理,那么當選擇自動登錄時,cookie獲得的密碼將是md5處理后的,當filter去再次驗證這個cookie的密碼是就會二次md5處理,所以導致這個cookie無法正確驗證
11             u=userService.loginUser(u);
12             if(u==null)
13             {
14 //登錄錯誤
15                 request.setAttribute("msg","用戶名或密碼錯誤");
16                 request.getRequestDispatcher("/login.jsp").forward(request, response);
17                 return;
18             }
19             else{
20 //登錄成功
21                 if("on".equals(request.getParameter("autologin")))
22                 {
23 //用戶選擇了自動登錄
24                     Cookie cookie=new Cookie("autologin",u.getUsername()+"#"+u.getPassword());
25                     cookie.setMaxAge(60*60*24*14);
26                     cookie.setPath("/");
27                     response.addCookie(cookie);
28                 }
29                 else{
30 //用戶沒有選擇自動登錄
31                     Cookie cookie=new Cookie("autologin","");//清除其他的登錄cookie
32                     cookie.setMaxAge(0);
33                     cookie.setPath("/");
34                     response.addCookie(cookie);
35                 }
36                 request.getSession().setAttribute("existUser", u);//創建session
37                 response.sendRedirect(request.getContextPath()+"/index.jsp");
38                 return;
39             }
40         }
41         response.sendRedirect(request.getContextPath()+"/index.jsp");
42     }
43 
44 }
View Code

登錄的業務大概是這樣的:提交表單→首先獲得表單數據,beanutils綁定到user實體中→判斷數據用戶名和密碼有效性→有效,則判斷用戶是否選擇了自動登錄,如果選擇了自動登錄則保存一個cookie來記錄用戶名密碼和一個保存user實體的session.否則僅僅創建一個個session.

這里注意點:一般保存到數據的密碼都是加密的,這里用的是md5加密.本來是直接把form表單中的密碼傳到dao層,然后在進行md5加密的,但是因為后面要做過濾器,實現自動登錄功能的.在過濾器中會根據自動登錄時保存的cookie(用戶名和密碼)數據去數據庫中核對數據的有效性然后返回所有user對象保存給session,這時session保存的密碼已經是md5加密后的密碼了,所以在需要判斷session的時候,這個密碼就會重復md5,所以這里索性就把md5放到serlvet中了,以免出錯.

涉及到的service層和dao層的代碼在前兩篇博客中:

estore商城案例(一)------用戶注冊&郵件激活(上)

estore商城案例(一)------用戶注冊&郵件激活(下)

添加商品涉及到文件上傳,可能涉及到的代碼會多點,所以就放到明天寫了.

明天子啊繼續擼......


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM