Servlet_HttpSession对象的特点
Servlet_HttpSession对象的创建
Servlet_获取HttpSession中的数据
Servlet_HttpSession的销毁方式
Servlet_通过HttpSession实现客户端与服务端会话的维持 (welcome那个例子)
Servlet_HttpSession的生命周期
HttpSession与Cookie的区别
HttpSession 就好像一个饭店(客户端服务器)记录客户手机号就知道积分情况,而不需要让客户自己拿着积分卡
package HttpSession05;
/**
* 创建HttpServlet对象
*/
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;
import java.io.PrintWriter;
public class CreateHttpSession01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession(true);
System.out.println(session);
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head><title> hhh </title></head>");
out.println("<dody>");
out.println("hello httpSession");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
}
package HttpSession05;
/**
* 获取HttpSessionServlet的值
*/
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;
import java.io.PrintWriter;
public class GetHttpSessionServlet02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//先获取HttpSession
HttpSession session = req.getSession();
String value = (String)session.getAttribute("key11");
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head><title> hhh </title></head>");
out.println("<dody>");
out.println("Value"+value);
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
}
package HttpSession05;
/**
* 当客户第一次访问该浏览器:欢迎您的访问;第二次及以后;欢迎回来
*/
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
public class WelcomeSessionServlet03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//创建一个HTTPSession 如果为false则已经有session,所以根据session的值是否为空来判断是否是第一次访问
HttpSession session = req.getSession(false);
boolean flag = true;
//session为空则是第一次访问则创建一个session
if(session ==null){
flag = false;
req.getSession();
}
resp.setContentType("text/html;charset:utf-8");
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head><title> hhh </title></head>");
out.println("<dody>");
if(flag){
out.println("welcome to back");
}else{
out.println("welcome to first");
Cookie cookie = new Cookie("first","first");
cookie.setMaxAge(60); //设置持久化响应60s
resp.addCookie(cookie);
}
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
}