gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 18:03:24 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com