jsp-4 用cookie实现记住密码

这次就有点简单了

基本是jsp-3的代码但是有些修改

public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
User user=new User();
String username=req.getParameter("username");
user.setUsername(username);
String password=req.getParameter("password");
user.setPassword(password); User use=userServive.login(user);
if(use!=null){
String [] ischeck=req.getParameterValues("ischeck");
if(ischeck!=null&&"true".equals(ischeck[0])){
//添加cookie信息
Cookie usernameCookie=new Cookie("username", username);
Cookie passwordCookie=new Cookie("password",password );
usernameCookie.setMaxAge(60*60*24*1);//一天
passwordCookie.setMaxAge(60*60*24*1);
resp.addCookie(usernameCookie);
resp.addCookie(passwordCookie);
}
resp.sendRedirect("loginSuccess.jsp");
}else{
resp.sendRedirect("loginFail.jsp");
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String username="";
String password="";
//取出Cookie
Cookie [] cookie=request.getCookies();
for(int i=0;i<cookie.length;i++){
if(cookie[i].getName().equals("username")){
username=cookie[i].getValue();
}
if(cookie[i].getName().equals("password")){
password=cookie[i].getValue();
}
} %>
<form action="<%=basePath%>/login.do" method="post">
<table border=0 cellpadding=0 cellspacing=0 style="margin:auto;border-collapse:separate; border-spacing:10px;">
<tr align="center">
<td colspan="2">
<label>登录</label>
</td>
</tr>
<tr>
<td>
用户名:
</td>
<td>
<input type="text" name="username" value="<%=username%>">
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input type="password" name="password" value="<%=password%>">
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="ischeck">记住我?
</td>
<td>
&nbsp;&nbsp;<a href="<%=basePath%>/toRegister.do">注册</a>
</td>
</tr>
<tr align="center">
<td>
<input type="submit" value="登录">
</td>
<td>
<input type="reset" value="重置">
</td>
</tr>
</table>
</form> </body>
</html>

我就在纠结的是根据mvc的原则,cookie的操作是不是放在jsp?

jsp-4 用cookie实现记住密码的更多相关文章

  1. 用cookie实现记住密码

    jsp-4 用cookie实现记住密码 这次就有点简单了 基本是jsp-3的代码但是有些修改 public void login(HttpServletRequest req, HttpServlet ...

  2. jquery.cookie.js 操作cookie实现记住密码功能的实现代码

    jquery.cookie.js操作cookie实现记住密码功能,很简单很强大,喜欢的朋友可以参考下.   复制代码代码如下: //初始化页面时验证是否记住了密码 $(document).ready( ...

  3. 记住密码功能 JS结合JQuery 操作 Cookie 实现记住密码和用户名!

    // 记住密码功能 JS结合JQuery 操作 Cookie 实现记住密码和用户名! var username = document.getElementById("username&quo ...

  4. cookie实现记住密码

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  5. JavaWeb学习----Cookie实现记住密码的功能

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  6. Cookie实现记住密码代码

    jsp页面 <%-- Created by IntelliJ IDEA. User: 60590 Date: 2019/11/28 Time: 14:10 To change this temp ...

  7. Cookie实现记住密码的功能

    一.什么是Cookie cookie是一种WEB服务器通过浏览器在访问者的硬盘上存储信息的手段.Cookie的目的就是为用户带来方便,为网站带来增值.虽然有着许多误传,事实上Cookie并不会造成严重 ...

  8. 【原创】js中利用cookie实现记住密码功能

    在登录界面添加记住密码功能,我首先想到的是在java后台中调用cookie存放账号密码,大致如下: HttpServletRequest request HttpServletResponse res ...

  9. node.js平台下,利用cookie实现记住密码登陆(Express+Ejs+Mysql)

    本博文需有node.js+express+mysql入门基础,若基础薄弱,可参考博主的其他几篇node.就是博文: 1.下载Mysql数据库,安装并配置 创建用户表供登录使用: 2.node.js平台 ...

随机推荐

  1. [译]ava 设计模式之职责链

    (文章翻译自Java Design Pattern: Chain of Responsibility) 职责链模式的主要设计思想是为了构建一连串的处理单元,如果阈值满足的话那么这个单元就来处理这个请求 ...

  2. Linux Shell脚本入门--Uniq命令

    uniq uniq命令可以去除排序过的文件中的重复行,因此uniq经常和sort合用.也就是说,为了使uniq起作用,所有的重复行必须是相邻的. uniq语法 [root@www ~]# uniq [ ...

  3. 用批处理文件自动备份文件及文件夹,并自动删除n天前的文件

    原文:用批处理文件自动备份文件及文件夹,并自动删除n天前的文件 ---恢复内容开始--- 下是备份的批处理,添加到"计划任务"中,设定时间自动运行 复制代码 代码如下:@echo ...

  4. mysql 安装后无法登陆mysql的 shell 那mysql&gt;经验:ERROR 1045 (28000): Access denied for user &#39;root&#39;@&#39;localhost‘

    [root@hzswtb2-mpc ~]# mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using pas ...

  5. Android: 自定义Tab样式,一种简单的方式。

    之前看到过论坛里已经有人发过自定义Tab样式的帖子,感觉有些复杂了,这里分享个简单的方法. 1.制作4个9patch的tab样式,可参考android默认的资源 tab_unselected.9.pn ...

  6. 动动手,写个knockout的分页模板

    最近一个项目用ASP.NET + knockout开发,很多列表页面都带分页,于是就有了写一个公共的分页模板的想法. 先把template写好: <script type="text/ ...

  7. 用C++进行简单的文件I/O操作-转自VC知识库

    原文请见 http://www.vckbase.com/index.php/wv/1158 序论 我曾发表过文件输入输出的文章,现在觉得有必要再写一点.文件 I/O 在C++中比烤蛋糕简单多了. 在这 ...

  8. ExtJs之表格控件入门

    extjs的表格功能非常强大,包括了排序,缓存,拖动,隐藏某一列,自动显示行号,列汇总,单元格编辑等实用功能.表格由类Ext.grid.GridPanel定义,继承自Panel,其xtype为grid ...

  9. Step one : 熟悉Unix/Linux Shell 常见命令行 (三)

    3.学会使用一些管理命令 ps/top/lsof/netstat/kill/tcpdump/iptables/dd 端口查看 ps -- process status ps aux  观察程序所有程序 ...

  10. Struts2的拦截器

    拦截器的作用: 拦截器可以动态地拦截发送到指定Action的请求,通过拦截器机制,可以载Action执行的前后插入某些代码,植入我们的逻辑思维. 拦截器的实现原理: 拦截器通过代理的方式来调用,通过动 ...