5.Servlet

5.Servlet

1.域对象概念引入

什么是域对象? 能存储数据、获取数据 并能通过共享以传递数据的对象
能够存储数据、获取数据、传递数据的对象
Servlet三大域对象:
1.Request域:对应HttpServletRequest对象,作用范围是一次请求/请求转发,生命周期是一次请求
2.Session域:对应HttpSession对象,作用范围跨请求,生命周期是一次会话
3.Application域:对应ServeltContext对象,在整个web项目下都是它的作用范围(跨会话),生命周期同这个webapp
不同对象的生命周期,作用范围都不同。
这三个域对象都有的方法:1.setAttribute(name,value) 设置/修改数据的方法 2.getAttribute(name) 获得数据的方法 3.removeAttribute(name) 删除数据的方法。我们主要使用这三个方法来通过域对象实现数据的传递。
Servlet三大域对象

JSP四大域对象:
1.Page域
2.Request域
3.Session域
4.Application域

2.Request域传递数据

有效范围:
一次请求内有效,请求转发时数据可以传递,除此以外该域没有办法实现数据共享。
生命周期:
创建:每发生一次请求创建一个独立的请求域
使用service方法中或者请求转发有效
销毁:一次请求结束,已经向浏览器响应数据
一次请求/一次响应,Tomcat给创建的Request对象和Response对象对所有Servlet来说都是同一个
Request域能传递数据的场景只有请求转发。
请求转发传递数据

Servlet1:

package com.example.demo5;/**
 * @Author:zhoayu
 * @Date:2023/11/6 22:51
 * @Description:com.example.demo5
 * @version:1.0
 */

import javax.servlet.RequestDispatcher;
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;
import java.util.ArrayList;
import java.util.Collections;

/**
 * @ClassName RequestDomain
 * @Description //TODO 
 * @Author zhaoyu
 * @Date 2023/11/6
 */
@WebServlet(urlPatterns = "/requestdomain.do")
public class RequestDomain extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //向request域中添加数据
        ArrayList<String> arrayList = new ArrayList<>();
        Collections.addAll(arrayList,"a","b","c");
        //name:String,value:Object
        req.setAttribute("list",arrayList);
        req.setAttribute("gender","man");
        //覆盖gender->man,修改为gender->woman
        req.setAttribute("gender","woman");
        
        //移除数据
        req.setAttribute("name","xx");
        req.removeAttribute("name");
        
        //请求转发
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("requestdomain2.do");
        //做出转发动作
        requestDispatcher.forward(req,resp);
    }
}

Servlet2:

package com.example.demo5.example;/**
 * @Author:zhoayu
 * @Date:2023/11/6 22:53
 * @Description:com.example.demo5.example
 * @version:1.0
 */

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;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName RequestDomain2
 * @Description //TODO 
 * @Author zhaoyu
 * @Date 2023/11/6
 */
@WebServlet(urlPatterns = "/requestdomain2.do")
public class RequestDomain2 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //从request域中读取数据(我们之前在HttpServletRequest中设置的数据)
        ArrayList<String> list = (ArrayList<String>)req.getAttribute("list");
        System.out.println(list); // [a,b,c]
        System.out.println((String)req.getAttribute("gender")); //woman
        //获取request对象中的参数(http请求携带的)
        System.out.println(req.getParameter("username")); //zy
        System.out.println(req.getParameter("password")); //123
        System.out.println(req.getAttribute("name")); //null
    }
}

如果我们使用响应重定向呢,还能通过HttpServletRequest域对象来传递数据吗?
不行,因为重定向的话,又要向requestdomain2.do单独发起一次新的请求,不是和之前共用的一个请求。

resp.sendRedirect("requestdomain2.do")

3.Session域传递数据

Session域传递数据

Servlet1:

package com.example.demo5;/**
 * @Author:zhoayu
 * @Date:2023/11/6 22:51
 * @Description:com.example.demo5
 * @version:1.0
 */

