Meaningless Drivel is fun!*
The moose likes JSP and the fly likes Bean will not remember values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Bean will not remember values" Watch "Bean will not remember values" New topic
Author

Bean will not remember values

Anders Boje Larsen
Greenhorn

Joined: Jul 15, 2010
Posts: 16
Hello Experts
I have a JSP page that when you open it runs a method which set some methods which are called the same JSP page with getDK() (true / false), etc. Like a normal bean.

My problem is when I call setDK(true) from the Java class it work fine, BUT on the JSP page is the value "null", it do not remembers.

Please help me

Here are some vode.

===================================
JSP PAGE
===================================

=============================
JAVA CODE "adgang"
=============================

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What do you mean, "it doesn't remember"? It's a page scope bean. After you're off the page that bean no longer exists. There is nothing to do the remembering. I don't even see how the bean gets into scope.
Anders Boje Larsen
Greenhorn

Joined: Jul 15, 2010
Posts: 16
okay, if i change the scope to "session" it still don't remenber.

In line 16 of the (test.adgang.authorized("NAME");) i call the java-class where it set the value.
The way the bean shouldbe set happens in line 28 (PropertyUtils.setProperty(u, "test", "tst");)

I for thought the method set the property of the bean.

You probably right missing something besalt so the bean can remember somthing.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If you're putting a new bean into scope each time then setting it to session won't matter--the next time you see it it would be a new bean, not the one from the session.
Anders Boje Larsen
Greenhorn

Joined: Jul 15, 2010
Posts: 16
Please help.
Where do replace the bean? AND is that the real problem? because right now i do not work first time i load the page.
when i call dataBean.getTest() i get null, but i then console i can see that the java-class are in the bean and setTest("test") and that seams to work, but why do it not remember ?
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
Anders Boje Larsen wrote:Please help.
Where do replace the bean? AND is that the real problem? because right now i do not work first time i load the page.
when i call dataBean.getTest() i get null, but i then console i can see that the java-class are in the bean and setTest("test") and that seams to work, but why do it not remember ?


You are writing



The "class" attribute means that a new bean will be instantiated (and put into the page scope) every time the JSP runs (see this page for more information). But you want to use a bean you set before (in a servlet or somewhere). You can leave out the <jsp:useBean..> tag completely...if the bean is set correctly in a scope, it will be accessible in the JSP.
Anders Boje Larsen
Greenhorn

Joined: Jul 15, 2010
Posts: 16
Okay, now i have try to look at the link you send me. But i still don't think that is the first problem.
My problem is that it return null, and that have nothing to do with the session i think. I have try to short down the code, so it is more simple to understand. REMEMBER it should be possible to get some value other then null first time you open the page.

JSP page


JAVA (test.adgang) class


Bean-class MenuAccess

D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
Anders Boje Larsen wrote:...REMEMBER it should be possible to get some value other then null first time you open the page.


No it MUST be null because you still create a new MeanAccess bean when you run the JSP! In your MenuAccess class, set a default value for the String test (for example, "Hello World!"). Then run the program again and you will get "Hello World!" as output.

I think you are just missing one step really. In your adgang class, you create a MenuAccess bean, and set the property test correctly. BUT: what do you do with this bean? Where do you put it into a scope (request or session)? If it is not in a scope, then the JSP cannot access it. Instead it accesses the bean that you CREATE in the JSP (using the <jsp:useBean> tag).
Anders Boje Larsen
Greenhorn

Joined: Jul 15, 2010
Posts: 16
OK if i remove this
<jsp:useBean id="dataBean" scope="session" class="utilities.MenuAccess"/>
From the JSP-page and write it in the java class (adgang). I don't now the code for create a session with the bean in java and not jsp, yet.

I thought that when i wrote
<jsp:useBean id="dataBean" scope="session" class="utilities.MenuAccess"/>
I make the scope for the bean in jsp and that I could yes same scope in the java-class also.
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
For a "quick and dirty" test you can change the adgang class a little bit so that it returns the created bean. Then you can simply access it in the JSP without using a scoped variable:



But note that this is no good program design, you really want to use JSPs only for view related things...they should not access the database.
Anders Boje Larsen
Greenhorn

Joined: Jul 15, 2010
Posts: 16
You are the man.....!
This work, now i just need to a way so the variable are been remember, in a session.
I can't find a method to do this from the adgang class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bean will not remember values