未经作者允许,不得转载

第一cookie技术

常用方法:

  • new Cookie(),构造一个cookie
  • getName() ,获取cookie的名字
  • getValue () ,取到具体cookie的值
  • setMaxAge(), 设置cookie过期时间 单位为秒
  • getpath() ,取cookie路径

    -需要注意的地方:

    setMaxAge();当值为0时,即为删除本地cookie文件

    还要注意cookie文件path路径必须一致

    代码事例:
package myCookie;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; @WebServlet(urlPatterns = "/mycookie")
public class myCookieDemo extends HttpServlet { @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// super.doGet(req, resp);
doPost(req, resp);
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// super.doPost(req, resp);
// 通用设置页面编码
resp.setContentType("text/html;charset=UTF-8");
resp.setCharacterEncoding("UTF-8");
// 控制文字输出流
PrintWriter writer = resp.getWriter();
Cookie[] cookies = req.getCookies();
if (cookies != null) {
writer.write("您上次访问的时间是:");
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookie.getName().equals("lasttime")) {
String value = cookie.getValue();
Date date = new Date(Long.parseLong(value));
writer.println("cookie的路径"+cookie.getPath());
writer.println(date.toString());
}
} } else {
writer.print("欢迎第一次访问本页面!");
}
//向系统增加cookie
Cookie cookie = new Cookie("lasttime", System.currentTimeMillis() + "");
//让session由内存写入到硬盘中 单位时间为 s秒 一天 60*60*24
cookie.setMaxAge(20);
resp.addCookie(cookie);
} }

  • 设置中文编码问题
  • -

    //cookie中采用中文编码问题 Urlencoder处理
new Cookie("username", URLEncoder.encode("亮剑精神", "UTF-8"));
///解码
URLEncoder.encode("cookie[i].getValue()", "UTF-8");

地址重写

应用方向:在浏览器不允许使用cookie时,那么就可以采用这个办法进行地址的重写

重要方法:resp.encodeURL 之前必须初始化会话 getSession()

注:浏览器默认会使用含有cookie的办法使用 代码测试很多次数没有成功,必须关闭浏览器cookie设置

package Urlencode;

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns="/myencode")
public class myEncode extends HttpServlet{ @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// super.doGet(req, resp);
doPost(req, resp);
} @SuppressWarnings("deprecation")
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//super.doPost(req, resp);
resp.setContentType("text/html;charset=UTF-8");
resp.setCharacterEncoding("UTF-8");
PrintWriter writer = resp.getWriter();
//必须手动初始化回话
req.getSession();
String str = resp.encodeURL("/SessionTchCookies/index.jsp");
String str2 = resp.encodeURL("/SessionTchCookies/error.jsp");
String url = resp.encodeRedirectURL("/SessionTchCookies/index.jsp"); writer.print("<a href='" + url + "'>购买</a><br/>");
writer.print("<a href='" + str2 + "'>结账</a>");
writer.close(); } }


关于重写技术在很多框架都有实现

  • struts2 <html:rewrite action='logout'/> html:rewrite
  • 类似的还有 <html:link/> <html:from />

  • jstl中 :<c:url /> 来进行地址重写


表单隐藏技术的实现

5.表单隐藏域
<form action="" method="">
相关的表单控件... <input type="hidden" name="参数名1" value="参数值1"/>
...
<input type="hidden" name="jsessionid" value="xxxxx"/>
<input type="hidden" name="参数名n" value="参数值n"/>
</form>

会话的实现

充电类:httpservletsession . getSession() 取得会话对象

setAttribute(“属性名”,”属性值”) 根据属性名设值

getAttribute(“属性名”) 根据属性名 获取对应的值

removeAttribute(“属性名”) 根据属性名删除

getId():获取Session唯一的ID

invalidate():使HttpSession对象失效

setMaxInactiveInterval(时间):设置Session过期时间,单位是秒

getCreationTime():获取HttpSession对象创建时间

getLastAccessedTime():获取HttpSession最近一次请求时间

<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>??</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">
</head>
<body>
首页
<%
out.print("会话是isnew"+session.isNew());
out.print(session.getCreationTime());
/* 设置会话的过期时间 在控制的时候建设
方法1:可以在多个页面进行传值控制 set** get***
方法2:可以在设置一个相对时间比较长的值 然后在不同的页面进行时间属性的更新
*/
session.setMaxInactiveInterval(60);
//手动控制使session过期
session.invalidate(); %>
</body>
</html>

最新文章

  1. [LeetCode] Palindrome Partitioning II 拆分回文串之二
  2. C#最简单例子
  3. tornado 学习笔记1 引言
  4. ubuntu中启用ssh服务
  5. bean标签
  6. js 函数-Tom
  7. Codeforces Gym 100015G Guessing Game 差分约束
  8. DotNET 开发常用工具汇集
  9. 转换成CSV文件、Word、Excel、PDF等的方法--读取CSV文件的方法
  10. Delphi 中 FindWindow 和 FindWindowEx 找到外部进程,然后发送消息(比如最大化)
  11. Oracle sql优化之分析函数优化标量子查询
  12. 【转】IntelliJ IDEA中文乱码问题
  13. PHP——秒数转换为时分秒
  14. elasticSearch 2.3 delete-by-query plugin
  15. 南大算法设计与分析课程OJ答案代码(3)
  16. sed 正则的一个小问题
  17. sorted 返回字典的所有键
  18. BZOJ.2816.[ZJOI2012]网络(LCT)
  19. hdu2896之AC自动机
  20. Linux-eval

热门文章

  1. 201521123028 《Java程序设计》第14周学习总结
  2. 深入浅出数据结构C语言版(19)——堆排序
  3. mongodb 在windows下面进行分片
  4. kvm 虚拟化的使用
  5. EntityFramework 6.x多个上下文迁移实现分布式事务
  6. JVM(五)内存(Heap)分配
  7. js时间戳和日期字符串相互转换
  8. E - 钱币兑换问题
  9. Java 中的语法糖
  10. 机器学习-KNN分类器