Using Beans in JSP. A brief introduction to JSP and Java Beans.
USING BEANS IN JSP
Java Beans
Java Beans are reusable components. They are used to separate Business logic from the Presentation logic. Internally, a bean is just an instance of a
Tutorial Details:
class.
JSP’s provide three basic tags for working with Beans.
bean name = the name that refers to the bean.
Bean class = name of the java class that defines the bean.
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 .
An variant for this tag is the property attribute can be replaced by an “ * ”. What this does is that it accepts all the form parameters and thus reduces the need for writing multiple setProperty tags. The only consideration is that the form parameter names should be the same as that of the bean property names.
Here the property is the name of the property whose value is to be obtained from the bean.
BEAN SCOPES :
These defines the range and lifespan of the bean.
The different options are :
Page scope :
Any object whose scope is the page will disappear as soon as the current page finishes generating. The object with a page scope may be modified as often as desired within the particular page but the changes are lost as soon as the page exists.
By default all beans have page scope.
Request scope :
Any objects created in the request scope will be available as long as the request object is. For example if the JSP page uses an jsp:forward tag, then the bean should be applicable in the forwarded JSP also, if the scope defined is of Request scope.
The Session scope :
In JSP terms, the data associated with the user has session scope. A session does not correspond directly to the user; rather, it corresponds with a particular period of time the user spends at a site. Typically, this period is defined as all the visits a user makes to a site between starting and existing his browser.
The BEAN structure :
The most basic kind of bean simply exposes a number of properties by following a few simple rules regarding method names. The Java BEAN is not much different from an java program. The main differences are the signature methods being used in a bean. For passing parameters to a bean, there has to be a corresponding get/set method for every parameter. Together these methods are known as accessors .
Eg. Suppose we want to pass a parameter “name” to the bean and then return it in the capital form. In the bean, there has to be an setName() method and an corresponding getProperty() method. A point to be noted is that the first letter of the property name is capitalized.(Here, N is in capital)
Also, it is possible to have either get or set in a bean, depending on the requirement for a read only or a write only property.
An example for a Database connection bean is as shown :
package SQLBean;
import java.sql.*;
import java.io.*;
public class DbBean {
String dbURL = "jdbc:db2:sample";
String dbDriver = "COM.ibm.db2.jdbc.app.DB2Driver";
private Connection dbCon;
public DbBean(){
super();
}
public boolean connect() throws ClassNotFoundException,SQLException{
Class.forName(dbDriver);
dbCon = DriverManager.getConnection(dbURL);
return true;
}
public void close() throws SQLException{
dbCon.close();
}
public ResultSet execSQL(String sql) throws SQLException{
Statement s = dbCon.createStatement();
ResultSet r = s.executeQuery(sql);
return (r == null) ? null : r;
}
public int updateSQL(String sql) throws SQLException{
Statement s = dbCon.createStatement();
int r = s.executeUpdate(sql);
return (r == 0) ? 0 : r;
}
}
The description is as follows :
This bean is packaged in a folder called as “SQLBean”. The name of the class file of the bean is DbBean . For this bean we have hardcoded the Database Driver and the URL. All the statements such as connecting to the database, fetching the driver etc are encapsulated in the bean.
There are two methods involved in this particular bean :
Executing a particular query.
Updating a database.
The execSQL(String sql) method accepts the SQL query in the form of a string from the JSP file in which this bean is implemented.
Then the createStatement() method initiates the connection with the dbCon connection object.
Further the executeQuery(sql) method executes the query which is passed on as a string.
return (r == null) ? null : r ;
What this statement does is that, if the value of r is null , it returns a null value and if it is a non null value, it returns the value of r . Though this statement seems redundant, it is useful for preventing any errors that might occur due to improper value being set in r.
The JSP Program is as shows :
DataBase Search
<%@ page language="Java" import="java.sql.*" %>
<%!
ResultSet rs = null ;
ResultSetMetaData rsmd = null ;
int numColumns ;
int i;
%>
Results from
<%
db.connect();
try {
rs = db.execSQL("select * from EMPLOYEE");
i = db.updateSQL("UPDATE employee set FIRSTNME = 'hello world' where EMPNO='000010'");
out.println(i);
} catch(SQLException e) {
throw new ServletException("Your query is not working", e);
}
rsmd = rs.getMetaData();
numColumns = rsmd.getColumnCount();
for(int column=1; column <= numColumns; column++){
out.println(rsmd.getColumnName(column));
}
%>
<%
while(rs.next()) {
%>
<%= rs.getString("EMPNO") %>
<%
}
%>
<%
db.close();
%>
Done
The corresponding tags used in the JSP are as follows :
This tag specifies that the id of this bean is “db”. This id is used throughout the page to refer to this particular bean. The scope of this bean is limited to the request scope only. The class attribute points to the class of the bean.
Here the class file is stored in the SQLBean folder.
This property is used for passing on all the values which are obtained from the form. In this program, the SQL query can be passed on to the program as a part of the request.getParameter so that the query can be modified according to the requests.
rs = db.execSQL("select * from EMPLOYEE");
We can access the execSQL() method by using the bean id. Also the SQL is passed on to this method.
i = db.updateSQL("UPDATE employee set FIRSTNME = 'hello world' where EMPNO='000010'");
The updateSQL() method can also be used in the same JSP program. Here we are updating the employee table and resetting the FIRSTNME field where the EMPNO is 000010.
The major difference between an executeQuery and executeUpdate is that, an executeQuery returns the result set and an executeUpdate returns an integer value corresponding to the number of rows updated by the current query.
As can be seen, it is very easy to connect to databases using beans rather than writing the whole code over and over again in every JSP which requires to talk to the database.
Rate Tutorial: http://www.roseindia.net/jsp/usingbeansinjsp.shtml
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: Using Beans in JSP. A brief introduction to JSP and Java Beans.
View Tutorial: Using Beans in JSP. A brief introduction to JSP and Java Beans.
Related
Tutorials:
Understanding JavaServer Pages Model 2
architecture - JavaWorld December
1999
Understanding JavaServer Pages Model 2
architecture - JavaWorld December
1999 |
Advanced form processing using JSP
This article examines the processing of a user registration form using JSP and JavaBeans while implementing the Memento design pattern. |
Which JSP book serves up the best lesson?
Which JSP bookAs for Web servers/databases, just mentioning a server in the book is not sufficient to be listed here. |
Superior app management with JMX - JavaWorld June
Integrate JMX, a reusable configuration framework, with your JSPs |
Boost Struts with
Boost Struts with XSLT and XML |
The J2EE 1.4 Tutorial
The J2EE 1.4 Tutorial is a guide to developing enterprise applications for the Java 2 Platform, Enterprise Edition (J2EE) version 1.4. Here we cover all the things you need to know to make the best use of this tutorial. |
Using a JMS Provider with MDBs via the J2EE Connector Architecture
Using a JMS Provider with MDBs via the J2EE Connector Architecture
In this article I will provide a brief introduction to MDB and the J2EE Connector Architecture (JCA), examining how MDBs can be deployed with the JCA 1.5 resource adapter to use a JMS pro |
Introduction to JSP
Introduction to JSP
Introduction to JSP
Java Server Pages or JSP for short is Sun's solution for developing dynamic web sites. JSP provide excellent server side scripting support for creating database driven web applications. JSP enable the |
Java Beans, Part 1 Introducing Java Beans
The basic idea of the Beans tutorial is to get you to the point where you can quickly create beans. You may want to write new beans from scratch, or you may want to take existing components, applets, or other classes and turn them into beans. |
Building Java Server Pages
A detailed look at building JSP pages. Should you use JSP or servlets? It mainly depends on the ratio of markup to code. Here you'll also find a guide to the different varieties of tag, and details about the main tags such as and |
JSP (JavaServer Pages) is a standard for combining Java and HTML to provide dynamic content in web pages.
With JSP, you embed Java code in HTML using special JSP tags similar to HTML tags. You install the JSP page, which has a .jsp extension, into the WebLogic Server document root, just as you would a static HTML page. When WebLogic Server serves a JSP page.. |
JSP Format Bean Library
JSP Format Bean Library is a collection of beans which support Java Server Pages(JSP). JSP allows the HTML developer to embed Java into a page. There are a number of common operations in a scripted page that would be tedious or complex without additional |
JavaServer Pages Technology - Documentation
Sun's tutorial for Java Server Pages that provide a good introduction to design web pages with JSP. |
Developing Distributed application using Enterprise Java Beans, J2EE Architecture, EJB Tutorial, WebLogic Tutorial.
Developing Distributed application using Enterprise Java Beans, J2EE Architecture, EJB Tutorial, WebLogic Tutorial.
Distributed Architecture
Two-tier application:
In the past two-tier applications were used. Two-tier applications are also know as |
Introduction To Enterprise Java Bean(EJB). WebLogic 6.0 Tutorial.
Introduction To Enterprise Java Bean(EJB). WebLogic 6.0 Tutorial.
Welcome to EJB Section
(Learn to Develop World Class Applications with Enterprise Java Beans)
(Online WebLogic 6.0 Tutorial)
Introduction To Enterprise Java Bean(EJB)
Enterprise |
Introduction To Enterprise Java Bean(EJB). Developing web component.
Introduction To Enterprise Java Bean(EJB). Developing web component.
Developing web component
Introduction To Java Beans
J2EE specification defines the structure of a J2EE application. According to the specification J2EE application consists of |
Introduction to the JSP Java Server Pages
Introduction to the JSP Java Server Pages
Welcome to JSP Section
Introduction To JSP
Java Server Pages or JSP for short is Sun's solution for developing dynamic web sites. JSP provide excellent server side scripting support for creating database |
Using Taglib in JSP. A brief introduction to taglibs and taglibs programing.
Using Taglib in JSP. A brief introduction to taglibs and taglibs programing.
JSP TAG LIBRARIES
JSP Tag Libraries :
JSP’s offer a unique feature of “Tag Libraries”. Simply put, these are custom defined JSP tags. They are basically meant for |
Introduction to JSP tags JSP Directives
Introduction to JSP tags JSP Directives
INTRODUCTION TO JSP TAGS
I n this lesson we will learn about the various tags available in JSP with suitable examples. In JSP tags can be devided into 4 different types. These are:
Directives
In the |
Using Beans in JSP. A brief introduction to JSP and Java Beans.
Using Beans in JSP. A brief introduction to JSP and Java Beans.
USING BEANS IN JSP
Java Beans
Java Beans are reusable components. They are used to separate Business logic from the Presentation logic. Internally, a bean is just an instance of a |
|
|
|