記住用戶名記住密碼remember mecookie會話javascript
java web中登陸時如何記住用戶名呢?java
具體思路:mysql
登陸界面以下:

在後臺(Servlet 或struts 的action)中進行判斷,若登陸成功則經過HttpServletResponse 添加一個cookie.web
在登陸的jsp頁面中,經過request 獲取cookie數組,而後遍歷,若找到相應的cookie,則把cookie的value設置到表單的對應文本框中.objective-c
具體代碼以下:sql
登陸的JSP頁面中(核心代碼):mongodb
Html代碼
數據庫
- <body>
- <%
- Cookie[] cookies = request.getCookies();
- String username33="";
- if (cookies != null) {
- for (Cookie c : cookies) {
- /*if ("password22".equals(c.getName())) {
- user.setPassword(URLDecoder.decode(c.getValue(), "utf-8"));
- continue;
- }*/
- if ("userEmail".equals(c.getName())) {
- username33=URLDecoder.decode(c.getValue(), "utf-8");
- break;
- }
- }
- }
- %>
- <script type="text/javascript">
- window.onload=function(){
- var username1='<%=username33 %>';
- //alert("username1:"+username1);
- if(username1){
- if(username1!='' && username1!=null &&username1!=undefined){
- document.getElementsByName("user.username")[0].value=username1;
- }
- }
- }
- </script>
- ...
- </body>
後臺的action:編程
Java代碼
數組
- if (isLogin) {
-
- //保存用戶名(目前是郵箱)
-
- String emaiCookieName = "userEmail";
- HttpServletRequest request=ServletActionContext.getRequest();
- Cookie[] cookies = request.getCookies();
- boolean flag = false;
- Cookie emailCook = null;
- if (cookies != null)
- {
- System.out.println("cookie 不爲空");
- for (Cookie c : cookies)
- {
-
- if (emaiCookieName.equals(c.getName()))
- {
- System.out.println("找到了 "+emaiCookieName);
- System.out.println("cookie的值爲 "+c.getValue());
- try {
- c.setValue(URLEncoder.encode(this.user.getUsername(), "utf-8"));
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- emailCook = c;
- flag = true;
- break;
- }
- }
-
- }
- if (!flag)
- {
- System.out.println("沒有找到 "+emaiCookieName);
-
-
- try {
- emailCook = new Cookie(emaiCookieName, URLEncoder.encode(
- this.user.getUsername(), "utf-8"));
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- }
-
- if (null != emailCook)
- {
- HttpServletResponse response=ServletActionContext.getResponse();
- if(!ValueWidget.isNullOrEmpty(issave) && issave.equalsIgnoreCase("save")){
- emailCook.setMaxAge(10000000);
-
- response.addCookie(emailCook);
- System.out.println("保存cookie");
- }else{
- System.out.println("讓 cookie 失效");
- emailCook.setMaxAge(0);
- response.addCookie(emailCook);
- }
- }
-
-
-
- return Action.SUCCESS;
- }
源代碼見附件
說明:
該項目使用maven 構建;
IDE:eclipse
數據庫:mysql
登陸地址:http://localhost:8080/shop_goods/user/loginInput.action
1
頂
1
踩
分享到:

java 獲取文件大小 | javascript 編程要注意的問題
參考知識庫

Hbase知識庫3874 關注 | 63 收錄

MongoDB知識庫3739 關注 | 271 收錄

區塊鏈知識庫2484 關注 | 90 收錄

Objective-C知識庫3703 關注 | 1209 收錄
評論
4 樓 hw1287789687 2014-10-07
應用場景:
Java代碼 
- /***
- *
- * @param model
- * @param user
- * @param request
- * @param session
- * @return : 返回null,則登陸成功,<br>不然,登陸失敗
- */
- public String loginCommon(Model model,User user,HttpServletRequest request,HttpServletResponse response
- , HttpSession session,String issave){
- if(ValueWidget.isNullOrEmpty(user)||ValueWidget.isNullOrEmpty(user.getUsername())){
- // model.addAttribute("info", "請輸入用戶名.");
- // System.out.println("user is null");
- // return "user/login";
- return "請輸入用戶名.";
- }
- if(user.getUsername().trim().length()<3 || user.getUsername().trim().length()>16){
- // model.addAttribute("info", "請輸入3-16位用戶名字符.");
- // return "user/login";
- return "請輸入3-16位用戶名字符.";
- }
- User user1 = userDao.getByUsername(user.getUsername());
- if(user1==null){
- // model.addAttribute("info", "您輸入的用戶名不存在.");
- // return "user/login";
- return "您輸入的用戶名不存在.";
- }
- if(!user.getPassword().equals(user1.getPassword())){
- // model.addAttribute("info", "您輸入的密碼有誤.");
- // return "user/login";
- return "您輸入的密碼有誤.";
- }
- session.setAttribute(Constant2.SESSION_KEY_LOGINED_USER, user1);//登陸成功的標識有兩個:"user",Constant2.SESSION_KEY_LOGINED_FLAG
- session.setAttribute(Constant2.SESSION_KEY_LOGINED_FLAG, Constant2.FLAG_LOGIN_SUCCESS);//登陸成功的標識有兩個:"user",Constant2.SESSION_KEY_LOGINED_FLAG
- model.addAttribute("user", user1);
- boolean isSave = !ValueWidget.isNullOrEmpty(issave)
- && issave.equalsIgnoreCase("save");
- System.out.println("isSave:"+isSave);
- WebServletUtil.rememberMe(request,response,"userEmail", user.getUsername(), isSave);
- return null;
- }
3 樓 hw1287789687 2014-10-07
hw1287789687 寫道
封裝的方法
Java代碼 
- /***
- * 是否保存cookie
- * @param request
- * @param response
- * @param emaiCookieName
- * @param cookieValue
- * @param isSave : 是否保存用戶名(記住用戶名)
- * @return
- */
- public static Cookie rememberMe(HttpServletRequest request,HttpServletResponse response,String emaiCookieName, String cookieValue,
- boolean isSave) {
- // HttpServletRequest request = ServletActionContext.getRequest();
- Cookie[] cookies = request.getCookies();
- boolean flag = false;
- // Cookie passwordCook = null;
- Cookie emailCook = null;
- if (cookies != null) {
- System.out.println("cookie 不爲空");
- for (Cookie c : cookies) {
- // if (passwordCookieName.equals(c.getName()))
- // {
- // c.setValue(URLEncoder.encode(password, "utf-8"));
- // passwordCook = c;
- // flag = true;
- // continue;
- // }
- if (emaiCookieName.equals(c.getName()) &&(! ValueWidget.isNullOrEmpty(cookieValue))) {
- System.out.println("找到了 " + emaiCookieName);
- System.out.println("cookie的值爲 " + c.getValue());
- try {
- c.setValue(URLEncoder.encode(cookieValue, "utf-8"));
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- emailCook = c;
- flag = true;
- break;
- }
- }
-
- }
-
- // HttpServletResponse response = ServletActionContext.getResponse();
- if (isSave) {
- if (!flag) {
- System.out.println("沒有找到 " + emaiCookieName);
- // passwordCook = new Cookie(passwordCookieName, URLEncoder
- // .encode(password, "utf-8"));
- try {
- emailCook = new Cookie(emaiCookieName, URLEncoder.encode(
- cookieValue, "utf-8"));
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- }
- emailCook.setMaxAge(10000000);
- response.addCookie(emailCook);
- flag=true;
- System.out.println("保存cookie:"+emailCook.getValue());
- } else {
- if (flag) {
- System.out.println("讓 cookie 失效");
- emailCook.setMaxAge(0);
- response.addCookie(emailCook);
- }
- }
-
-
- return emailCook;
- }
使用場景:
Java代碼 
- if(! StringUtil.isNullOrEmpty(resultCode)&&resultCode.equals("0001")){
-
- System.out.println("issave:"+issave);
- boolean isSave = !StringUtil.isNullOrEmpty(issave)
- && issave.equalsIgnoreCase("save");
- System.out.println("isSave:"+isSave);
- Json2Util.rememberMe(emaiCookieName, email, isSave);
- success=true;
- }
2 樓 hw1287789687 2014-10-07
http://hw1287789687.iteye.com/blog/2124945
1 樓 hw1287789687 2014-10-07
封裝的方法
Java代碼 
- /***
- * 是否保存cookie
- * @param request
- * @param response
- * @param emaiCookieName
- * @param cookieValue
- * @param isSave : 是否保存用戶名(記住用戶名)
- * @return
- */
- public static Cookie rememberMe(HttpServletRequest request,HttpServletResponse response,String emaiCookieName, String cookieValue,
- boolean isSave) {
- // HttpServletRequest request = ServletActionContext.getRequest();
- Cookie[] cookies = request.getCookies();
- boolean flag = false;
- // Cookie passwordCook = null;
- Cookie emailCook = null;
- if (cookies != null) {
- System.out.println("cookie 不爲空");
- for (Cookie c : cookies) {
- // if (passwordCookieName.equals(c.getName()))
- // {
- // c.setValue(URLEncoder.encode(password, "utf-8"));
- // passwordCook = c;
- // flag = true;
- // continue;
- // }
- if (emaiCookieName.equals(c.getName()) &&(! ValueWidget.isNullOrEmpty(cookieValue))) {
- System.out.println("找到了 " + emaiCookieName);
- System.out.println("cookie的值爲 " + c.getValue());
- try {
- c.setValue(URLEncoder.encode(cookieValue, "utf-8"));
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- emailCook = c;
- flag = true;
- break;
- }
- }
-
- }
-
- // HttpServletResponse response = ServletActionContext.getResponse();
- if (isSave) {
- if (!flag) {
- System.out.println("沒有找到 " + emaiCookieName);
- // passwordCook = new Cookie(passwordCookieName, URLEncoder
- // .encode(password, "utf-8"));
- try {
- emailCook = new Cookie(emaiCookieName, URLEncoder.encode(
- cookieValue, "utf-8"));
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- }
- emailCook.setMaxAge(10000000);
- response.addCookie(emailCook);
- flag=true;
- System.out.println("保存cookie:"+emailCook.getValue());
- } else {
- if (flag) {
- System.out.println("讓 cookie 失效");
- emailCook.setMaxAge(0);
- response.addCookie(emailCook);
- }
- }
-
-
- return emailCook;
- }