保存作用域有4个:page(页面级别,几乎不再用),request(一次请求响应范围),session(一次会话范围),application(一次应用程序范围)
一. request保存作用域
request保存作用域是一次请求响应范围有效,同一客户端的不同请求,无法获取保存作用域中的值
1. 同一客户端的两次请求(客户端重定向)
package ServletStudy;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/servlet1") //使用注解可以代替在web.xml中配置servlet
public class Servlet1 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.向request保存作用域保存数据
req.setAttribute("name","Tom");
//2.客户端重定向
resp.sendRedirect("/WebStudy_war_exploded/servlet2");
}
}
package ServletStudy;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/servlet2")
public class Servlet2 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取request保存作用域保存的数据
System.out.println(req.getAttribute("name")); //null
}
}
2. 同一客户端的一次请求(服务器内部转发)
package ServletStudy;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/servlet1") //使用注解可以代替在web.xml中配置servlet
public class Servlet1 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.向request保存作用域保存数据
req.setAttribute("name","Tom");
//2.服务器内部转发
req.getRequestDispatcher("/servlet2").forward(req,resp);
}
}
package ServletStudy;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/servlet2")
public class Servlet2 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取request保存作用域保存的数据
System.out.println(req.getAttribute("name")); //Tom
}
}
二. session保存作用域
https://blog.csdn.net/Archer__13/article/details/123769557
session保存作用域是一次会话范围有效,同一客户端在一次会话内的多个请求,都可以获取到session保存作用域内的值。其他客户端访问servlet2,则返回null。
1. 同一客户端的两次请求(客户端重定向)
package ServletStudy;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/servlet1") //使用注解可以代替在web.xml中配置servlet
public class Servlet1 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.setAttribute("name","Tom");
resp.sendRedirect("/WebStudy_war_exploded/servlet2");
}
}
package ServletStudy;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/servlet2")
public class Servlet2 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
System.out.println(session.getAttribute("name")); //Tom
}
}
2.同一客户端的一次请求(服务器内部转发)
package ServletStudy;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/servlet1") //使用注解可以代替在web.xml中配置servlet
public class Servlet1 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.setAttribute("name","Tom");
req.getRequestDispatcher("/servlet2").forward(req,resp);
}
}
package ServletStudy;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/servlet2")
public class Servlet2 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
System.out.println(session.getAttribute("name")); //Tom
}
}
三. application保存作用域
application保存作用域是一次应用程序范围有效,所有客户端的请求,都可以获取到application保存作用域中的值
package ServletStudy;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/servlet1") //使用注解可以代替在web.xml中配置servlet
public class Servlet1 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext application = req.getServletContext();
application.setAttribute("name","Tom");
resp.sendRedirect("/WebStudy_war_exploded/servlet2");
}
}
package ServletStudy;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/servlet2")
public class Servlet2 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext application = req.getServletContext();
System.out.println(application.getAttribute("name")); //Tom
}
}