This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSP and the fly likes how to access a class in servlet context from JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "how to access a class in servlet context from JSP" Watch "how to access a class in servlet context from JSP" New topic
Author

how to access a class in servlet context from JSP

ying lam
Ranch Hand

Joined: May 17, 2004
Posts: 85
Hi,

I set attribute 'msg' in my servletContext, like this, before I dispatch to a JSP.



And In my JSP, I try to access the 'msg' object like this:



But I get an error like this when I run:




Can you please tell me what did i do wrong?

Thank you.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

If the bean is in the context scope, how can you get it from the session scope ?
So the problem lies in the scope.


Hope this helps


SCJP, SCWCD.
|Asking Good Questions|
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

When you do not specify a class attribute, this action expects a scoped variable of the specified name and type to already exist.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Originally posted by seetharaman venkatasamy:
When you do not specify a class attribute, this action expects a scoped variable of the specified name and type to already exist.


It does exist, isn't it?
ying lam
Ranch Hand

Joined: May 17, 2004
Posts: 85
Thank you.

For my case of 'session' scope. Where I should I put my Msg object with the key 'msg' so that JSP can see it when I dispatch to it from the servlet.

Thank you.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61082
    
  66

Unless you have a good reason for it to stay around for the long haul, you should be placing it in request scope. Putting it in session scope is a good way to keep it around for longer than the current request, but you should only do that when necessary. Using application scope (the servlet context) should only be done for information that needs to be shared across al users.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
ying lam
Ranch Hand

Joined: May 17, 2004
Posts: 85
Thank you for all the help.

I have this in my code:



And i have a breakpoint in my msg getTitle() method. It does break (calling from tomcat) and the function does return the right value.

But why in my final page (view source in a browser), i only see

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61082
    
  66

You really should learn to use the EL rather than scriptlets and scriptlet expressions, but your syntax:
is a scriptlet statement. In order to emit a value, you need to use a scriptlet expression, whose syntax would be:

However, again, you should look into modernizing your JSP knowledge with the Expression language (EL).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to access a class in servlet context from JSP