记住用户名记住密码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;
- }