数据共享:OneServlet工作完毕后,将产生的数据交给TwoServlet来使用。
在Servlet规范中提供四种数据共享方案:
注意:全局作用域对象的生命周期贯穿网站整个运行期间
在同一个网站中,将OneServlet中的数据共享给TwoServlet。
OneServlet:
public class OneServlet extends HttpServlet { @Overrite protected void doGet(HttpServletRequest request,HttpResponse response) throws IOException { //1.通过【请求对象】向Tomcat服务器索要【全局作用域对象】 ServletContext application = request.getServletContext();//全局作用域对象名默认都写作application //2.将数据添加到全局作用域对象中作为【共享数据】 application.setAttribute("key1",数据【任意类型】); } }
TwoServlet:
public class TwoServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.通过【请求对象】向服务器索要当前网站中的【全局作用域】对象 ServletContext application = request.getServletContext(); //2.通过指定关键字从【全局作用域对象】中得到对应数据 Object 存储数据的变量 = application.getAttribute("关键字key"); } }
流程:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登录</title> <style type="text/css"> * { font-size: 20px; } </style> </head> <body> <form action="/MyWeb/one"> <table> <tr> <td>用户名:</td> <td><input type="text" name="userName"></td> </tr> <tr> <td>密 码:</td> <td><input type="password" name="passWord"></td> </tr> <tr> <td><input type="submit" value="登录"></td> <td><input type="reset" value="重置"></td> </tr> </table> </form> </body> </html>
package com.tsccg.controller; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @Author: TSCCG * @Date: 2021/08/17 16:29 */ public class OneServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.通过【请求对象】获取请求头中的【请求参数】 String userName = request.getParameter("userName"); String passWord = request.getParameter("passWord"); //2.通过【请求对象】向服务器索要【全局作用域对象】 ServletContext application = request.getServletContext(); //3.将【请求参数】添加到【全局作用域对象】中 application.setAttribute("key1",userName); application.setAttribute("key2",passWord); //4.处理完当前请求后,需要访问TwoServlet //4.1通过当前【请求对象】生成资源文件申请报告对象,将TwoServlet地址传进去 RequestDispatcher report = request.getRequestDispatcher("/two"); //4.2将报告对象发送给服务器 report.forward(request,response); } }
package com.tsccg.controller; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; /** * @Author: TSCCG * @Date: 2021/08/17 16:52 */ public class TwoServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.通过【请求对象】获取【全局作用域对象】 ServletContext application = request.getServletContext(); //2.通过指定关键字【key】获取【全局作用域对象】中的值 String userName = (String)application.getAttribute("key1"); String passWord = (String)application.getAttribute("key2"); //3.将获取到的数据打印在控制台 System.out.println("userName=" + userName); System.out.println("passWord=" + passWord); //4.通过响应对象向服务器索要输出流 PrintWriter out = response.getWriter(); //5.将一个字符串返回给浏览器作为结束标识 out.print("Hello"); } }
在同一个网站中,OneServlet与TwoServlet借助Cookie实现数据共享。
OneServlet
public class OneServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.创建Cookie对象,保存共享数据(当前用户数据) //Cookie对象相当于一个Map,以键值对方式存放数据 //键值对中的key和value都只能是String类型,且key不能为中文 //一个Cookie只能存放一个键值对 Cookie card1 = new Cookie("key1","abc"); Cookie card2 = new Cookie("key2","def"); //2.将Cookie对象写入响应头中,发送给浏览器 response.addCookie(card1); response.addCookie(card2); } }
浏览器/用户接收到响应包:
状态栏 | 200 |
---|---|
响应头 | Cookie:key1="abc";key2="def" |
空白行 | |
响应体 | 处理结果 |
浏览器再次向同一网站发送请求访问TwoServlet,发送的请求包:
请求行 | url:/MyWeb/two; method:get; |
---|---|
请求头 | 请求参数:***; Cookie: key1="abc";key2="def"; |
空白行 | |
请求体 |
TwoServlet:
public class TwoServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.调用请求对象从请求头中获取浏览器返回的Cookie Cookie[] cookieArray = request.getCookies(); //2.循环遍历数据得到每一个Cookie的key和value for(Cookie card : cookieArray) { String key = card.getName();//读取key值--->"key1" String value = card.getValue();//读取value值--->"abc" } } }