JavaBeans Using JSP [ implementing View Helper Design Pattern ]
- What is Component?
- What is Distributed Component?
- What is JavaBean?
- What is Difference between JavaBean and EJB (Enterprise Java Bean )?
- How JavaBeans can be Used in JSP pages?
- JavaBean scope setting in Servlet.
- JavaBean Examples
What is Component? |
- Components are deployable units that provide a business service to their clients.
- It is reusable piece of code and free from the surroundings., Component services can be used as a part of business logic of other components.
-
Components are operation independent of
- Hardware
- The underlying operating system
- Their Application Server
- The network protocol they use
- Each component provides an interface in the form of operations, properties and events
- Components can be developed in any language such as JAVA, C++, VB
- Components are frequently organized into application frameworks for vertical domains.
- If multiple clients are using a component simultaneously, the component will provide the same quality of service to all the clients. [ So, Components are Shareable. ]
- Components should provide services to clients running locally or remotely.
Note :- An application server provides the infrastructure and services to run components/applications.
What is Distributed Component? |
- Distributed Component Object Model (DCOM) is a proprietary Microsoft technology for communication among software components distributed across networked computers. DCOM, which originally was called “Network OLE”, extends Microsoft’s COM, and provides the communication substrate under Microsoft’s COM+ application server infrastructure.
- It has been deprecated in favor of the Microsoft .NET Framework.
- The addition of the “D” to COM was due to extensive use of DCE/RPC (Distributed Computing Environment/Remote Procedure Calls) – more specifically Microsoft’s enhanced version, known as MSRPC.
-
In terms of the extensions it added to COM, DCOM had to solve the problems of
- Marshalling – serializing and deserializing the arguments and return values of method calls “over the wire”.
- Distributed garbage collection – ensuring that references held by clients of interfaces are released when, for example, the client process crashed, or the network connection was lost.
- In Java, We are implementing by using EJB and making distributed component
What is JavaBean? |
- JSP allows separation of the roles of Web designer and programmers involved in developing Web applications using JavaBeans.
- A JavaBean is a reusable and self-contained software component that take advantage of all the security and platform independent features of Java.
- The programmer can create a JavaBean to access the database and develop data processing code in the bean, while the designer can design the user interface in the JSP page.
- The JavaBean can be included in a JSP page to start the processing of user requests.
-
A JavaBean:
- Class should be public
- All properties should be private
- And it should have getter and setter methods (i.e. accessor and mutator)
- It must implements Serializable interface
Example : A sample of Customer Bean
Click Here to download the Complete Code
What is Difference between JavaBean and EJB ( Enterprise Java Bean )? |
-
The server-side components deployed on the application servers can be implemented using various technologies, such as EJB and Microsoft’s COM/DCOM.
Enterprise JavaBeans are different from JavaBeans, - OR we can say Sun Microsystems component based technology.
- EJB is a standard for developing and deploying server side distributed component in java.
- It defines an agreement between component and application servers that enables any component to run in any compliant Application Server.
- EJB is designed to work in distributed environment using rmi/iiop protocol where as JavaBean is standalone and works only in the same JVM.
- Implicit services are provided to EJB by the EJBContainer in which it is deployed according to EJB contract but in JavaBean we need to code all these services.
- EJB require EJB Conatainer to Execute while javaBeans do not require any container.
- JavaBeans are not typed. EJBs are having types – session beans , entity beans, MDB.
Table : summarizes some of the major differences between a JavaBean and an Enterprise JavaBean.
JavaBeans | Enterprise JavaBeans |
---|---|
JavaBeans may be visible or nonvisible at runtime. For example, the visual GUI component may be a button, list box, graphic, or a chart. | An EJB is a nonvisual, remote object. |
JavaBeans are intended to be local to a single process and are primarily intended to run on the client side. Although one can develop server-side JavaBeans, it is far easier to develop them using the EJB specification instead. | EJBs are remotely executable components or business objects that can be deployed only on the server. |
JavaBeans is a component technology to create generic Java components that can be composed together into applets and applications. | Even though EJB is a component technology, it neither builds upon nor extends the original JavaBean specification. |
JavaBeans have an external interface called the Properties interface, which allows a builder tool to interpret the functionality of the bean. | EJBs have a deployment descriptor that describes its functionality to an external builder tool or IDE. |
JavaBeans may have BeanInfo classes, property editors, or customizers. | EJBs have no concept of BeanInfo classes, property editors ,or customizers and provide no additional information other than that described in the deployment descriptor. |
JavaBeans are not typed. | EJBs are having typessession beans ,entity beans and MDB. |
No explicit support exists for transactions in JavaBeans. | EJBs may be transactional and the EJB Servers provide transactional support. |
Component bridges are available for JavaBeans. For example, a JavaBean can also be deployed as an ActiveX control. | An EJB cannot be deployed as an ActiveX control because ActiveX controls are intended to run at the desktop and EJBs are server side components. However, CORBA-IIOP compatibility via the EJB-to-CORBA mapping is defined by the OMG. |
Bean Work as a Business Components in Business Tier of J2EE Architechture
- Business code, which is logic that solves or meets the needs of a particular business domain such as banking, retail, or finance is handled by enterprise beans running in the business tier.
- In MVC model , it work as a model.
How JavaBeans can be Used in JSP pages? |
To use a JavaBean in your JSP page, you need to:
- Include the JavaBean reference in the JSP page
- Set the JavaBean property
- Get the JavaBean property
JSP Actions tag helps:
To perform tasks, such as insertion of files, reusing beans, forwarding a user to another page, and instantiating objects.
The syntax to use a JSP action in a JSP page is:
<jsp:attribute>
<jsp:useBean>
- Instantiates or references a bean with a specific name and scope.
- The <jsp:useBean> element locates or instantiates a JavaBeans component.
- <jsp:useBean> first attempts to locate an instance of the bean.
- If the bean does not exist, <jsp:useBean> instantiates it from a class or serialized template.
JSP Syntax
<jsp:useBean class="nbeanInstanceName" scope="page|request|session|application"
{
class="package.class" [ type="package.class" ]| beanName="{package.class | <%=expression %>}"
type="package.class" | type="package.class"
}
{ /> | > other elements </jsp:useBean> }
Example
<jsp:useBean class="nobj" scope="page" class="pack.Student" />
<jsp:setProperty name="obj" property="*" />
OR
<jsp:useBean class="nobj" scope="page" class="pack.Student" >
<jsp:setProperty name="obj" property="sname" value="JamesBond" />
<jsp:setProperty name="obj" property="sid" value="121" />
</jsp:useBean>
scope="page | request | session | application"
The scope in which the bean exists and the variable named in id is available. The default value is page. The meanings of the different scopes are shown below:
page :
You can use the bean within the JSP page with the <jsp:useBean> element or any of the page’s static include files, until the page sends a response back to the client or forwards a request to another resource.
request :
You can use the bean from any JSP page processing the same request, until a JSP page sends a response to the client or forwards the request to another resource. You can use the request object to access the bean, for example, request.getAttribute(beanInstanceName).
session :
You can use the bean from any JSP page in the same session as the JSP page that created the bean. The bean exists across the entire session, and any page that participates in the session can use it. The page in which you create the bean must have a page directive with session=”true”.
application :
You can use the bean from any JSP page in the same application as the JSP page that created the bean. The bean exists across an entire JSP application, and any page in the application can use the bean.
<jsp:setProperty>
Sets a bean property value or values.
JSP Syntax
<jsp:setProperty name="beanInstanceName"
{
property="*" |
property="propertyName" [ param="parameterName" ] |
property="propertyName" value="{stringLiteral| <%= expression %>}"
}
/>
Example
<jsp:setProperty name="obj" property="*" />
<jsp:setProperty name="obj" property="sname" />
<jsp:setProperty name="obj" property="sname" value="JamesBond" />
Note:- [ property=”*” ] :- Stores all of the values of request parameters in bean properties. The names of the bean properties must match the names of the request parameters
<jsp:getProperty>
Inserts the value of a bean property into the response.
JSP Syntax
<jsp:getProperty name="beanInstanceName" property="propertyName" />
Example
<jsp:useBean class="nobj" scope="page" class="pack.Student" />
<h2>
Student Name is : <jsp:getProperty name="obj" property="sname" />
</h2>
JavaBean scope setting in Servlet |
When the JSP engine translates the JSP page to a servlet, the corresponding servlet code for the preceding <jsp:useBean> action tag will be;
page scope :
it is the default scope. it Specifies that the JavaBean object is available only for the current page. The following code snippet shows the <jsp:useBean> action tag that includes a bean with a page scope:
In JSP:
<jsp:useBean class="ncObj" scope="page" class="javaskool.Customer"/>
In Servlet:
javaskool.Customer cObj=new javaskool.Customer();
request scope :
Specifies that the JavaBean object is available for the current request. The following code snippet shows the <jsp:useBean> action tag that includes a bean with a request scope:
In JSP:
<jsp:useBean class="ncObj" scope="request" class="javaskool.Customer"/>
In Servlet:
javaskool.Customer cObj=new javaskool.Customer();
request.setAttribute("cObj", cObj);
session scope :
Specifies that the JavaBean object is available only for the current session. The following code snippet shows the <jsp:useBean> action tag that includes a bean with a session scope:
In JSP:
<jsp:useBean class="ncObj" scope="session" class="javaskool.Customer"/>
In Servlet:
HttpSession session=request.getSession(true);
javaskool.Customer cObj=new javaskool.Customer();
session.setAttribute("cObj", cObj);
application scope :
Specifies that the JavaBean object is available for the entire Web application. The following code snippet shows the <jsp:useBean> action tag that includes a bean with an application scope.
In JSP:
<jsp:useBean class="ncObj" scope="application" class="javaskool.Customer"/>
In Servlet:
javaskool.Customer cObj=new javaskool.Customer();
getServletContext.setAttribute("cObj", cObj);
JavaBean Examples |
Use Customer JavaBean to Retrieve Customer Details.
Use Customer JavaBean to Store Customer Transaction
Recent Comments