File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sharing instances of a bean between JSP files

 
Yves Wheeler
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I have a master JSP page which instantiates an application wide bean using the jsp:usebean function, I would like the other JSP files that the master page includes to be able to access the instantiated bean. How do I go about doing that?

Thanks
 
kri shan
Ranch Hand
Posts: 1453
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use scope application (ServletContext)
 
Yves Wheeler
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kri shan wrote:use scope application (ServletContext)


How would I acquire the ServletContext object from the JSP file?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Yves Wheeler
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:


I think you have misread my question, what you have given goes in with the "jsp:useBean" declaration.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The sub-pages now have access to the application-scoped bean. You don't need any further information to access it from the "sub"-pages.
 
Yves Wheeler
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:The sub-pages now have access to the application-scoped bean. You don't need any further information to access it from the "sub"-pages.


The syntax highlighter in my IDE says differently. What is the syntax to acquire the bean declared in the other object, as using jsp:useBean with the name declared in the other will trigger a duplicated bean name error.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Honestly, I'm not interested in what your syntax highlighter says--I'm interested in whether or not it actually *works*.
 
Yves Wheeler
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Honestly, I'm not interested in what your syntax highlighter says--I'm interested in whether or not it actually *works*.

No need to be rude.



 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He's not being rude. He's right.

IDEs are notorious for getting it wrong -- especially in JSP pages.

Try it out! Then see if works or not.
 
Yves Wheeler
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:He's not being rude. He's right.

IDEs are notorious for getting it wrong -- especially in JSP pages.

Try it out! Then see if works or not.


Syntax highlighting in Java has served me well for 4 years. However I am pretty new to JSP, so I'll take your word that IDE's mess up on it. I have executed the code and received this error as stated in my last post.

As shown my last post:-
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well, what is privPolicy? Is it defined in whatever bean siteDetails references?

P.S. At this point, scriptlets shouldn't be used in new JSP code -- they're over 8 years outdated. Is this a legacy application?
 
Yves Wheeler
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Well, what is privPolicy? Is it defined in whatever bean siteDetails references?

P.S. At this point, scriptlets shouldn't be used in new JSP code -- they're over 8 years outdated. Is this a legacy application?


It's a public string held within the siteDetails object declared with:-


It's how I learnt to use a bean in jsp from a few tutorials dotted about the net. What should I be using instead?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yves Wheeler wrote:It's a public string held within the siteDetails object declared with:-

Don't tell us, show us the relevant bean code.

It's how I learnt to use a bean in jsp from a few tutorials dotted about the net. What should I be using instead?

Most of tutorials on the net will be very outdated. Any Java code in a JSP, to include syntax using <%= %> is pre-2002 code and should no longer be used. Rather, you need to learn how to use the EL (Expression Language), and the JSTL (standard tag library).

Also, if you are referencing instance variables directly from your bean, you are using beans incorrectly. You should only be referencing bean properties (exposed with getters and setters).

I'd advise finding more modern tutorials, or grabbing a more modern book on JSP.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might benefit from reading this article, and then this one.
 
Yves Wheeler
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yves Wheeler wrote:
kri shan wrote:use scope application (ServletContext)


How would I acquire the ServletContext object from the JSP file?


Thanks kri shan, using ServletContext seems to have done the trick

 
Yves Wheeler
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Yves Wheeler wrote:It's a public string held within the siteDetails object declared with:-

Don't tell us, show us the relevant bean code.


Also, if you are referencing instance variables directly from your bean, you are using beans incorrectly. You should only be referencing bean properties (exposed with getters and setters).


Yes, very true. That was a nasty habit I picked up from one tutorial. I haven't done much with beans before but I am starting to find them useful.
 
Yves Wheeler
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:You might benefit from reading this article, and then this one.


They look like interesting reads, I will go through them and pick up a thing or two.

Thank you very much.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic