SET AS HOME PAGE

ADD TO FAVORITES / BOOKMARK THIS WEBSITE (More Details)

Introduction

Servlet

Jsp

Security

Enterprise Beans

Contact Us


Using JavaBeans in JSP

 Introduction

JAVABEANS components are Java classes that can be easily reused and composed together into applications. Any Java class that follows certain design conventions can be a JavaBeans component.


 Including Bean Reference

The following code snippet shows the syntax of the <jsp:useBean> action tag:
<jsp:useBean id="Bean Name" scope="ScopeName" class="class name />
A JavaBean can exist in the following scopes:
  • page: Specifies that the JavaBean object is available for the current page only. This is the default scope of a JavaBean.
    <jsp:useBean id="option_bean" scope ="page" class="test.OptionBean"/>
    test.OptionBean option_bean=new test.OptionBean();
  • request: Specifies that the JavaBean object is available for the current request.
    <jsp:useBean id="option_bean" scope ="request" class="test.OptionBean"/>
    test.OptionBean option_bean=new test.OptionBean();
    request.setAttribute("option_bean ", option_bean);
  • session: Specifies that the JavaBean object is available for the current session only.
    <jsp:useBean id="option_bean" scope ="session" class="test.OptionBean"/>
    HttpSession session = request.getSession(true);
    test.OptionBean option_bean=new test.OptionBean();
    session.setAttribute("option_bean ", option_bean);
  • application: Specifies that the JavaBean object is available for the entire Web application.
    <jsp:useBean id="option_bean" scope ="application" class="test.OptionBean" />
    test.OptionBean option_bean=new test.OptionBean();
    getServletContext().setAttribute("option_bean ", option_bean);


 Setting Beans Properties

The following syntax shows how to set a property of a JavaBean using <jsp:setProperty> tag:

<jsp:setProperty name="beanName" property=" propertyName">
 Reading Bean Properties

The following syntax shows how to get the JavaBean property using <jsp:getProperty> tag:

<jsp:getProperty name="beanName" property="propertyName" />

 Click for Next Topic
<- PREVIOUSNEXT ->