aspose file tools*
The moose likes JSP and the fly likes use of  value  object ( javabeans ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "use of  value  object ( javabeans )" Watch "use of  value  object ( javabeans )" New topic
Author

use of value object ( javabeans )

kenji kunoichi
Ranch Hand

Joined: Jun 02, 2007
Posts: 46
we can do like :
in a html file :
<BODY>
<FORM METHOD=POST ACTION="SaveName.jsp">
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20><BR>
What's your e-mail address? <INPUT TYPE=TEXT NAME=email SIZE=20><BR>
What's your age? <INPUT TYPE=TEXT NAME=age SIZE=4>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>

then a jsp file like

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%
String name = request.getParameter( "username" );
session.setAttribute( "theName", name );
String email = request.getParameter("email");
session.setAttribute("email", email );
String age = request.getParameter( "age" );
session.setAttribute("age", age);

%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<A HREF="NextPage.jsp">Continue</A>
</body>
</html>


then I will show the value :::

<HTML>
<BODY>
Hello, <%= session.getAttribute( "theName" ) %>
your email is <%= session.getAttribute("email") %> and by the way you are
<%= session.getAttribute("age") %> years old .
</BODY>
</HTML>


so what is the advantage of doing like this using a javabean ::

<HTML>
<BODY>
<FORM METHOD=POST ACTION="SaveName.jsp">
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20><BR>
What's your e-mail address? <INPUT TYPE=TEXT NAME=email SIZE=20><BR>
What's your age? <INPUT TYPE=TEXT NAME=age SIZE=4>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>




package user;

public class UserData {

String username;
String email;
int age;

public void setUsername( String value )
{
username = value;
}

public void setEmail( String value )
{
email = value;
}

public void setAge( int value )
{
age = value;
}

public String getUsername() { return username; }

public String getEmail() { return email; }

public int getAge() { return age; }

}



<jsp:useBean id="user" class="user.UserData" scope="session"/>
<jsp:setProperty name="user" property="*"/>
<HTML>
<BODY>
<A HREF="NextPage.jsp">Continue</A>
</BODY>
</HTML>


<jsp:useBean id="user" class="user.UserData" scope="session"/>
<HTML>
<BODY>
You entered<BR>
Name: <%= user.getUsername() %><BR>
Email: <%= user.getEmail() %><BR>
Age: <%= user.getAge() %><BR>
</BODY>
</HTML>
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The setProperty with the wildcard (*) was a convenience method for loading a bean with all the parameters in a given form. It's not used much these days.

On of the big disadvantages to coding everything in JSP scriptlets is that the code can't be run/tested outside of the JSP environment. When you start to factor your functionality back from JSPs into beans and plain old Java objects (POJOs) you gain the ability to work with the logic from the command line before worrying about how it will work in a web environment. You can also automate a lot of the testing with tools like JUnit.

In general JSP has moved past useBean tags and scriptlets altogether.
These days, it is expected that your control logic will be performed in a servlet and all of your model codeing will be done in POJOs. After all this is done, context is passed to the JSP where (using the newer JSTL and EL) the JSP performs only markup tasks.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
How about ...



If your had a form with 50 attributes and saved each attribute in the session, your session would have 50 references to 50 objects.

If you had a single JavaBean with 50 attributes and saved the bean in the session, your session would have 1 reference to 1 object.
[ April 29, 2008: Message edited by: James Clark ]
 
Consider Paul's rocket mass heater.
 
subject: use of value object ( javabeans )