user811471
user811471

Reputation: 31

Difference between java bean and session for session management in jsp

When managing the user session in jsp, we sometime use sessions by setting varialble using session.setAttribute() and on the otherhand we can create java bean object(setting scope to session) to store user information and can retrive it on another page. can anyone tell me what is the difference between these two things?

Upvotes: 3

Views: 3646

Answers (2)

MatthewD
MatthewD

Reputation: 2534

They are just slightly different ways of passing the same data.

As you say, you could either:

(1)

session.setAttribute("entry", entry);

or (2)

session.setAttribute("name", entry.getName());
session.setAttribute("address", entry.getAddress());
session.setAttrubute("phone", entry.getPhone());

Then for (1), the JSP might include:

Name: ${entry.name}<br/>
Address: ${entry.address}<br/>
Phone: ${entry.phone}<br/>

whereas for (2), the equivalent JSP might be:

Name: ${sessionScope['name']}<br/>
Address: ${sessionScope['address']}<br/>
Phone: ${sessionScope['phone']}<br/>

(or you might use <c:set> to set some local variables to make the above code a bit more readable).

Essentially, both are the same, but (1) uses less code and is easier to read and understand.

(Though I am still a Java EE newbie myself, so I wouldn't mind a more seasoned opinion!)

Upvotes: 1

General Yorr
General Yorr

Reputation: 11

There are a number of differences.

First, when you store something as an attribute it is stored as an Object, so it has to be cast into whatever it actually is when you retrieve it, a bean is always the class that it is.

Second, when you're using a bean, you don't have to worry about whether it has been created or not. the jsp:useBean tag will access the bean if it has already been created, and create it if it has not, so if you want to call a function or use a get method, you are guaranteed that it exists when you use a bean. If you plan on storing it in the session variable, but try to access it before you store it, it will return null and calling a function will cause some problems.

Generally you use the session variable to pass information, while you use beans to pass classes that have functions that you might want to call.

So if you're planning on passing relatively simple data around (eg strings), there's nothing wrong with tossing it into the session variable. null makes sense as a value, just be careful in your comparisons (eg do string_var.equals((String)session.getAttribute("attribute_name")) not ((String)session.getAttribute("attribute_name")).equals(string_var) as the second may be null.equals() which is not defined).

If you're programming in the MVC framework, then (to my understanding), you generally use beans to write into the session (or other, depending on the situation) variable, then your JSP page will load the information from the session variable. This (among other things) makes the webpage refresh-safe (meaning, if you hit the refresh button it will simply get the information from the session variable again, instead of running all the code again and reloading the information from the session variable) which is a big deal when you are appending information, incrementing, or decrementing.

Admittedly, I'm relatively new to JSP, but that is my understanding of the situation.

Upvotes: 1

Related Questions