java web中登录时如何记住用户名呢?
具体思路:
登录界面如下:
在后台(Servlet 或struts 的action)中进行判断,若登录成功则通过HttpServletResponse 添加一个cookie.
在登录的jsp页面中,通过request 获取cookie数组,然后遍历,若找到相应的cookie,则把cookie的value设置到表单的对应文本框中.
具体代码如下:
登录的JSP页面中(核心代码):
<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:
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
相关推荐
cookie实现记住用户名密码(IntelliJ IDEA+tomcat)完整项目代码
使用jsp+Servlet+cookie实现一个登录功能,登录时可以选中记住密码。
里面详细记述了如何保持用户登录状态 记住用户名和密码多长时间 非常好用
jsp如何设置cookie和读取cookie 做了一个登录界面,很简单,就两个文本框,两个按钮。我 想使用cookie记住用户名和密码
回显用户名,在login.jsp中通过jsp脚本片段获取记住用户名的cookie,并对用户名解码 可以写任意java代码 //获取用户名cookie Cookie[]cookies=request.getCookies(); Stringusername=""; if(cookies!=null){ for...
jsp java中实现记住密码 把用户名和密码存放到cookie中。
JSP页面内容介绍 index.jsp 登录表单界面,默认勾选十天记住登录 提交表单执行doLogin.jsp逻辑 同时页面加载时需要判断之前是否有记住的登陆状态的选项,即查cookie是否存在 存在则在用户名和密码输入框中自动填入...
DailyFresh(天天生鲜) 天天生鲜是Python中Django框架的一个经典案例,现将其用java的SSM框架实现 ...记住用户名:判断用户是否勾选记住用户名,将信息存在cookie中 主页顶部显示用户欢迎信息:通过判断
request.setAttribute("error","用户名长度必须大于6JSP"); request.getRequestDispatcher("/admin/login.jsp").forward(request,response);//后面语句还会执行 return; //后面的语句不再执行 } // boolean ...
(通过使用Cookie,实现记住首页用户名和密码功能)(4)查看商品。(通过查询MongoDB数据库,返回商品具体信息,并显示在客户端界面)(5)购买商品。(6)购物车。(显示用户购买商品具体信息,删除已购买商品或...