标签:pos utf-8 api on() lazy exce pattern sch instance
用户打开一个浏览器到关闭浏览器,这个过程可以称之为会话。
客户端再次访问服务端时,服务端知道客户端曾经来过,称之为有状态会话。
Cookie[] cookies = req.getCookies();//获取cookie
cookie.getName();//获取cookie的key
cookie.getValue();//获取cookie的value
Cookie cookie = new Cookie("lastLoginTime",String.valueOf(System.currentTimeMillis()));//新建cookie
cookie.setMaxAge(24*60*60);//设置cookie有效期设置为1天
resp.addCookie(cookie);//响应给客户端一个cookie
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qing</groupId>
<artifactId>javaweb-cookie-session</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
</dependencies>
</project>
package com.qing.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
/**
* 保存用户上一次访问的时间
*/
public class CookieDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//服务器告诉你,你来的时间,把这个时间封装为一个信件,下次来时,带上信件,服务器就知道你又来了
//简单解决中文乱码问题
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
//Cookie,服务端从客户端获取
Cookie[] cookies = req.getCookies();//这里返回数组,说明cookie可能存在多个
//判断cookie是否存在,第一次访问服务器时,不存在,cookie是由服务器给客户端的
if (cookies == null || cookies.length < 1) {
out.write("这是您第一次访问本站");
} else {
//如果存在,读取cookie,获取cookie中的值
boolean flag = true;
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if ("lastLoginTime".equals(cookie.getName())) {
flag = false;
out.write("您上一次访问本站的时间是:");
long lastLoginTime = Long.parseLong(cookie.getValue());
out.write(new Date(lastLoginTime).toLocaleString());
System.out.println(new Date(lastLoginTime).toLocaleString());
}
}
if (flag) {
out.write("这是您第一次访问本站");
}
}
//服务器给客户端响应一个cookie
Cookie cookie = new Cookie("lastLoginTime",String.valueOf(System.currentTimeMillis()));
//cookie有效期设置为1天
cookie.setMaxAge(24*60*60);
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<servlet>
<servlet-name>cookieDemo01</servlet-name>
<servlet-class>com.qing.servlet.CookieDemo01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cookieDemo01</servlet-name>
<url-pattern>/cookie</url-pattern>
</servlet-mapping>
</web-app>
package com.qing.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class SessionDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决中文乱码问题
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
//得到session
HttpSession session = req.getSession();
//给session存数据
session.setAttribute("name","清风");
//获取session的ID
String id = session.getId();
//判断session是不是新创建的
if (session.isNew()) {
resp.getWriter().write("session创建成功,ID:" + id);
} else {
resp.getWriter().write("session已经在服务器中存在了,ID:" + id);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
package com.qing.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class SessionDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决中文乱码问题
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
//得到session
HttpSession session = req.getSession();
//从session中取数据
String name = (String) session.getAttribute("name");
System.out.println(name);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
package com.qing.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class SessionDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决中文乱码问题
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
//得到session
HttpSession session = req.getSession();
//手动注销session
session.invalidate();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<servlet>
<servlet-name>cookieDemo01</servlet-name>
<servlet-class>com.qing.servlet.CookieDemo01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cookieDemo01</servlet-name>
<url-pattern>/cookie</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>sessionDemo01</servlet-name>
<servlet-class>com.qing.servlet.SessionDemo01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sessionDemo01</servlet-name>
<url-pattern>/session1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>sessionDemo02</servlet-name>
<servlet-class>com.qing.servlet.SessionDemo02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sessionDemo02</servlet-name>
<url-pattern>/session2</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>sessionDemo03</servlet-name>
<servlet-class>com.qing.servlet.SessionDemo03</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sessionDemo03</servlet-name>
<url-pattern>/session3</url-pattern>
</servlet-mapping>
<!--设置session的默认失效时间-->
<session-config>
<!--15分钟后session自动失效,以分钟为单位-->
<session-timeout>15</session-timeout>
</session-config>
</web-app>
<!--设置session的默认失效时间-->
<session-config>
<!--15分钟后session自动失效,以分钟为单位-->
<session-timeout>15</session-timeout>
</session-config>
标签:pos utf-8 api on() lazy exce pattern sch instance
原文地址:https://www.cnblogs.com/wl3pb/p/14552778.html