首页 > 其他分享 >四个常用的域对象

四个常用的域对象

时间:2023-01-08 23:34:45浏览次数:31  
标签:index 常用 对象 req session jsp servletContext 四个

四个常用的域对象

:看了下web课上的PPT,我承认之前太大声了╥﹏╥

四大域对象

常用的四大域,即pageContext,request,session,application

功能:都是存取数据,但对数据的存取范围不同

代码

除xml的配置方式还可以用

注解的方式映射路径

@WebServlet(name = "ActionScope",urlPatterns = "/index")
  • 创建ActionScope类
@WebServlet(name = "ActionScope",urlPatterns = "/index")
public class ActionScope extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("来过此类");
        //        获取session对象
        HttpSession session = req.getSession();
//        获取servletContext对象,即jsp的application对象
        ServletContext servletContext = this.getServletContext();

//        存数据
        req.setAttribute("reqKey","存在request中的数据");
        session.setAttribute("sessionKey","存在session中的数据");
        servletContext.setAttribute("applicationKey","存在servletContext中的数据");
        req.getRequestDispatcher("index.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
  • index.jsp代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>域对象</title>
</head>
<body>
<%
    pageContext.setAttribute("pageContextKey","保存在pageContext对象中的数据");
%>
<%= pageContext.getAttribute("pageContextKey") %> <br>
<%= request.getAttribute("reqKey") %> <br>
<%= session.getAttribute("sessionKey") %> <br>
<%= application.getAttribute("applicationKey") %> <br>
</body>
</html>
  • index02.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>域对象</title>
</head>
<body>
    <%= pageContext.getAttribute("pageContextKey") %> <br>
    <%= request.getAttribute("reqKey") %> <br>
    <%= session.getAttribute("sessionKey") %> <br>
    <%= application.getAttribute("applicationKey") %> <br>
    <a href="index03.jsp">跳转到index03.jsp页面</a>
</body>

</html>
  • index03.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%= pageContext.getAttribute("pageContextKey") %> <br>
<%= request.getAttribute("reqKey") %> <br>
<%= session.getAttribute("sessionKey") %> <br>
<%= application.getAttribute("applicationKey") %> <br>
</body>
</html>
运行tomcat
  • 初始页面

结论:
pageContext存取范围只能在当前jsp页面

  • url栏输入index

得到

  • url栏输入index.jsp

得到

结论:
request只在一次请求内有效

  • url栏输入

得到!

  • 点击跳转index03.jsp

结论:
session和context都能在一次会话范围内有效

  • 关闭浏览器
  • url栏输入index02.jsp

得到

查看index03.jsp

结论:
servletContext整个web工程内都有效

结论

标签:index,常用,对象,req,session,jsp,servletContext,四个
From: https://www.cnblogs.com/yorha/p/17035724.html

相关文章

  • 客服系统前端开发:JavaScript删除对象数组中指定key value的对象【唯一客服】网页在线
    经常我们有这样的需要,比如有一个对象数组,我们要把这个数组里某个对象删除掉,根据他的某一个key的value来删除可以使用JavaScript的filter()方法来删除对象数组中指定k......
  • C#枚举的常用用法
    usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceConsoleApp3{enummyenu......
  • Windchill_二次开发新手入门常用的API
    Windchill_二次开发新手入门常用的API 1.根据零件名称/编码得到该零件   wt.clients.prodmgmt.WTPartHelper.findPartByName(name);   wt.clients.prodm......
  • 请求接口接收不到请求对象
    我在使用Apifox的时候,测试接口,报错Resolved[org.springframework.http.converter.HttpMessageNotReadableException:Requiredrequestbodyismissing:原因是我的接口......
  • HTML_2_常用head标签
    head标签是html组成的一个部分,主要用于配置页面信息。  标题标签:<title>网页标题!</title>编码格式标签:<!--编码配置:html5--><metacharset="UTF-8"><!--编......
  • python常用命令
    使用pip-review库(推荐)安装库pipinstallpip-review检查是否有需要更新的包>pip-reviewscikit-learn==0.23.2isavailable(youhave0.23.1)scipy==1.5.4isavail......
  • 6.2window对象及常用方法
    ​  window对象及常用方法什么是window对象Window对象描述Window对象简单理解就是把浏览器抽象成一个对象,它表示一个浏览器窗口或一个框架。在客户端JavaScript......
  • 6.2window对象及常用方法
    ​  window对象及常用方法什么是window对象Window对象描述Window对象简单理解就是把浏览器抽象成一个对象,它表示一个浏览器窗口或一个框架。在客户端JavaScript......
  • maven过程常用的pom模板
    下面都是要复制的--因为复制到博客就会有格式的转换,就不太注重格式了:<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmln......
  • 6.3BOM中其他常见对象
    ​ location对象location对象,是window对象的一个属性,代表浏览器上URL地址栏,使用location对象可以操作地址栏  <!DOCTYPEhtml><html><head>......