import javax.servlet.RequestDispatcher;
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;
import java.util.ArrayList;
import java.util.Collections;

/**
 * @ClassName Domain
 * @Description //TODO 
 * @Author zhaoyu
 * @Date 2023/11/6
 */
@WebServlet(urlPatterns = "/session1.do")
public class Domain1 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //向session域中添加数据
        HttpSession session = req.getSession();
        ArrayList<String> arrayList = new ArrayList<>();
        Collections.addAll(arrayList,"a","b","c");
        //name:String,value:Object
        session.setAttribute("list",arrayList);
        session.setAttribute("gender","man");
        //覆盖gender->man,修改为gender->woman
        session.setAttribute("gender","woman");

        //移除数据
        session.setAttribute("name","xx");
        session.removeAttribute("name");
        
        //响应重定向测试一下
        resp.sendRedirect("session2.do");
    }
}

Servlet2:

package com.example.demo5.example;/**
 * @Author:zhoayu
 * @Date:2023/11/6 22:53
 * @Description:com.example.demo5.example
 * @version:1.0
 */

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;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName Domain2
 * @Description //TODO 
 * @Author zhaoyu
 * @Date 2023/11/6
 */
@WebServlet(urlPatterns = "/session2.do")
public class Domain2 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //从session域中读取数据(我们之前在HttpServletRequest中设置的数据)
        HttpSession session = req.getSession();
        ArrayList<String> list = (ArrayList<String>)session.getAttribute("list");
        System.out.println(list); // [a,b,c]
        System.out.println((String)session.getAttribute("gender")); //woman
        System.out.println(session.getAttribute("name")); //null
    }
}

如果我们清掉了cookie(JSESSIONID就也被清掉了),再次请求的话,后端会创建一个新的HttpSession对象,上一个HttpSession对象的生命周期就结束了

4.Application域传递数据

Application域传递数据
Servlet1:

package com.example.demo5;/**
 * @Author:zhoayu
 * @Date:2023/11/6 22:51
 * @Description:com.example.demo5
 * @version:1.0
 */

import javax.servlet.RequestDispatcher;
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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

/**
 * @ClassName Domain
 * @Description //TODO 
 * @Author zhaoyu
 * @Date 2023/11/6
 */
@WebServlet(urlPatterns = "/application1.do")
public class Domain1 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //向application域中添加数据
        ServletContext servletContext = this.getServletContext();
        ArrayList<String> arrayList = new ArrayList<>();
        Collections.addAll(arrayList,"a","b","c");
        //name:String,value:Object
        servletContext.setAttribute("list",arrayList);
        servletContext.setAttribute("gender","man");
        //覆盖gender->man,修改为gender->woman
        servletContext.setAttribute("gender","woman");

        //移除数据
        servletContext.setAttribute("name","xx");
        servletContext.removeAttribute("name");
    }
}

Servlet2:

package com.example.demo5.example;/**
 * @Author:zhoayu
 * @Date:2023/11/6 22:53
 * @Description:com.example.demo5.example
 * @version:1.0
 */

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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName Domain2
 * @Description //TODO 
 * @Author zhaoyu
 * @Date 2023/11/6
 */
@WebServlet(urlPatterns = "/application2.do")
public class Domain2 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        
        //从application域中读取数据(我们之前在HttpServletRequest中设置的数据)
        ArrayList<String> list = (ArrayList<String>)servletContext.getAttribute("list");
        System.out.println(list); // [a,b,c]
        System.out.println((String)servletContext.getAttribute("gender")); //woman
        System.out.println(servletContext.getAttribute("name")); //null
    }
}

换用户/清cookie,换servlet的路径访问,都不影响我们从ServletContext对象中拿到数据,因为ServletContext的对象是跨会话的,生命周期是webapp的起停。
在实际的工作中,一般不会往application域中放数据,因为它的生命周期太长了(项目停止数据才消失),因此很容易放很多数据,不要放用户信息这种,放点配置信息就得了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值