aspose file tools*
The moose likes JSP and the fly likes Using jsp:useBean across JSPs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Using jsp:useBean across JSPs" Watch "Using jsp:useBean across JSPs" New topic
Author

Using jsp:useBean across JSPs

Kumara SharmaS
Greenhorn

Joined: Nov 08, 2011
Posts: 14
Hi all,

I have a problem with 3 JSPs, say A.jsp and B.jsp and A.jsp includes B.jsp

A uses a useBean named 'userinfo' which is picked from the session initialized in C.jsp.
If I directly access A.jsp everything works fine.

Heres the problem:
I have to access both A(B included) and B separately.
If I try to access B.jsp which expects a 'userinfo' object I get JSP compilation errors,
and if provide the same userBean from C.jsp

in B.jsp, B.jsp works fine, but then A.jsp fails to load and says duplicate object: userinfo(one from B and the other from C).

And B does not and cannot include C.jsp, which would have solved the problem.

Is there any solution for this issue?

Thanks
Sharma

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60072
    
  65

Avoid using useBean and set up the beans in the page controllers.

If you don't use page controllers, you should. Please see this article.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
himanshu.harish agrawal
Ranch Hand

Joined: Oct 18, 2010
Posts: 47

Hello Kumara,

Why do you want to have an object of org.ex.UserInfo in both JSP's. If your requirement suffice then I would prefer to have this object created in B.jsp and as you are including it in A.jsp then you would automatically get access to this object from B.jsp. I am not sure about include standard action but include idrective should do the job!

Makes sense?

Regards,
Himanshu
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60072
    
  65

An include will not cause the same object instance to be shared across JSP pages.
himanshu.harish agrawal
Ranch Hand

Joined: Oct 18, 2010
Posts: 47

Bear Bibeault wrote:An include will not cause the same object instance to be shared across JSP pages.


Hello Bear,

I could get the access to object created in included JSP into including JSP, both though include directive and standard action.
I think we can get the access.

Please let me know if I am going in wrong direction.

Regards,
Himanshu
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60072
    
  65

You will have to be clearer about what you are doing.
himanshu.harish agrawal
Ranch Hand

Joined: Oct 18, 2010
Posts: 47

Bear Bibeault wrote:You will have to be clearer about what you are doing.


Precisely, I am creating an object in A.jsp; including it another JSP through include directive and include standard action. And I am able to access the properties of that object in both the cases.
You can try the same and I hope you will get the same results.

In nutshell: We can access the object created in one JSP into another JSP through include directive and include standard action.


Regards,
Himanshu
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60072
    
  65

Of course you can access the property of the created bean in both case, but in no way is that the same instance of the object.
Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

Bear Bibeault wrote:Avoid using useBean and set up the beans in the page controllers.

If you don't use page controllers, you should. Please see this article.

Bear i loved the article thank you for posting.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using jsp:useBean across JSPs
 
Similar Threads
Problem caused by pressing the 'BACK' button on the broswer
doubt on redirect
Changing the URL when using forward or redirect...
jsp:include
page relative vs file relative [ include action vs include directive]