IT Community - Software Programming, Web Development and Technical Support

How can we use beans in JSP?

This is a discussion on How can we use beans in JSP? within the Java Server Pages (JSP) forums, part of the Web Development category; How can we use beans in JSP?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Java Server Pages (JSP)

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-26-2007, 03:25 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Question How can we use beans in JSP?

How can we use beans in JSP?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-26-2007, 03:41 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Default Re: How can we use beans in JSP?

JSP provides three tags to work with beans:-

<jsp:useBean id=“bean name” class=“bean class” scope = “page | request | session|application ”/>
Bean name = the name that refers to the bean.
Bean class = name of the java class that defines the bean.
<jsp:setProperty name = “id” property = “someProperty” value = “someValue” />
id = the name of the bean as specified in the useBean tag.
property = name of the property to be passed to the bean.
value = value of that particular property .
<jsp:getProperty name = “id” property = “someProperty” />
Here the property is the name of the property whose value is to be obtained from thebean.Below is a code snippet which shows how MyUserClass is used and the valuesaccessed.

<jsp:useBean id="user" class="MyUserClass" scope="session"/>
<HTML>
<BODY>You entered
<BR>Name: <%= user.getUsername() %><BR>Email:
<%= user.getEmail() %><BR></BODY></HTML>
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-09-2007, 06:54 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
leoraja8 is on a distinguished road
Default Re: How can we use beans in JSP?

Jsp provides as approach to utilizing javabeans that is based on the concept of tags. These are really no more complicated than
the standard html tags; they have a name and they take attributes.
There are three tags provided by the specification to support the use of javabeans in your jsp pages:
1. <jsp:usebean>
2.<jsp:setproperty>
3.<jsp:getproperty>.

Example : Javabean class.

import java.io.Serializable;
public class CarBean implements Serializable
{
private String make = "Ford";
public CarBean()
{}
public String getMake()
{
return make;
}

public void setMake(String make)
{
this.make = make;
}

}

Jsp page:
<html>
<head> <title> Using a bean</title> </head>
<body>
<jsp:usebean id = "MyCar: Class = "CarBean"/>
I have a <jsp:getproperty name = "MyCar" property = "make" />
<jsp:setproperty name = "MyCar" property = "make" value = "Ferrari" />
Now I have a <jsp:getproperty name = "MyCar" property = "make" />
</body>
</html>

output:
--------------
Using a bean

i have a Ford
Now i have a Ferrari
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-19-2008, 05:33 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 180
saravanan is on a distinguished road
Default Re: How can we use beans in JSP?

In jsp eacy to use java beans.

now i just explained a JSP exaple that uses a Java Beans for login

steps.

1. Create a login.html page

2. Create a loginaction.jsp page

3. create a login.java page

login.html
---------

<html>
<head>
<title>Login Page</title>
</head>
<body>
<form action="loginaction.jsp" method="post">
<table width="38%" border="1">
<tr>
<td width="38%">User Name</td>
<td width="62%"><input name="uname" type="text" id="uname"></td>
</tr>
<tr>
<td>Password</td>
<td><input name="pass" type="text" id="pass"></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit"></td>
<td>&nbsp;</td>
</tr>
</table>
</form>
</body>
</html>

login.java
Rule
----
ompile this file and copy the class file to
WEB-INF/classes/your_package_name/your_class_file

in our example
------------
WEB-INF/classes/login/Login.class

package login;
// Java Document
public class Login
{
/*
BEAN PROPERTIES
These values must match with
login.html form text fields names
*/
private String uname;
private String pass;
public Login()
{

}
/*
Now write setter and getter method
just add set or get before the BEAN PROPERTIES
*/
public void setUname(String uname)
{
this.uname=uname;
}
public String getUname()
{
return uname;
}

public void setPass(String pass)
{
this.pass=pass;
}
public String getPass()
{
return pass;
}
public boolean validate()
{
if((uname.equals("kamal") && (pass.equals("kannan"))
{
return true;
}
else
{
return false;
}
}


}

loginaction.jsp
--------------
<%@ page contentType="text/html; charset=" language="java" import="java.sql.*" errorPage="" %>
<%@page import="login.Login"%>
<jsp:useBean id="login" scope="request" type="login.Login"/>
<%-- This is a comment --%>
<%-- Sets all form values to the BEAN PROPERTY --%>
<%-- This only works if your BEAN PROPERTY names and FORm NAMES are mactch --%>
<jsp:setProperty property="*"/>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<%
if(login.validate())
{
Welcome !
%>
<jsp:getProperty property="uname"/>
<%
}
else
{
out.print("Invalid User");
}
%>
</body>
</html>
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
integerate Java Beans in java application saravanan Java Programming 1 03-26-2008 01:22 AM
is there any visual tool or netbeans plugin available to build JAVA BEANS? saravanan Java Server Pages (JSP) 0 03-19-2008 09:08 PM
Can anybody explain in brief Life cycle for Stateless and Stateful beans? oxygen Java Programming 1 07-26-2007 03:49 AM


All times are GMT -7. The time now is 12:22 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0