Beruflich Dokumente
Kultur Dokumente
1. generating HTML with servlets can be tedious and can yield a result that is hard to modify. That’s
where JSP comes in.
2. you can write the HTML in the normal manner and putting your Web content developers to
work on your JSP documents. Then also some security problem occurs.
3. The solution is to use both servlets and JavaServer Pages. In this approach, known as the Model
View Controller (MVC) or Model 2 architecture.
4. The original request is handled by a servlet. The servlet invokes the business-logic and data-
access code and creates beans to represent the results.
5. The servlet decides what business logic code applies(the servlet is the controller) and which JSP
page should present the results(the JSP page is the view) and forwards the request there.
6. The most important point about MVC is the idea of separating the business logic and data access
layers from the presentation layer.
Implementing MVC:-
1. Define beans to represent the data.
2. Use a servlet to handle requests.
3. Populate the beans.
4. Store the bean in the request, session, or servlet context.
5. Forward the request to a JSP page.
6. Extract the data from the beans.
CHAPTER-JAVA BEAN
Java bean is a java class that contains getter and setter methods.
The instance variable of java bean class is called bean properties.
Getter method is used to read the data from bean properties.
Setter method is used to write the data to bean properties.
Java bean class does not need to extend from any other class and does not implement any
java interface.
Advantages of java bean
Java syntax is hidden
Page author can manipulate java using xml syntax only.
Stronger separation between content and presentation.
Useful while developing separate terms of html and java developers.
o Simpler object sharing
Easy to share bean across pages in application.
o Convenient mapping between request parameters and object properties
Bean constructs simplify the loading of bean.
Bean properties
There are 3 types of bean properties
o Simple property
It allows only one value at a time.
You can store any type of data like integer, string, double etc.
Example
String name;
public void setName(String nm)
{
name=nm;
}
public String getName()
{
return name;
}
o Boolean property
It allows only Boolean value to bean property.
You can store only true or false value.
Example
Boolean minor;
Public void setMinor(Boolean m)
{
Minor=m;
}
Public Boolean getMinor()
{
Return minor;
}
o Index property
It allows to set multiple values into bean properties.
Example
String[] animals;
public void setAnimals(String anim[])
{
animals=anim;
}
public String[] getAnimals()
{
return animals;
}
<jsp:useBean>
It is used for creating and locating java bean class.
It internally uses pageContext attributes to keep bean class object in specified scope.
It has following attributes
o Id
Id="instance name or object name”
The name is case sensitive.
o Class
Class=”java bean class name”
A fully qualified name of the class.
The class name is case sensitive.
o Scope
Scope=”page/session/request/application”
Default scope is “page”.
o Type
Type=”reference type name of java bean class object or supper class of
bean class.
Small example of <jsp:useBean>:
<jsp:useBean id=”ab” class=”p1.BeanDemo” scope=”session” />
Here, ab is object name of BeanDemo class and keep that object in session scope
P1 is the package name where BeanDemo class resides.
<jsp:setProperty>
Jsp:setProperty element call setter() method on java bean.
It is used to set given values to bean properties.
There are 3 attributes of jsp:setProperty action.
o Property
Property=”bean property name”
If setter methodis setName() then write property=”name” i.e. last name.
o Name
Name=”bean class object name”
Id attribute value given in the <jsp:useBean>
o Value
Value=”value to be set for bean property”
This attribute can accept a request as a value.
Small example of <jsp:setProperty>
<jsp:setProperty name=”ab” name=”nm” value=”hello” />
Here, ab is the object name or id name of <jsp:useBean>, nm is the bean property name
and hello is the value of bean property.
<jsp:getProperty>
Jsp:getProperty element call getter() method on java bean.
It is used to get values from bean properties.
There are 2 attributes of jsp:getProperty action
o Name
Name=”bean class object name”.
Id attribute value given in <jsp:useBean>
o Property
Property=”bean property name”
Names to property to getter() method.
Small example of <jsp:getProperty>
<jsp:getProperty name=”ab” name=”nm” />
Here, ab is the object name of bean class or id name of <jsp:useBean> and nm is the
bean property name.