Re: Setting the properties of a javabean in JSP



On Dec 11, 2:02 pm, Lew <l...@xxxxxxxxxxxxx> wrote:
harryos wrote:
... 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 .

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.


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.


Correct. Found this link useful for more on HttpSession.
http://publib.boulder.ibm.com/infocenter/wasinfo/v6r1/index.jsp?topic=/com.ibm.websphere.express.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.

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.

--
Lew- Hide quoted text -

- Show quoted text -

.



Relevant Pages

  • Re: Setting the properties of a javabean in JSP
    ... public class UserBean implements java.io.Serializable{ ... It depends on the scope where the bean lives. ... For entity-like objects such as the one under ...
    (comp.lang.java.programmer)
  • Re: [JSF] einfache (Benutzer-)Verwaltung
    ... Caused by: javax.faces.FacesException: Property id references object in a scope with shorter lifetime than the target scope session ... Request haben bzw. kann nicht den Scope Session haben. ... Die Bean kann keine längere "Lebensdauer" haben als das referenzierte Objekt. ... Falls deine Bean Session-Scope besitzt, du also Sessions benutzt, kannst du dann nicht einfach die Informationen, die du austauschen willst, in der Session halten? ...
    (de.comp.lang.java)
  • Re: JSF: managed-bean-scope
    ... "The managed-bean-scope element defines the scope in which the bean will be stored. ... If you define the bean with a none scope, the bean is instantiated anew each time it is referenced, and so it does not get saved in any scope. ... Jetzt sollten ja eigendlich die Attribute einer Bean nicht mehr irgendwo gespeichert werden, sondern bei jedem Request neu erzeugt werden. ... Das war allerdings nicht so, nach einer neuen Browser Session wurden auf der entsprechenden seite, die an die backing bean angebunden ist, immernoch die zuvor eingegebenen Werte angezeigt. ...
    (de.comp.lang.java)
  • JSF: managed-bean-scope
    ... "The managed-bean-scope element defines the scope in which the bean will be stored. ... If you define the bean with a none scope, the bean is instantiated anew each time it is referenced, and so it does not get saved in any scope. ... One reason to use a scope of none is that a managed bean references another managed bean. ... Das war allerdings nicht so, nach einer neuen Browser Session wurden auf der entsprechenden seite, die an die backing bean angebunden ist, immernoch die zuvor eingegebenen Werte angezeigt. ...
    (de.comp.lang.java)
  • Re: Clearing properties of Object in session scope.
    ... This declares that your page is going to use a bean of class obj.XX, ... new one will be created and bound to the session. ... "obj" based on the parameters of the current request, ... within its scope. ...
    (comp.lang.java.programmer)