Why there is a need of pageContext in JSP?

前端 未结 3 1447
执念已碎
执念已碎 2020-12-23 12:32

When we can access all the implicit variables in JSP, why do we have pageContext ?

My assumption is the following: if we use EL expressions or JSTL, to acc

3条回答
  •  自闭症患者
    2020-12-23 12:56

    You need it to access non-implicit variables. Does it now make sense?


    Update: Sometimes would just like to access the getter methods of HttpServletRequest and HttpSession directly. In standard JSP, both are only available by ${pageContext}. Here are some real world use examples:


    Refreshing page when session times out:

    
    

    Passing session ID to an Applet (so that it can communicate with servlet in the same session):

    
    

    Displaying some message only on first request of a session:

    Welcome!
    

    note that new has special treatment because it's a reserved keyword in EL, at least, since EL 2.2


    Displaying user IP:

    Your IP is: ${pageContext.request.remoteAddr}

    Making links domain-relative without hardcoding current context path:

    login
    

    Dynamically defining the tag (with a bit help of JSTL functions taglib):

    
    

    Etcetera. Peek around in the aforelinked HttpServletRequest and HttpSession javadoc to learn about all those getter methods. Some of them may be useful in JSP/EL as well.

提交回复
热议问题