什么是session:
服务器会给每一个用户(浏览器)创建一个session对象。
一个Session独占一个浏览器,只要浏览器没有关闭,这个session就存在
用户登录之后,整个网站都可以访问
SessionDemo01
package com.xz.servlet;
import com.xz.pojo.Person;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
/**
* @author 小龚
* @create 2022-09-03 18:32
*/
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",new Person("小赵",18));
//获得Session的ID
String id = session.getId();
//判断Session是否是新创建的
if(session.isNew()){
resp.getWriter().write("Session创建成功,ID:"+id);
}else {
resp.getWriter().write("Session已经在服务器中创建了,ID:"+id);
}
//Session创建的时候做了什么事情
//1.创建一个cookie
//Cookie cookie = new Cookie("JSESSIONID", id);
//2.响应
//resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
SessionDemo02
package com.xz.servlet;
import com.xz.pojo.Person;
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;
/**
* @author 小龚
* @create 2022-09-03 21:41
*/
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();
//获取name
Person person =(Person) session.getAttribute("name");
//判断获取是否为空
if(person!=null){
//前端输出
resp.getWriter().write("成功获取Session的Person");
resp.getWriter().write(person.toString());
String id = session.getId();
resp.getWriter().write("ID:"+id);
//后台输出
System.out.println(person.toString());
}else {
resp.getWriter().write("Session已被注销,需要重新创建Session");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
SessionDemo03
package com.xz.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;
/**
* @author 小龚
* @create 2022-09-03 21:59
*/
public class SessionDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获得Session
HttpSession session = req.getSession();
//删除Session的内容
session.removeAttribute("name");
//手动注销Session
session.invalidate();
//解决中文乱码
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//前端反馈
resp.getWriter().write("Session注销成功");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
Person
package com.xz.pojo;
/**
* @author 小龚
* @create 2022-09-03 21:49
*/
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
web.xml
<servlet>
<servlet-name>SessionDemo01servlet-name>
<servlet-class>com.xz.servlet.SessionDemo01servlet-class>
servlet>
<servlet-mapping>
<servlet-name>SessionDemo01servlet-name>
<url-pattern>/s1url-pattern>
servlet-mapping>
<servlet>
<servlet-name>SessionDemo02servlet-name>
<servlet-class>com.xz.servlet.SessionDemo02servlet-class>
servlet>
<servlet-mapping>
<servlet-name>SessionDemo02servlet-name>
<url-pattern>/s2url-pattern>
servlet-mapping>
<servlet>
<servlet-name>SessionDemo03servlet-name>
<servlet-class>com.xz.servlet.SessionDemo03servlet-class>
servlet>
<servlet-mapping>
<servlet-name>SessionDemo03servlet-name>
<url-pattern>/s3url-pattern>
servlet-mapping>
<session-config>
<session-timeout>1session-timeout>
session-config>
测试
获取Session
读取Session中的信息
注销
再次回到s2中
使用场景:
Cookie
Session
Java Server Pages:Java服务器端页面,也和Servlet一样,用于开发动态Web。
最大特点:
思路:JSP到底是怎么执行的
代码层面
服务器内部工作
tomcat中有一个work目录
IDEA中使用tomcat的会在IDEA的tomcat中产生一个work目录
我电脑的地址
C:\Users\10904\AppData\Local\JetBrains\IntelliJIdea2022.2\tomcat\ebd52463-143f-437f-87b9-23abcf738afb\work\Catalina\localhost\javaweb_session_cookie_war\org\apache\jsp
发现页面转变成了java程序
浏览器向服务器发送请求,不管访问什么资源,其实都是在访问Servlet!
JSP最终也会被转换成一个java类
查看HttpJspBase源码
结论:JSP本质就是一个Servlet
三种方法
访问JSP流程
在JSP页面中:
只要是JAVA代码就会原封不动的输出
如果是HTML代码,就会被转换为
out.write("内容");