In this tutorial you will learn how to use
JSP useBean Action Example
In this tutorial you will learn how to use <jsp:useBean> in JSP.
Here I am giving a simple example which will demonstrate you how to use useBean action in JSP. Lets start to create an example at first I have created package named pack and then created a Bean class in this package named Bean.java that contains a data member, setter and getter methods. Then created a jsp file named jspUseBean.jsp where uses a <jsp:useBean> tag into which the id specifies the bean name from which it will instantiated and the class specifies the Bean class. In the body of the <jsp:useBean> I have used the <jsp:setProperty> into which the name specifies the Bean instantiation name property specifies the setter method name and the value specifies the value that you have set into the setter method variable. Further I have used <jsp:getProperty> to fetch the value that are set using <jsp:setProperty>
Example:
Bean.java
package pack; public class Bean { String str; public String getStr() { return str; } public void setStr(String str) { this.str = str; } }
jspUseBean.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@page import="pack.Bean"%><html> <title>JSP useBean Action Example</title> <head> </head> <body> <jsp:useBean id="useBean" class="pack.Bean"> <jsp:setProperty name="useBean" property="str" value="Welcome !" /> </jsp:useBean> <p> <jsp:getProperty name="useBean" property="str" /></p> </body> </html>
Output :
When you will execute the jspUseBean.jsp you will get the output as :
[ 0 ] Comments