Setting the properties of a javabean in JSP

H

harryos

hi,
while going through some jsp examples ,I came across one that sets
javabean properties using user input values .

userinput.jsp
------------------
....
<body>
<h2>Entry Form</h2>
<form name="InputForm" method="post" action="setBean.jsp">
Enter your Name:
<input type="text" name="username" value="" />
Enter your Email:
<input type="text" name="email" value="" />
<input type="submit" value="OK" />
</form>
....

setBean.jsp
--------------
....
<jsp:useBean id="userB" class="org.mypackage.user.UserBean" >
<jsp:setProperty name="userB" property="*"/>
</jsp:useBean>
<body>
<h2>Posted Data</h2>
<strong> UserName </strong>
<c:eek:ut value="${userB.username}" /><br>
<strong> Email </strong>
<c:eek:ut value="${userB.email}" /><br>
....

where the UserBean.java is

public class UserBean implements java.io.Serializable{
String username;
String email;
public UserBean( ){}
public void setUsername(String _username){
if(_username != null && _username.length( ) > 0)
username = _username;
else
username = "Unknown";
}
public String getUsername( ){
if(username != null)
return username;
else
return "Unknown";
}
....
}

Why do UserBean need to implement Serializable interface? I tried it
without implementing Serializable and the fields are set correctly
according to the user input .Isn't this implements clause unnecessary?
thanks
harry
 
J

John B. Matthews

harryos said:
while going through some jsp examples, I came across one that sets
javabean properties using user input values. [...]
Why do UserBean need to implement Serializable interface? I tried it
without implementing Serializable and the fields are set correctly
according to the user input. Isn't this implements clause
unnecessary?

It depends. This article describes many of the pros and cons:

<http://mindprod.com/jgloss/serialization.html>

Joshua Bloch's _Effective_Java_ devotes all of chapter 11 to the subject:

<http://java.sun.com/docs/books/effective/>
 
L

Lew

harryos said:
... javabean properties using user input values ...

userinput.jsp
------------------
...
<body>
 <h2>Entry Form</h2> ...
<jsp:useBean id="userB" class="org.mypackage.user.UserBean" >
<jsp:setProperty name="userB" property="*"/>
</jsp:useBean>
...

where the UserBean.java is

public class UserBean implements java.io.Serializable{ ....
Why do UserBean need to implement Serializable interface? I tried it
without implementing Serializable and the fields are set correctly
according to the user input .
Isn't this implements clause unnecessary?

It depends on the scope where the bean lives. If it ever needs to
live at session scope, it will need to be Serializable for most
containers that do any kind of load balancing or session migration.
Conceptually since the bean represents information that is passed
around, even if only at request scope, it makes sense to think of it
as Serializable even if it isn't, so there's a pressure to implement
it that way. If the bean doubles as an entity for persistence
purposes, it pretty much has to be Serializable to fulfill that
function effectively.

In the Bloch reference that John B. Matthews cites,
<http://java.sun.com/docs/books/effective/>,
the author discusses the commitment embodied in making a class
Serializable. For entity-like objects such as the one under
discussion here, this doesn't pose too much additional burden since
the 'Serializable'ness is over the publicly accessible (via methods)
attributes anyway.
 
V

vk02720

You will not see any difference in setting fields with or without
implementing Serializable because it is a marker interface and does
not require you to implement any methods.

It depends on the scope where the bean lives.  If it ever needs to
live at session scope, it will need to be Serializable for most
containers that do any kind of load balancing or session migration.
Conceptually since the bean represents information that is passed
around, even if only at request scope, it makes sense to think of it
as Serializable even if it isn't, so there's a pressure to implement
it that way.  If the bean doubles as an entity for persistence
purposes, it pretty much has to be Serializable to fulfill that
function effectively.

Correct. Found this link useful for more on HttpSession.
http://publib.boulder.ibm.com/infoc...press.doc/info/exp/ae/cprs_best_practice.html

Suggests Implement the java.io.Serializable interface when developing
new objects to be stored in the HTTP session. as a best practice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top