目录
1. cookie
cookie 在客户端(浏览器)中保持http状态的 信息技术
本质 是浏览器缓存
每次发请求的时候 在请求头中 带给 服务端
常见的应用场景:实现7天免登录
浏览器 F12
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("gbk");
Cookie cookie = new Cookie("0001", "beijing");
response.addCookie(cookie);
response.getWriter().write("learning cookie !");
cookie的获取
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("gbk");
Cookie[] cookies = request.getCookies();
if (cookies.length>0){
for (Cookie cookie : cookies) {
String name = cookie.getName();
String value = cookie.getValue();
System.out.println("key:"+ name);
System.out.println("value:"+ value);
}
}
}
}
使用之前的用户登录项目
实现7天免登录
对于之前的
loginServlet 登录成功添加cookie
// response.getWriter().write("welcome:" + u.getName());
Cookie cookie = new Cookie("userid", u.getId()+"");
cookie.setMaxAge(7*24*3600);
response.addCookie(cookie);
response.sendRedirect("hello");
增加一个CookieServlet是实现/ck接口
package com.msb.control;
import com.msb.entity.User;
import com.msb.service.UserService;
import com.msb.service.impl.UserServiceImpl;
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.sql.SQLException;
/**
* @Auther: jack.chen
* @Date: 2023/10/19 - 10 - 19 - 21:07
* @Description: ${PACKAGE_NAME}
* @version: 1.0
*/
public class CookieServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
this.doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("gbk");
UserService userService = new UserServiceImpl();
Cookie[] cookies = request.getCookies();
if (cookies!=null){
for (Cookie cookie : cookies) {
String name = cookie.getName();
if ("userid".equals(name)){
String userid = cookie.getValue();
// 对 userid 校验
try {
User u = userService.getUserById(Integer.valueOf(userid));
if (u!=null){
response.sendRedirect("hello");
}else{
response.sendRedirect("page");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return;
}
}
response.sendRedirect("page");
}else{
response.sendRedirect("page");
}
}
}
/ck-->/page 未登录
/ck --> /hello 已经登录
2. session
登录成功之后 再访问其他的页面 不需要再带上 用户名密码 就能直接访问
现实的例子:理发店 办理会员卡
第一种方式:给客户留一张 卡片--客户端
第二种方式:之后只要 报卡号 或者手机号--服务端
session是用于解决用户访问不同页面时 带上sessionid
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("sess api do get ");
HttpSession session = request.getSession();
System.out.println("session id:"+session.getId());
response.getWriter().write("learn session");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html:charset=utf-8");
System.out.println("sess api do get ");
HttpSession session = request.getSession();
System.out.println("session id:"+session.getId());
//设置有效期
// session.setMaxInactiveInterval(5);
// 强制失效
// session.invalidate();
session.setAttribute("111", "beijing");
response.getWriter().write("learn session");
}
Object attribute = (String)session.getAttribute("111");
response.getWriter().write("get session attr:"+attribute);
实现登录页面 欢迎xxxx登录
HttpSession session = request.getSession();
session.setAttribute("name", u.getName());
HttpSession session = request.getSession();
String name = (String)session.getAttribute("name");
3. servletcontext
不同用户 数据传递 数据共享 ---当前访问人数 当前帖子的的在线阅读人数
被web项目中所有的servlet访问
服务器创建 一个项目只能有一个servletcontext对象
项目的任意位置 获取到的是同一个对象
该对象所有用户共有
servlet1
ServletContext servletContext = this.getServletContext();
// ServletContext servletContext = this.getServletConfig().getServletContext();
// ServletContext servletContext = request.getSession().getServletContext();
//三种方式获取的都是同一个对象
servletContext.setAttribute("111", "beijing");
response.getWriter().write("servlet context 1");
servlet2
ServletContext servletContext = this.getServletContext();
String attribute = (String)servletContext.getAttribute("111");
System.out.println(attribute);
response.getWriter().write("servlet context 2");
公共设置
web.xml中的公共配置
<context-param>
<param-name>beijing</param-name>
<param-value>beautiful</param-value>
</context-param>
String beijing = (String)servletContext.getInitParameter("beijing");
System.out.println(beijing);
如何实现 当前访问量的统计
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
ServletContext context = this.getServletContext();
Integer num = (Integer) context.getAttribute("num");
if(num==null){
context.setAttribute("num", 1);
}else{
num++;
context.setAttribute("num", num);
}
PrintWriter writer = response.getWriter();
writer.write("<html>");
writer.write("<head>");
writer.write("</head>");
writer.write("<body>");
writer.write("用户访问量:"+context.getAttribute("num"));
writer.write("</body>");
writer.write("</html>");
}
4.servletConfig
每个servlet 单独获取自己的属性配置
ServletConfig servletConfig = this.getServletConfig();
String china = servletConfig.getInitParameter("china");
System.out.println("====="+china+"==");
标签:java,String,request,write,session,cookie,response
From: https://www.cnblogs.com/cavalier-chen/p/17775926.html