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. float浮动的世界

    loat有四个属性,分别是: float:none:  没有浮动: float:left:  左浮动: float:right: 右浮动: float:inherit:继承父元素的浮动: ------ ...

  2. 解决PHP Redis扩展无法加载的问题(zend_new_interned_string in Unknown on line 0)

    出错代码如下 PHP Warning: PHP Startup: Unable to load 最近在工作中需要使用PHP访问Redis,从https://github.com/phpredis/ph ...

  3. webstorm编辑器相关

    1.怎么去掉webstorm中间那条线? 如图: 2.webstorm 常见快捷键 1.代码导航和用法查询:只需要按着Ctrl键点击函数或者变量等,就能直接跳转到定义:可以全项目查找函数或者变量,还可 ...

  4. 2018-11-06 Visual Studio Code插件-英汉词典初版发布

    VS插件市场地址: 英汉词典 - Visual Studio Marketplace 开源在: program-in-chinese/vscode_english_chinese_dictionary ...

  5. java程序存入数据库中文乱码解决方案

    一.问题描述 背景:代码迁移,ssm框架在插入数据到mysql数据库时,中文乱码.代码中的编码配置没有问题,因为该项目代码以前使用过,没有问题.现在换了数据库,数据库配置也做了修改,统一使用utf8, ...

  6. [新特性]PeopleTools8.54+:PeopleSoft Application Engine新特性

    PeopleTools 8.54 的Application Engine 已经被更新,特别是在AE跟踪设置中有了更多的选项,本文将帮助您了解8.54的新AE特性以及如何使用这些特性. AE trace ...

  7. 自动排版工具——XML自动排版生成工具

    ——支持全球化/多语言/符合W3C标准的XML自动排版工具 Boxth XML/XSL Formatter是专为XML数据或其他结构化数据源自动输出排版文件(如: PDF等)而设计的集数据格式化.版式 ...

  8. Android为TV端助力 http下载视频到指定目录

    public void httpget(String uri){ HttpURLConnection connection = null; FileOutputStream fos = null; F ...

  9. Play 2D games on Pixel running Android Nougat (N7.1.2) with Daydream View VR headset

  10. Mac 电脑如何安装mac os 和win7双系统(win7多分区)

    转载:Mac 电脑如何安装mac os 和win7双系统(win7多分区) 本文主要参考了2篇博文,并通过自己的亲身实践总结的.参考的2篇博文地址: http://wenku.baidu.com/li ...