• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

use of value object ( javabeans )

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic