jsp:useBean standard action Tag

The <jsp:useBean> action is used to instantiate a JavaBean or locate an existing bean instance, and assign it to a variable name or id.
Syntax of <jsp:useBean> action tag
<jsp:useBean id= "name" class= "packageName.className" scope= "page | request | session | application" type= "packageName.className" beanName="packageName.className | <%= expression >" > </jsp:useBean>
Attributes of <jsp:useBean> action tag
five attributes that can be used in a jsp:useBean action element are as follows:
  1. id : The id attribute defines a unique identifier for the bean. This identifier can be used throughout the page and can be thought of as the object reference for the bean.
  2. class : The class attribute specifies the fully qualified name for the JavaBean class
  3. type : If the type attribute is present in a jsp:useBean element, it specifies the type of the JavaBean class.
  4. scope :The scope is a powerful feature that lets you control how long the bean will continue to exist. The scope attribute defines the accessibility and the life span of the bean. This attribute can take one of the following values:
    • page : Using the page attribute value, the bean is available only in the current page after the point where the jsp:useBean action element is used
    • request : With the request attribute value, the accessibility of the bean is extended to the forwarded or included page referenced by a jsp:forward or jsp:include action elemen
    • session : A bean with a session scope is placed in the user's session object. The instance of the bean will continue to exist as long as the user's session object exists. In other words, the bean's accessibility extends to other pages.
    • application : A bean with an application scope lives throughout the life of the JSP container itself. It is available from any JSP page within the same web application
  5. beanName : The beanName attribute represents a name for the bean that the instantiate method of the java.beans.Beans class expects.
Example of <jsp:useBean> action tag
LoginValidate.java
package ns; public class LoginValidate { public String validate(String user, String pass) { if (user != null && pass != null && user.equals("Sunil") && pass.equals("Kumar")) { return("Login Success"); } else { return("Login Fail"); } } }
index.html
<html> <head> <title>Welcome jsp:useBean example </title> </head> <body> <form action="login.jsp" method="post"> <fieldset> <legend> <font face="Courier New" size="+1" color="red"> Login </font> </legend> <table> <tr> <td>User Name </td> <td> <input type="text" name="userName"> </td> </tr> <tr> <td>Password </td> <td> <input type="text" name="password"> </td> </tr> <tr> <td colspan=2> <input type="submit" value="Login"> </td> </tr> </table> </fieldset> </form> </body> </html>
login.jsp
<%@ page language="java" %> <html> <head> <title>Login page </title> </head> <body> <% String userName = request.getParameter("userName"); String password = request.getParameter("password"); %> <jsp:useBean id="login" class="ns.LoginValidate" /> <% out.println(login.validate(userName, password)); %> </body> </html>
Output
jsp:usebean
jsp:usebean
jsp:usebean
jsp:usebean
Download this example



 

Latest Tutorial


Most Visited Tutorial