Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about pageContext in jsp

 
rai talari
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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!)
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic