1.要建的文档,.java用servlet创建

  2.建一张登陆表格 index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<form name="f1" id="f1" action="<%=path %>/servlet/LoginServlet" method="post">
<table border="0">
<tr>
<td>Username:</td>
<td><input type="text" name="username" value="${un}"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" value="${pw}"></td>
</tr>
<tr>
<td>Autologin:</td>
<td><input type="checkbox" name="auto" value="1"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>

  

  3.建立预备登陆servlet,判断该用户名和密码是否已经存在,若是存在则呈现已经有用户名和密码的值。PrepareLogin.java

package com.amaker.servlet;

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class PrepareLogin extends HttpServlet { /**
*
*/
private static final long serialVersionUID = -8306532188182712167L; /**
* Constructor of the object.
*/
public PrepareLogin() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { Cookie[] cs=request.getCookies();
if(cs!=null&&cs.length>0){
for(int i=0;i<cs.length;i++){
Cookie c=cs[i];
if(c.getName().equals("username")){
String value=c.getValue();
request.setAttribute("un", value);
}
if(c.getName().equals("password")){
String password=c.getValue();
request.setAttribute("pw", password);
} }
}
request.getRequestDispatcher("/index.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doGet(request, response); } public void init() throws ServletException {
// Put your code here
} }

  4.修改index.html中

     <TR>
      <td>Username:</td>
<td><input type="text" name="username" value="${un}"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" value="${pw}"></td>
     </tr>

  

  5.建立登陆的servlet,在提交后转到显示之前输入的显示用户名和密码的页面。LoginServet.java

package com.amaker.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public LoginServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { String username=request.getParameter("username");
String password=request.getParameter("password");
String auto=request.getParameter("auto");
if(auto!=null&&auto.equals("1")){
Cookie c1=new Cookie("username", username);
Cookie c2=new Cookie("password", password);
c1.setMaxAge(60*60*24*7);//保存的时间为一周
c2.setMaxAge(60*60*24*7);
response.addCookie(c1);
response.addCookie(c2); } response.setContentType("text/html");
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>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println("welcome,"+username+":"+password);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}

  

5.servlet cookie自动登录的实例的更多相关文章

  1. cookie自动登录的实现

         cookie自动登录是指把用户登录的信息按期限(自定)保存在客户端,当用户请求登录时判断客户端用没有cookie对象,有的话填充值,否则登录界面的输入框为空,不进行填充.      登录界面 ...

  2. jsp使用cookie自动登录

    Login.jsp <%@ page language="java" import="java.util.*" pageEncoding="ut ...

  3. 爬虫模拟cookie自动登录(人人网自动登录)

    什么是cookie? 在网站中,HTTP请求时无状态的,也就是说即使第一次和服务器连接后并且登录成功后,第二次请求服务器依然不能知道当前请求是谁,cookie的出现就是为了解决这个问题,第一次登陆后服 ...

  4. 如何设计相对安全的cookie自动登录系统

    很多网站登录的时候,都会有一个"记住我"功能,用户可以在限定时间段内免登录, 比如豆瓣.人人.新浪微博等都有这种设计.这种技术其实就是基于 cookie的自动登录, 用户登录的时候 ...

  5. 8、Django实战第8天:session和cookie自动登录机制

    因为http是无状态协议,因此,并不会记录用户的登录状态.在早期,是直接把用户名和密码等信息存储在浏览器的cookie来实现记录用户密码登录. 但是这样存在安全隐患,只要别人登录你的电脑cookie信 ...

  6. session和cookie自动登录机制

    cookie的存储 cookie是浏览器支持的一种本地存储方式.以dict,键值对方式存储. {"sessionkey": "123"} 浏览器会自动对于它进行 ...

  7. Servlet实现Cookie自动登录,并显示保存的用户信息

    转自:https://blog.csdn.net/qq_29612963/article/details/51100565

  8. 第十一篇 session和cookie自动登录机制

    session和cookie的作用和区别可以在网上查到,这里简单说下,我们使用的http协议本身是种无状态的协议,就是说web服务器接收到浏览器的请求,会直接返回相应内容,并不会检查是哪个浏览器,即浏 ...

  9. 使用cookie实现自动登录

    一.从登录——>主页面,进行的过程是,输入 用户名和密码,以及验证码,点击“登录”跳转到Activity.jsp login1.action(跳转到登录页面) /** 跳转到login(有积分排 ...

随机推荐

  1. Cannot instantiate the type HttpClient问题

    看自己动手写爬虫,没想到一上来就跪了. 里面提到用的jar包是apache的http客户端开源项目---HttpClient 就去官网下载了一个版本4.3 当按书上代码敲时 HttpClient ht ...

  2. 30款css3实现的鼠标经过图片显示描述特效

    今天给大家分享30款css3实现的鼠标经过图片显示描述特效,鼠标经过这些图片的时候以非常炫的方式显示图片的描述.为保证最佳的效果,请在 IE10+.Chrome.Firefox 和 Safari 等现 ...

  3. js的Touch事件

    js的touch事件,一般用于移动端的触屏滑动$(function(){document.addEventListener("touchmove", _touch, false); ...

  4. C# 之 判断或设置以管理员身份运行程序

    一.判断程序是否以管理员权限运行 using System.Security.Principal; public bool IsAdministrator() { WindowsIdentity cu ...

  5. bing 输入法,切换简体、繁体快捷键与myeclipse 格式化代码冲突。。

    bing 输入法,切换简体.繁体快捷键与myeclipse 格式化代码冲突...蛋碎 myeclipse 代码格式化快捷键是:ctrl+shift+F,bing输入法简体.繁体切换也是,于是蛋疼的事情 ...

  6. ava SE ---逻辑运算符

      java中有4个逻辑运算符:&与,&& 逻辑与,| 或,|| 逻辑或这些运算符要求操作数和结果值都是布尔型. a&&b   a||b 1) 逻辑与& ...

  7. Web Api 2 用户认证模板解析-----外部用户认证模式

    一般的社交提供商不提供一个Web Service进行身份验证(有很好的理由),而提供一个身份验证的界面,其中包含了某种协议如OpenID(连接)或使用OAuth2认证.这意味着客户端应用必须使用一个浏 ...

  8. SQL查询中的in与join效率比较

    大多数情况下,程序员比较喜欢使用in来查询符合某些条件的数据,最近在查询某个角色有哪些用户的方法中,使用了in语句: ) FROM baseuser AND BaseUser.Id IN (SELEC ...

  9. nodejs 实现 http proxy 透明转发

    这两天需要安装一个Linux虚拟机,接下来要通过 yum 来安装需要的软件. 因单位的网络需要通过代理才能访问外网,公司代理服务器:proxy.xxxx.com,给yum增加了proxy设置: # v ...

  10. PHP trim()函数的一些用法

    string trim ( string $str [, string $charlist ] ) - 去除字符串首尾处的空白字符(或者其他字符) trim()函数当第二个参数为空时,默认去掉空格.制 ...