• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to access a class in servlet context from JSP

 
ying lam
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ying lam
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic