Sei sulla pagina 1di 6

Objectives:

Understand what are Java Beans and how it is applied to JSP

Learn to process information using Java Beans

Understand the JSP tags pertaining to Java Beans

Concepts

Java Beans are reusable software components

o We can write a Java Bean that can be used in a variety of other Java applications

A Java Bean is just a Java class. It must adhere with the following rules

o

It has a public class

o

It has a public constructor with no arguments

o

It has a public get and set method to read and write to properties

You just need to define a class that has a field corresponding to each field in the

form The class fields must have “setters ” and getters that match the names of the

form fields The “setter” method is just a method that starts with “set” followed by the

name of the field. The first character of the field name is upper-cased

Getter methods are defined similarly with “get’ instead of “set”

The benefit of Java Beans is that HTML programmers and graphic designers can do presentation development while the Java programmer can do the programming logic.

By using Java Beans you can fully separate the business logic from the generation of display

JSP Java Beans Tags

<jsp:useBean>

<jsp:setProperty>

<jsp:getProperty>

<jsp:useBean>

o

This tag is used to declare and instantiate the Java Bean class.

o

Syntax:

<jsp:useBean id=”object-name” scope=”page | request | session | application” type=”type-of-object”

class=”fully-qualified- class name” beanName=”fully-qualified-bean-name”

/>

Where:

id – the variable you use to reference the Bean scope page: it last until the page completes and there is no storage of state. request: The Java Bean instance lasts as the client request and so will remain if the request is forwarded session: The Java Bean lasts as long as the client session application: The Java Bean is instantiated with the application and remains in use until the application ends. type – type of the object which can be the same class, super class or an interface which the class implements. (optional) class – the fully qualified class name beanName – it is also the fully qualified class name just like the class attribute describe above. Only difference is that the class name in the case of beanName can be provided at request time

<jsp:setProperty>

This tag is used to set the value of one or all of the properties of given Java Bean

Syntax

<jsp:setProperty

id=”object-name”

property=”name-of-property”

param=”name-of-request-parameter-to-use”

value=”new-value-of-this-property”

/>

Where name – id of the <jsp:useBean /> tag you previously set property – name of property whose value you want to set param – name of request parameter you want to use to set the value of this property value – the value you want to set for this property

<jsp:getProperty>

This tag is used to retrieve the value of a given property from the Java Bean

Syntax

<jsp:getProperty

name=”name-of-the-object”

property=”name-of-property”

/>

Where name – id of the <jsp:useBean> tag you previously set property- name of property whose value you want to retrive

Deploying your Java Bean

1. If you haven’t done modifying your ‘CLASSPATH’ environment variable:

a. Add the system variable “CLASSPATH” and C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT\WEB-INF\classes. Here C:\Program Files\Apache Software Foundation\Tomcat 7.0\ is the installation directory of my Apache Tomcat server.

. Here C:\Program Files\Apache Software Foundation\Tomcat 7.0\ is the installation directory of my Apache Tomcat server.
2. Create your Java source code using your favorite editor. 3. Compile and then save

2. Create your Java source code using your favorite editor.

3. Compile and then save the class file under C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT\WEB-INF\classes\

Examples:

myfirst.jsp

<html>

<head>

<title>Java Bean Example One</title> </head> <h1>Java Bean Example One</h1> <hr /> <body> <jsp:useBean id="mybean" class="examples.MyFirstBean" scope="session"> <jsp:setProperty name="mybean" property="name" value="Hello World"/> </jsp:useBean>

<h1><jsp:getProperty name="mybean" property="name" /></h1>

</body>

</html>

MyFirstBean.java

package examples; public class MyFirstBean { private String name = new String(); public MyFirstBean(){}

public String getName(){ return name;

}

public void setName(String name){ this.name = name;

}

}

exampletwo.jsp

<html>

 

<head><title>This is another Java Bean Example Used In JSP</title></head>

<body>

<form action="processfrm.jsp" method="post"> Name: <br /> <input type="text" name="name" /> <br /> Email: <br /> <input type="text" name="emailadd" /> <br /> <input type="submit" value="Submit" />

</form>

</body>

</html>

FormBean.java

package examples; public class FormBean {

String name = new String(); String emailadd = new String();

public FormBean(){}

public void setName(String fname){

name = fname;

}

public void setEmailadd(String eadd) { emailadd = eadd;

}

public String getName(){ return name;

}

public String getEmailadd(){

}

}

return emailadd;