1.eclipse中tomcate镜像位置:
D:\javaTools\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tem1

2.cookie对象
作用:是能将客户的信息保存在客户端,是以文本的形式保存,生命周期由程序决定,可以很长
使用步骤:
  a.创建一个cookie对象 :Cookie cook = new Cookie("key","value");
  b.可以设置cookie的有效期:cook.setMaxAge();
  c.将该cook对象添加到response对象中,发送(转发和重定向都可以)到前台页面
  d.页面发送请求到服务端会带上cookie对象,服务端可以获取里面的信息来做相应的处理

课堂练习:
  1.用cookie保存用户名,在成功页面获取显示
  2.登录时课选择保存密码状态,下次访问该页面直接登录成功

  login.jsp

 <%@page import="com.pojo.UserInfo"%>
<%@page import="com.service.UserInfoService"%>
<%@page import="com.service.IUserInfoService"%>
<%@page import="java.net.CookieStore"%>
<%@ 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>
<html>
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
Cookie[] cookies = request.getCookies();
IUserInfoService userSer = new UserInfoService();
if(cookies!=null)
for(int i=0;i<cookies.length;i++){
if("username".equals(cookies[i].getName())){
if(cookies[i].getValue()!=null&&!"".equals(cookies[i].getValue())){
//根据用户名查询用户信息
UserInfo user = userSer.getUserByName(cookies[i].getValue());
session.setAttribute("user", user);
response.sendRedirect("success.jsp");
}
}
}
%>
<body>
<form action="cookie/control.jsp" method="post">
用户名:<input name="username"><br>
密码:<input type="password" name="password"><br>
是否记住<input type="checkbox" name="isremder" value="1"><input type="submit" value="登录">
</form>
</body>
</html>

  control.jsp

 <%@page import="com.pojo.UserInfo"%>
<%@page import="com.service.UserInfoService"%>
<%@page import="com.service.IUserInfoService"%>
<%@ 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>
<html>
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String isrember = request.getParameter("isremder");
IUserInfoService userSer = new UserInfoService();
UserInfo user = userSer.login(username, password);
if(user!=null){
session.setAttribute("user",user);
//登录成功,将用户名和密码保存到cookie中
if("1".equals(isrember)){//记住用户名
Cookie cookie = new Cookie("username",user.getUser_name());
cookie.setMaxAge(1000*3600*24*7);//设置cookie的有效期
response.addCookie(cookie);//将cookie添加到响应中发送给客户端
}
response.sendRedirect("success.jsp");
}else{
response.sendRedirect("login.jsp?error=true");
}
%>
</body>
</html>

  success.jsp

<%@page import="com.pojo.UserInfo"%>
<%@ 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>
<html>
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//获取cookie中的值
// Cookie[] cookies = request.getCookies();
// String username = "";
// String password = "";
// if(cookies!=null)
// for(int i=0;i<cookies.length;i++){
// if("username".equals(cookies[i].getName())){
// username = cookies[i].getValue();
// }
// if("password".equals(cookies[i].getName())){
// password = cookies[i].getValue();
// }
// }
//session中获取用户信息
UserInfo user = (UserInfo)session.getAttribute("user"); %>
<h1>欢迎<%=user.getUser_name() %>登录 <a href="">退出登录</a> </h1>
</body>
</html>

  exit.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>
<html>
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
//获取username的cookie
Cookie[] cookies = request.getCookies();
if(cookies!=null)
for(int i=0;i<cookies.length;i++){
if("username".equals(cookies[i].getName())){
cookies[i].setMaxAge(0);
response.addCookie(cookies[i]);//覆盖掉客户端的有效cookie
}
}
response.sendRedirect("login.jsp");
%>
<body> </body>
</html>

