Home  >  Article  >  Java  >  JSP Usebean

JSP Usebean

PHPz
PHPzOriginal
2024-08-30 15:09:24910browse

Introduction: JSP usebean tag is used to locate the remotely EJB (Enterprise JAVA bean) and create an object of it so that it can be used. The IDE (Integrated development environment) is capable of locating and creating objects of the EJB in the JAVA source folder, so in that case, it is already being detected, and there is no need to have jsp usebean instantiate it explicitly. While if the object is not created, then instantiation is required.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

The syntax of JSP usebean is specified in this section. The syntax for the use bean tag is determined when the jsp tag is started with the useBean attached to it. There are different attributes that can be assigned to this tag. Some attributes have predefined attributes already present. We just have to decide on that. The attributes are explained below:

1. Id: Id is an identifier for the JSP useBean. It is used to identify the EJB in the defined scope.

2. Scope: This attribute defines the scope of this tag. This tag will be non-functional if called outside the scope defined in this tag. There are four scopes “Page”,” Request”,” Session”,” Application”, respectively.  “Page” is the default attribute taken by the useBean tag in JSP if there is no explicit definition. “Request” is to support the wider scope of the bean. This increases the page scope to all the users who access the bean using the same JSP request. The same is the keyword here because if the request is changed, then the bean will not be accessible. “Session” scope provides the privilege to EJB to be used in that particular session irrespective of any requestor. It provides a wider scope than the request scope. “Application” scope provides the widest visibility for EJB. This scope type signifies that EJB can be used by any JSP page calling via any request within the scope of an application. It can be accessed via different sessions as well.

3. Class: The class tag mentions the name of the java implantable class. This class contains data members and member functions to implement the business logic. This class should be present under a package. The package should be used as a prefix of class to call the class in the useBean tag. The class defined should not have constructors or arguments. The class should not be abstract either. Only when the previously mentioned conditions are fulfilled the useBean tag works.

4. Type: It is used to provide the data type to an object of the already instantiated class under a defined scope. The class must have an object created for this to function. This is defined with the bean or class name (Tip: Bean and class name should be the same to avoid any confusion or data issues).

5. beanName: This is used if one wants to instantiate the EJB using a predefined instantiate() function from the JSP library. The full calling path of this function is java.beans.Beans.instantiate().

<jsp:useBean id= "nameOfInstance" scope= "predefinedScope"
class= "packageName.className" type= "packageName.className"
beanName="packageName.className | <%= anyExpression >" >
</jsp:useBean>

How Usebean tag work in JSP?

This can be understood better with the help of examples given in the below section.

The examples provided below explain the working of the useBean tag in JSP. It is used to locate the remotely exiting EJB and include the function or procedure from there to the jsp page. The examples provided below will help you in understanding the data and control flow.

Example #1

JAVA file: BODMAS.java

package test1JSP;
public class BODMAS {
public int bodmas(int a, int b, int c, int d, int e){return a+b-c/d*e;}
}

JSP file: useBean1.jsp

<jsp:useBean id="obj" class="test1JSP.BODMAS"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Example 1 of JSP useBeans</title>
</head>
<body>
<h1>This is an example to demonstrate the use of useBean tag in JSP.</h1>
<br><br>
<p>
<%
int m=obj.bodmas(5,10,24,24,12);
out.print("The outcome of calculating 5+10-24/24*12 expression using BODMAS rule is ");
%>
<font color="#FF0000"> <% out.print(m); %></font> <% out.print("!"); %>
</p>
</body>
</html>

Output:

JSP Usebean

Example #2

useBean2.jsp :

<jsp:useBean id="obj" class="test1JSP.Calculator"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Example 2 of JSP useBeans</title>
</head>
<body>
<h1>This is an example to demonstrate the use of useBean tag in JSP.</h1>
<br><br>
<p>
<%
String Uname=request.getParameter("name");
int m=obj.calculate(Uname);
out.print("The number of letters containing in the string is ");
%>
<font color="#FF0000"> <% out.print(m); %></font> <% out.print("!"); %>
</p>
</body>
</html>

useBean3.jsp :

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> Example 2 of JSP useBeans </title>
</head>
<body>
<form action="useBean2.jsp" method="post">
How many number of letters are present in this letter: <input type="text" name="name"><br> <br>
<input name="name" type="submit" value="Find">
</form>
</body>
</html>

Calculator.java :

package test1JSP;
public class Calculator {
public int calculate(String name) {
String str = name;
int count = 0;
for (int i = 0; i < str.length(); i++)
{
if (Character.isLetter(str.charAt(i)))
count++;
}
return count;
}
}

Output:

JSP Usebean

JSP Usebean

Explanation: In the example, number 1 BODMAS EJB is created, which will calculate any number of provided numbers along with the operations to be performed. This EJB called “BODMAS.java” is present in the remote location and can be called using the JSP page. In this case, useBean1.jsp is the calling page. Please focus on the class attribute of the useBean tag. For example, number 2, three files are created with two JSP pages while one EJB “calculator.java” to perform business operations present remotely. useBean3.jsp  is used to get the input from the user and pass that to useBean2.jsp to perform calculation which involves calculating the number of characters in the provided string. Once this operation is performed via calculator.java, the “count” is passed again to the jsp page and displayed.

Conclusion

JSP useBean tag is one of the most convenient methods to call remotely located EJB. The utility of the useBean tag increases due to its simple syntax and increase in the separation of business operations with designing. The increasing presence of remotely located resources has also increased the utility of this tag in JSP.

The above is the detailed content of JSP Usebean. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Java Method ReferencesNext article:Java Method References