How To Access Java Beans in JSP Page
When you add a bean to a JSP, you can either create a new bean or use an existing one.
The JSP engine determines whether it needs to create a new bean for you based on the bean's id.
While adding a bean to a page, you must at least give the bean an id (which is just a name) and the bean's class.
The JSP engine first searches for an existing bean with the same id.
If it doesn't find an existing bean, the JSP engine creates a new instance of the class you specified.
package beans;
public class Person implements java.io.Serializable {
protected String firstName;
protected String lastName;
protected int age;
public Person() {
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String aFirstName) {
firstName = aFirstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String aLastName) {
lastName = aLastName;
}
public int getAge() {
return age;
}
public void setAge(int anAge) {
age = anAge;
}
}
jsp page
<%@page contentType="text/html"%>
<%-- Create an instance of the bean --%>
<%-- Copy the parameters into the bean --%>
The bean values are:
First Name:
Last Name:
Age:
This Java tip illustrates a method of using a Bean in a JSP page. To use a bean in a JSP page, three attributes must be supplied - an id, which provides a local name for the bean, the bean's class name, which is used to instantiate the bean if it does not exit, and a scope, which specifies the lifetime of the bean. There are four scopes available - page, request, session, and application. A page-scoped bean is available only within the JSP page and is destroyed when the page has finished generating its output for the request. A request-scoped bean is destroyed when the response is sent. A session-scoped bean is destroyed when the session is destroyed. An application-scoped bean is destroyed when the web application is destroyed.
First Name:
Last Name:
Age:
This Java tip illustrates a method of using a Bean in a JSP page. To use a bean in a JSP page, three attributes must be supplied - an id, which provides a local name for the bean, the bean's class name, which is used to instantiate the bean if it does not exit, and a scope, which specifies the lifetime of the bean. There are four scopes available - page, request, session, and application. A page-scoped bean is available only within the JSP page and is destroyed when the page has finished generating its output for the request. A request-scoped bean is destroyed when the response is sent. A session-scoped bean is destroyed when the session is destroyed. An application-scoped bean is destroyed when the web application is destroyed.
Comments
Post a Comment