Reputation: 11032
There is -
<html>
<body>
<jsp:useBean id="user" class="user.UserData" scope="session"/>
</body>
</html>
And -
<html>
<body>
<%
Object user = session.getAttribute("user.UserData") ;
%>
</body>
</html>
Assume user.UserData
exists on the session
. is there any differnce between the two ways ?
Upvotes: 3
Views: 2858
Reputation: 14624
A well known issue in JSPs is: avoid everything you can on using Java code with your page (.jsp).
So the first approach fits better, do you agree? Taglibs <jsp:useBean />
among others are a nice way of accessing code without mixing the layers. This concepts I barely introduced are part of MVC "specification".
-- EDIT --
The second way of acessing a bean is known as scriptlets and should be avoided as always as possible. A brief comparison can be found here JSTL vs jsp scriptlets.
Upvotes: 3
Reputation: 301
<jsp:useBean id="user" class="user.UserData" scope="session"/>
is equivalent to
<%
Object userDataObject = session.getAttribute("user") ; // id="user" of <jsp:useBean> maps to session attribute name "user"
%>
Besides, the scriptlet only reads existing data from session or returns null if no attribute is found.
If <jsp:useBean> finds attribute "user" in session to be null,
It will create an instance of 'user.UserData' and add to attribute "user" in session scope.
Upvotes: 3