aspose file tools*
The moose likes JSP and the fly likes about pageContext in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "about pageContext in jsp " Watch "about pageContext in jsp " New topic
Author

about pageContext in jsp

rai talari
Ranch Hand

Joined: Jan 31, 2006
Posts: 34
hi i found that we cannot access pagecontext objects directly in the methods intialized and defined in the jsp.can any one explain why the pageContext object is not accessed as its scope is entire page ?
[ June 16, 2006: Message edited by: rai talari ]

nothing is impossible in the world even the word impossible sounds as I'M'Possible.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
pageContext variable of type PageContext is available only in _jspService
method. Its not a instance variable rather its a local variable.

When you access pageContext by scriplets that scriptlet will get inserted in _jspService(). That why you don't get any error there.

But when you are going to access pageContext in a user defined method, it won't be available in that method. So error will come at translation time.

Following will work...




Now same pageContext which is created in _jspService() method is also available in meth() method.


Regards


Naseem


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67



Good lord, NO!

This is not thread safe. This will cause any simultaneous threads accessing the JSP to stomp all over each other.

Your best bet is to avoid any class-level declarations at all in JSPs. (Heck, it's best to avoid any Java at all in JSPs!)


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

P.S. If you decide to keep the Java methods in your JSP -- remember, I always advise moving all Java out of the JSPs -- you will need to pass the pageContext as a parameter to the methods.

Again, do not create an instance variable to store it for sharing. That way lies despair and agony.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Thanks Bear for correcting me. I was just thrown by that race condition.

Following code is better than previous one.




Regards


Naseem
[ June 16, 2006: Message edited by: Naseem Khan ]
 
Don't get me started about those stupid light bulbs.
 
subject: about pageContext in jsp