4-cookie 简介的更多相关文章

  1. Session & Cookie 简介

    (一)简介 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通 ...

  2. cookie简介

    上例子 1.首先要用php创建cookie发送给客户端,利用setcookie()方法即可 <?php /* * * @Authors peng--jun * @Email 1098325951 ...

  3. session cookie简介

    会话机制:Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端记录信息确定用户身 ...

  4. Session&Cookie 简介及使用

    Cookie cookie 是存储于访问者的计算机中的变量.每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie.你可以使用 JavaScript 或其它语言来创建和取回 cookie ...

  5. cookie技术简介

    Cookie简介 众所周知,HTTP协议是一个无状态的协议.一个Web应用由很多个Web页面组成,每个页面都有唯一的URL来定义.用户在浏览器的地址栏输入页面的URL,浏览器就会向Web Server ...

  6. C#中Cookie的概述及应用

    1.Cookie简介 Cookie 提供了一种在 Web 应用程序中存储用户特定信息的方法.例如,当用户访问您的站点时,您可以使用 Cookie 存储用户首选项或其他信息.当该用户再次访问您的网站时, ...

  7. Asp.net操作cookie大全

    实例代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 3 ...

  8. [转载]JavaEE学习篇之——Session&&Cookie

    原文链接: http://blog.csdn.net/jiangwei0910410003/article/details/23337043 今天继续来看看JavaWeb的相关知识,这篇文章主要来讲一 ...

  9. session与cookie的区别---

    session和cookie的最大区别在于session是保存在服务端的内存中, 而cookie保存与浏览器或客户端文件里面: session是基于访问的进程,记录了一个访问的开始到结束,当浏览器或进 ...

  10. Servlet和JSP读书笔记(三)之Cookie

    一. 浏览器和服务器之间通信的简单介绍引出Cookie和Session(只是简单的简介,不包含协议方面的知识) 1.当我们在浏览器中输入一个地址后,回车后就可以看到浏览器给我们展示的漂亮页面.在这个过 ...

随机推荐

  1. 通过示例学习JavaScript闭包

    译者按: 在上一篇博客,我们通过实现一个计数器,了解了如何使用闭包(Closure),这篇博客将提供一些代码示例,帮助大家理解闭包. 原文: JavaScript Closures for Dummi ...

  2. Laravel5.5 数据库迁移:创建表与修改表

    数据库迁移是数据库的版本管理,要使用数据库迁移,需要在.env文件中连接好数据库(不多说).laravel本身已经存在user表和password_resets表的迁移了,因此,执行 php arti ...

  3. vue-基于elementui换肤

    思路: 生成不同的css颜色文件,每个文件内部命名前加上.custom-颜色值做命名空间. 然后app.vue里引入全部的颜色文件. 用户点击某颜色,就在body加上class:custom-00a5 ...

  4. @meda媒体查询

    定义和使用 使用 @media 查询,你可以针对不同的媒体类型定义不同的样式. @media 可以针对不同的屏幕尺寸设置不同的样式,特别是如果你需要设置设计响应式的页面,@media 是非常有用的. ...

  5. Nginx 集群 反向代理多个服务器

    准备多个服务器,使用 nginx 先做好代理(我这里只有一台服务器,就拷贝两个 tomcat 了,端口分别设置为 8081 和 8082) 1,复制 tomcat cp -r apache-tomca ...

  6. selenium+pyquery爬取淘宝美食100页(无头静默模式)

    import re from selenium import webdriver from selenium.webdriver.common.by import By from selenium.w ...

  7. AIOps背景/所应具备技术能力分析(上)

    本文篇幅较长,分为上,中,下,三个部分进行连载.内容分别为:AIOps 背景/所应具备技术能力分析(上),AIOps 常见的误解(中),挑战及建议(下). 前言 我大概是 5,6 年前开始接触 ITO ...

  8. PE文件基础

    ① PE (Portable Executable):微软参考COFF(Common Object File Format)规范,在Windows NT系统上制定的一种标准, 用于exe可执行文件.o ...

  9. c/c++ const this指针

    const this指针 方法列表后面的const是什么含义呢?答案:不可以在方法里修改成员变量 class Test{ public: void fun()const{ //data = 10;// ...

  10. 浏览器本地数据存储解决方案以及cookie的坑

    本地数据存储解决方案以及cookie的坑 问题: cookie过长导致页面打开失败 背景: 在公司的项目中有一个需求是打开多个工单即在同一个页面中打开了多个tab(iframe),但是需要在刷新时只刷 ...