You can use JSP action element <jsp:useBean> to create an object of a Java class and give it an ID. Using this ID you can get and
set the value of the associated object by using <jsp:getProperty> and <jsp:setProperty> JSP action tags.
Syntax of <jsp:useBean>
This is a shortcut syntax of <jsp:useBean> action element with its attributes.
<jsp:useBean
id = "nameOfJavaBean"
scope = "page|request|session|application"
class = "packageName.className" | beanName = "packageName.className"
type = "packageName.className"
/>
This is a another syntax of<jsp:useBean> action element which does the same thing as previous syntax but it's just a longer version.
<jsp:useBean>
id = "nameOfJavaBean"
scope = "page|request|session|application"
class = "packageName.className" | beanName = "packageName.className"
type = "packageName.className"
</jsp:useBean>
Let's explain the use of the attributes of <jsp:useBean> action element.
id
The id attribute points to the name assigned to a Java Bean which we are going to create using a Java class. This attribute is case-sensitive.
scope
The scope attribute represents the scope in which this Java Bean is going to be created or will be searched(when needed).
A Java Bean could be in one of the following scopes - page, request, session or application.
By default, a Java Bean is created in the page scope.
class
The class attribute represents the fully qualified class(which includes the package name and the class name) used to create a Java Bean.
In order to create a Java Bean, this Java class should be public and should have a public no-argument constructor.
beanName
beanName :
Just like the class attribute, the beanName attribute also represents the fully qualified class(which includes the package name and the class name) used to create a Java Bean. At one time, either
class or beanName attribute should be used. This is an optional attribute.
type
The type attribute takes the class name, which could be similar to class name pointed by class attribute or beanName
attribute or its super type. This is also an optional attribute.
Advertisement
Creating a simple class in Java
Now we are going to show to how to create an object of class in java with and without using
<jsp:useBean> action. Let's say this Java class is A.java, as shown below.
A.java
package decodeJava;
public class A
{
public A()
{
System.out.println("A");
}
String str = "Hello from A class";
public String message()
{
return str;
}
}
Creating an object of a Java class without using <jsp:useBean> action
We are going to create the object of the class A.java without using <jsp:useBean> action.
Sample.jsp
<html>
<head>
<title>JSP page</title>
</head>
<body>
<%
decodeJava.A ob = new decodeJava.A();
%>
<b>Simple calling a method of class A</b> : <%= ob.message() %>
<br/>
<br/>
</body>
</html>
Creating an object of a Java class using <jsp:useBean> action
We are going to create the object of the class A.java using <jsp:useBean> action.
Sample.jsp
<html>
<head>
<title>JSP page</title>
</head>
<body>
<jsp:useBean id="MyBean" class = "decodeJava.A" />
<b>Calling a method of class A using JSP "useBean" action element</b> : <%= MyBean.message() %>
</body>
</html>