File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Sharing instances of a bean between JSP files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Sharing instances of a bean between JSP files" Watch "Sharing instances of a bean between JSP files" New topic
Author

Sharing instances of a bean between JSP files

Yves Wheeler
Greenhorn

Joined: Feb 22, 2010
Posts: 10
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

Joined: Apr 08, 2004
Posts: 1378
use scope application (ServletContext)
Yves Wheeler
Greenhorn

Joined: Feb 22, 2010
Posts: 10
kri shan wrote:use scope application (ServletContext)


How would I acquire the ServletContext object from the JSP file?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61415
    
  67



[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Yves Wheeler
Greenhorn

Joined: Feb 22, 2010
Posts: 10
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Feb 22, 2010
Posts: 10
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

Joined: Sep 29, 2008
Posts: 12617

Honestly, I'm not interested in what your syntax highlighter says--I'm interested in whether or not it actually *works*.
Yves Wheeler
Greenhorn

Joined: Feb 22, 2010
Posts: 10
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

Joined: Jan 10, 2002
Posts: 61415
    
  67

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

Joined: Feb 22, 2010
Posts: 10
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

Joined: Jan 10, 2002
Posts: 61415
    
  67


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

Joined: Feb 22, 2010
Posts: 10
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

Joined: Jan 10, 2002
Posts: 61415
    
  67

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

Joined: Jan 10, 2002
Posts: 61415
    
  67

You might benefit from reading this article, and then this one.
Yves Wheeler
Greenhorn

Joined: Feb 22, 2010
Posts: 10
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

Joined: Feb 22, 2010
Posts: 10
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

Joined: Feb 22, 2010
Posts: 10
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sharing instances of a bean between JSP files