Cookie中用户登录信息的提示

public class LoginServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");

request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
System.out.println("调用后台实现正常登录的功能:"+name+","+pwd);

//下面实现显示用户上次登录的功能
String showTime = func(request,response);
out.println(name+","+showTime);

out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

//模块:实现显示用户上次登录的功能
private String func(HttpServletRequest request, HttpServletResponse response) {
String showTime="您最近是第一次登录!";
//所获客户端的所有cookie
Cookie cs[] = request.getCookies();
if(cs!=null){
for(Cookie c:cs){
if(c.getName().equals("loginTime")){
String str = c.getValue();
long tm = Long.parseLong(str);
Date d = new Date(tm);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
showTime = "您上次登录时间为: "+sdf.format(d);
break;
}
}
}

//不管之前是否已经存在该cookie,都要重新添加一次,因为:若没有则要添加新的,若已有则要更新旧的(把新的登录时间记录进去)
//添加一个新的 或 修改旧的 用于记录当前登录时间的cookie---loginTime
Date d = new Date();
long tm = d.getTime();
Cookie coo = new Cookie("loginTime",""+tm);
coo.setMaxAge(60*60*24*30);//秒为单位 有效期为:1个月
coo.setPath("/"); //设为项目根目录,这样我们项目中所有servlet或jsp都可以访问。但其它项目是不能访问我们的cookie的
response.addCookie(coo); //添加即是写到客户端

return showTime;
}

------------------------------------------------------------------------------------------------------------------------------------------------

<h3>用cookie技术实现显示用户上次登录时间</h3>
<form action="LoginServlet" method="post">
姓名:<input type="text" name="name"/> <br/>
密码:<input type="password" name="pwd"/> <br/>
<input type="submit" value="登录"/>
</form>

时间: 2024-07-31 08:09:54

Cookie中用户登录信息的提示的相关文章

Cookie中用户登录信息登录验证

public class FormServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletR

使用cookie保存用户登录信息

写入Cookie HttpCookie _cookie = new HttpCookie("User"); _cookie.Values.Add("UserName", login.UserName); _cookie.Values.Add("Password", login.Password); Response.Cookies.Add(_cookie); 读取Cookie HttpCookie _cookie = httpContext.Re

.NET跨平台之旅:ASP.NET Core从传统ASP.NET的Cookie中读取用户登录信息

小分享:我有几张阿里云优惠券,用券购买或者升级阿里云相应产品最多可以优惠五折!领券地址:https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=ohmepe03 在解决了asp.net core中访问memcached缓存的问题后,我们开始大踏步地向.net core进军--将更多站点向asp.net core迁移,在迁移涉及获取用户登录信息的站点时,我们遇到了一个问题--如何在asp.net core

Oracle 中记录用户登录信息

我们可以使用 Oracle Audit 函数来记录用户登录信息,但是如果开放了 Audit 函数将会使 Oracle 性能下降,甚至导致 Oracle 崩溃.那我们如何才能记录用户登录信息呢?其实我们可以通过建立触发器的方式来实现.方法如下: 1. 用 sys 用户登录 Oracle2. 创建记录用户登录信息的表CREATE TABLE LOG$INFORMATION (    ID        NUMBER(10),    USERNAME  VARCHAR2(30),    LOGINTI

把discuzX 的用户登录信息添加到纯静态页面

把discuzX 的用户登录信息添加到纯静态页面 转:http://cq6.com/forum.php?mod=viewthread&tid=86560&extra=page%3D1&page=1 分享&收藏 花了几个小时的间,终于把discuzX 的用户登录信息加到纯静态页面了:演示地址:http://cq6.com/bm/ 源码:1. zx.php (存放在网站根目录) <?php require './source/class/class_core.php';

php使用session来保存用户登录信息

php使用session来保存用户登录信息 使用session保存页面登录信息 1.数据库连接配置页面:connectvars.php <?php//数据库的位置define('DB_HOST', 'localhost');//用户名define('DB_USER', 'root');//口令define('DB_PASSWORD', '19900101');//数据库名define('DB_NAME','test') ;?> 2.登录页面:logIn.php <?php//插入连接数据

Asp.Net MVC记住用户登录信息 下次登录无需输入密码

有的时候做网站,就需要记住用户登录信息,下次再登录网站时,不用重复输入用户名和密码,原理是浏览器的cookie把状态给记住了! 那么具体是怎么实现的呢?下面博主将一部分代码贴出来,想要完整版的Demo可以到百度云和码云下载, 百度云下载链接:https://pan.baidu.com/s/1rfQNxpM8WGxkBbdiS1mLeQ    密码:if1q 马云下载链接:https://gitee.com/WuFengZui/RememberLoginDemo    [没有下载链接的都是耍流氓 

系统管理与用户登录信息查看命令(二)

关机: halt,poweroff,shutdown,init 0 重启: reboot,shutdown, init 6 用户登录信息查看:whoami,who,w,last,users 2.1.halt 这个命令相当于直接拔电源关机,不管你目前的系统状态如何,进行硬件强制关机.一般不建议使用. 2.2.poweroff poweroff意思是立即关机,跟shutdown -h now效果一样,在系统启动级别为3的情况下,不建议使用. 2.3.shutdown 功能:关机 语法:shutdow

SourceTree不出现用户登录窗口,提示错误fatal: unable to access&#39;...&#39;; error setting certificate verify locations

SourceTree不出现用户登录窗口,提示错误fatal: unable to access'...'; error setting certificate verify locations; ...\Git\mingw64\libexec\ssl\certs 去查看这个这个目录下的文件是否存在,不存在则放到对应的地方即可. SourceTree不出现用户登录窗口,提示错误fatal: unable to access'...'; error setting certificate verif