Starter Tutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Programming » Advanced Java » JSP » Create a JSP page to demonstrate JavaBean and implement usebean tag
Suryateja Pericherla Categories: JSP. No Comments on Create a JSP page to demonstrate JavaBean and implement usebean tag
Join Our Newsletter - Tips, Contests and Other Updates
Email
Name

Aim

Create a JSP page to demonstrate JavaBean and implement usebean tag.

 

Theory

JavaBeans

A Java Bean is a platform-independent component that allows us to reuse the class object in our Java code. Using JavaBeans in the Java program allows us to encapsulate many objects into a single object called a bean. There are various advantages of a JavaBean that are as follows:

  • Exposure to other applications
  • Registration to receive events
  • Ease of configuration
  • Portable
  • Lightweight

 

jsp:useBean action Tag

The jsp:useBean action tag is used to locate or instantiate a bean class. If bean object of the Bean class is already created, it doesn’t create the bean depending on the scope. But if object of bean is not created, it instantiates the bean.

 

Syntax of jsp:useBean action tag is:

<jsp:useBean id= "instanceName" scope= "page | request | session | application"
class= "packageName.className" type= "packageName.className"
beanName="packageName.className | <%= expression >" >
</jsp:useBean>

 

Program

While creating the bean class in Eclipse remember the following points:

  1. Create a new package and add the bean class in that package
  2. Create a public default (no argument) constructor in the bean class

 

Student.java

package beans;

public class Student {
	
	private String name;
	private int age;
	private char grade;	
	
	public Student() {}
	
	public void setName(String n) {
		name = n;
	}
	
	public void setAge(int a) {
		age = a;
	}
	
	public void setGrade(char g) {
		grade = g;
	}
	
	public String getName() {
		return name;
	}
	
	public int getAge() {
		return age;
	}
	
	public char getGrade() {
		return grade;
	}
}

 

index.jsp

<html>
<head>
<title>JSP UseBean Tag</title>
</head>
<body>
	<jsp:useBean id="student" class="beans.Student"></jsp:useBean>
	<%
		student.setName("Raj");
		student.setAge(20);
		student.setGrade('A');
		out.print("Student name is: " + student.getName() + "<br/>");
		out.print("Student age is: " + student.getAge() + "<br/>");
		out.print("Student grade is: " + student.getGrade());
	%>
</body>
</html>

 

Input and Output

index.jsp

Student name is: Raj
Student age is: 20
Student grade is: A

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory