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. [Javascript]jquery $(document).ready() 与window.onload的区别

    引用:http://www.jb51.net/article/21628.htm Jquery中$(document).ready()的作用类似于传统JavaScript中的window.onload ...

  2. 解决eclipse下pydev的unresolved import的问题

    有些模块,比如PIL,已经装入过,但是在pydev中无法自动提示,甚至有报 unresolved import的问题,虽然不会引起运行时问题,但是无法实现自动提示,还是一件很麻烦的事情.   下面有个 ...

  3. java 编程军规

    军规条例军规一:[避免在程序中使用魔鬼数字,必须用有意义的常量来标识.]军规二:[明确方法的功能,一个方法仅完成一个功能.]军规三:[方法参数不能超过5个]军规四:[方法调用尽量不要返回null,取而 ...

  4. umbraco表单

    view Model定义类 controller 创建Form @HTML.RenderPartial(“”,)

  5. CentOS中ps配合Kill进程的N种方法

    首先,用ps查看进程,方法如下: $ ps -ef  …… smx       1822     1  0 11:38 ?        00:00:49 gnome-terminal smx     ...

  6. python 打包exe注意的问题

    教程百度.谷歌都有,本文讲讲安装时出错的问题. 教程:http://keliang.blog.51cto.com/3359430/661884 1.cxfreeze 找不到路径时: 需要修改Scrip ...

  7. sqlserver跟踪

    本文以实际应用为目的,不在理论方面深究 1.打开跟踪器 或 2.新建跟踪-事件选择-列筛选器,HostName默认不显示,需勾选“显示所有列”,如果希望只跟踪某一客户端,可按下面的设置HostName ...

  8. [转载]在线考试javaScript倒计时

    <script type="text/javascript"> /* ================== hi.baidu.com/coolinc === */ va ...

  9. z-index无效

    1.要想给元素设置z-index样式,必须先让它变成定位元素,说的明白一点,就是要给元素设置一个postion:relative(定位元素:position属性值设置除默认值static以外的元素,包 ...

  10. 9种CSS3炫酷图片展开预览展示动画特效

    详细内容请点击 在线预览立即下载 这是一组共9款CSS3炫酷图片预览展示动画特效插件.css的新特性可以让我们制作出各种炫酷的动画效果.该图片预览展示动画特效就是一个很好的例子,该效果开始时图片堆叠在 ...