about why you must use ${pagecontext.request.contextpath} to get the project path in a JSP page, but not with ${request.contextpath}?

Source: Internet
Author: User

The question here is that both PageContext and request are one of the built-in objects in the JSP, so why not just use ${request.contextpath} to get the project path?

The problem is that the built-in object of the JSP is confused with the built-in object of the El expression.

9 large built-in objects for JSPs:

Application: is an instance of ServletContext that represents the Web project itself that the JSP belongs to

Config: is an instance of ServletConfig that represents the configuration information for the current JSP

Exception: is an instance of java.lang.Throwable that can be used only if Iserrorpage is set to true in the JSP page

An instance of Out:jspwriter that represents the output stream of a JSP page

Page: Represents the pages themselves, not much use

PageContext: Page Context Object

Request: is an instance of HttpServletRequest, representing a single request

Response: is an instance of HttpServletResponse that represents the response of the server

Session: Is an instance of HttpSession, representing a session

11 built-in objects of the El Expression: (el expression format ${expression}, which is a simple way to access data in order to be introduced in a JSP page without using the Java scripting language)

PageContext: The PageContext object representing the page, same as the PageContext object of the JSP

Pagescope: Used to get property values in the page range

Requestscope: Used to get the value of a property within the request scope

Responsescope: Used to get property values in the response range

Sessionscope: Used to get the value of the attribute in the session range

Applicationscope: Used to get property values in the application range

Param and Paramvalues: The parameter value used to get the request

Header and Headervalues: used to get request header information

Cookie: Used to get the value in a cookie

Initparam: Used to get initialization information for a web App

From the comparison above, it is not difficult to find that the El expression has only the PageContext object, and without the request object, the request object is the built-in object of the JSP. The request object is obtained in the El expression only through ${pagecontext.request}, and similarly, the response and session objects are obtained. So when getting the absolute path of the project, it can only be obtained by ${pagecontext.request.contextpath}, which corresponds to the <%=request.getcontextpath ()%> in the Java scripting language

about why you must use ${pagecontext.request.contextpath} to get the project path in a JSP page, but not with ${request.contextpath}?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.