java - JSP中pageContext.REQUEST_SCOPE和request对象的区别

标签 java jsp

pageContext.setAttribute("first","value1",pageContext.REQUEST_SCOPE);

request.setAttribute("first","value1");

这两个是相同的还是不同的? JSP中pageContext和Page的作用是什么?

最佳答案

这两行做同样的事情,在请求范围内创建一个新属性。如果您查看 PageContext 类,它有一个方法 initialize ,因为它存储了对 request 对象的引用。因此,当您执行 pageContext.setAttribute("first","value1",pageContext.REQUEST_SCOPE); 时,它会在请求范围内创建一个新属性。

what is the purpose of pageContext and Page in JSP.

pageContext 提供了一些其他有用的方法,例如 findAttribute(继承自 JspContext 的方法),当您不知道哪个属性的范围时可以使用它被储存了。

pageContext 属于 javax.servlet.jsp.PageContext 类型,它是一个抽象类。此类的目的是提供单个对象,该对象为页面作者和容器实现者公开实用方法。根据 API DOC ,

  • Methods Intended for Container Generated Code

Some methods are intended to be used by the code generated by the container, not by code written by JSP page authors, or JSP tag library authors.

The methods supporting lifecycle are initialize() and release()

The following methods enable the management of nested JspWriter streams to implement Tag Extensions: pushBody()

  • Methods Intended for JSP authors

The following methods provide convenient access to implicit objects:

getException(), getPage() getRequest(), getResponse(), getSession(), getServletConfig() and getServletContext().

The following methods provide support for forwarding, inclusion and error handling: forward(), include(), and handlePageException().

关于java - JSP中pageContext.REQUEST_SCOPE和request对象的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31359660/

相关文章:

java - 如何创建在Android后台进程中设置计时器的程序

java - 调用notifyItemChanged后,适配器不会收到通知,并且onBindViewHolder不会被调用

java - Applet 中的图像未显示在网页中

javascript - 如何在 JSP 中的以下场景中跳过双引号而不使用单引号

java - 访问位于 WEB-INF 文件夹中的资源

java - 默认构造函数无法处理隐式 super 构造函数抛出的异常类型 Exception

java - 如何使用 java lambda 流 :Given a list of students having books, 查找所有正在阅读最便宜书籍的学生

java - Android 中的原生 AAC 流媒体

java - 如何使用 Spring Boot 和 WildFly 11.0 配置 JNDI 数据源

java - 没有 Struts 和 Maven 的 JSP Tiles