0% found this document useful (0 votes)
35 views

Java Bean

A Java Bean is a reusable Java class that follows certain conventions to make it serializable and easy to access properties through getter and setter methods. The jsp:useBean tag is used to locate or instantiate a bean class in JSP pages and has attributes to specify the bean id, scope, class, and type. An executable JAR file contains the necessary classes and resources for a Java application and specifies a main class so it can be run without explicitly specifying the Java class.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Java Bean

A Java Bean is a reusable Java class that follows certain conventions to make it serializable and easy to access properties through getter and setter methods. The jsp:useBean tag is used to locate or instantiate a bean class in JSP pages and has attributes to specify the bean id, scope, class, and type. An executable JAR file contains the necessary classes and resources for a Java application and specifies a main class so it can be run without explicitly specifying the Java class.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Bean

A Java Bean is a java class that should follow following conventions:

 It should have a no-arg constructor.


 It should be Serializable.
 It should provide methods to set and get the values of the properties, known as getter and
setter methods.

Why use Java Bean?


According to Java white paper, it is a reusable software component. A bean encapsulates many
objects into one object, so we can access this object from multiple places. Moreover, it provides
the easy maintenance.

jsp:useBean action tag


1. jsp:useBean action tag
2. Syntax of jsp:useBean action tag
3. Attributes and Usage of jsp:useBean action tag
4. Simple example of jsp:useBean action tag

The jsp:useBean action tag is used to locate or instantiate a bean class. If bean object of the Bean
class is already created, it doesn't create the bean depending on the scope. But if object of bean is
not created, it instantiates the bean.

Syntax of jsp:useBean action tag


1. <jsp:useBean id= "instanceName" scope= "page | request | session | application"
2. class= "packageName.className" type= "packageName.className"
3. beanName="packageName.className | <%= expression >" >
4. </jsp:useBean>

Attributes and Usage of jsp:useBean action tag

1. id: is used to identify the bean in the specified scope.


2. scope: represents the scope of the bean. It may be page, request, session or application. The
default scope is page.
o page: specifies that you can use this bean within the JSP page. The default scope is page.
o request: specifies that you can use this bean from any JSP page that processes the same
request. It has wider scope than page.
o session: specifies that you can use this bean from any JSP page in the same session
whether processes the same request or not. It has wider scope than request.
o application: specifies that you can use this bean from any JSP page in the same
application. It has wider scope than session.
3. class: instantiates the specified bean class (i.e. creates an object of the bean class) but it must
have no-arg or no constructor and must not be abstract.
4. type: provides the bean a data type if the bean already exists in the scope. It is mainly used with
class or beanName attribute. If you use it without class or beanName, no bean is instantiated.
5. beanName: instantiates the bean using the java.beans.Beans.instantiate() method.

Simple example of jsp:useBean action tag

In this example, we are simply invoking the method of the Bean class.

For the example of setProperty, getProperty and useBean tags, visit next page.

Calculator.java (a simple Bean class)

1. package com.javatpoint;
2. public class Calculator{
3.
4. public int cube(int n){return n*n*n;}
5.
6. }

index.jsp file

1. <jsp:useBean id="obj" class="com.javatpoint.Calculator"/>


2.
3. <%
4. int m=obj.cube(5);
5. out.print("cube of 5 is "+m);
6. %>

1. Defining and using Java libraries


1.1. What is a JAR file?

A JAR file is a Java archive based on the pkzip file format. JAR files are the deployment format
for Java. A JAR can contain Java classes and other resources (icons, property files, etc.) and can
be executable.

You can distribute your program in a jar file or you can use existing java code via jars by putting
them into your classpath.
1.2. Using Java libraries

If you add a JAR file to your classpath, you can use its classes in your Java application.

2. Executable JAR
An executable JAR means the end-user can run the Java application without explicitly specifying
the Java class which should be started. This is done via a MANIFEST.MF file which tells the JVM
among other things which class contains the main() method and the classpath.

The following example shows one possible MANIFEST.MF file.

Manifest-Version: 1.0
Main-Class: com.vogella.example.MyApp
Class-Path:. lib/jcommon-1.0.6.jar lib/itext-1.4.6.jar

An empty line is required at the end of the MANIFEST.MF file.

You can create an executable JAR file via the following command.

jar -cvmf MANIFEST.MF app1.jar *.class

Eclipse provides an export wizard to create a JAR file which can be started via File → Export →
Java → JAR file

You might also like