The jsp directives are messages that tells the web container how to translate a JSP page into the
corresponding servlet.
There are three types of directives:
page directive
include directive
taglib directive
import
contentType
extends
info
buffer
language
isELIgnored
isThreadSafe
autoFlush
session
pageEncoding
errorPage
isErrorPage
1)import
The import attribute is used to import class,interface or all the members of a package.It is similar
to import keyword in java class or interface.
<html>
<body>
<%@ page import="java.util.Date" %>
Today is: <%= new Date() %>
</body>
</html>
2)contentType
The contentType attribute defines the MIME(Multipurpose Internet Mail Extension) type of the
HTTP response.The default value is "text/html;charset=ISO-8859-1".
<html>
<body>
<%@ page contentType=application/msword %>
Today is: <%= new java.util.Date() %>
7. </body>
8. </html>
3)extends
The extends attribute defines the parent class that will be inherited by the generated servlet.It is
rarely used.
4)info
This attribute simply sets the information of the JSP page which is retrieved later by using
getServletInfo() method of Servlet interface.
5)buffer
The buffer attribute sets the buffer size in kilobytes to handle output generated by the JSP
page.The default size of the buffer is 8Kb.
<html>
<body>
<%@ page buffer="16kb" %>
Today is: <%= new java.util.Date() %>
</body>
</html>
6)language
The language attribute specifies the scripting language used in the JSP page. The default value is
"java".
7)isELIgnored
We can ignore the Expression Language (EL) in jsp by the isELIgnored attribute. By default its
value is false i.e. Expression Language is enabled by default. We see Expression Language later.
1. <%@ page isELIgnored="true" %>//Now EL will be ignored
8)isThreadSafe
Servlet and JSP both are multithreaded.If you want to control this behaviour of JSP page, you
can use isThreadSafe attribute of page directive.The value of isThreadSafe value is true.If you
make it false, the web container will serialize the multiple requests, i.e. it will wait until the JSP
finishes responding to a request before passing another request to it.If you make the value of
isThreadSafe attribute like:
<%@ page isThreadSafe="false" %>
The web container in such a case, will generate the servlet as:
1. public class SimplePage_jsp extends HttpJspBase
2. implements SingleThreadModel{ ....... }
9)errorPage
The errorPage attribute is used to define the error page, if exception occurs in the current page, it
will be redirected to the error page.
10)isErrorPage
The isErrorPage attribute is used to declare that the current page is the error page.
Note: The exception object can only be used in the error page.
Purpose of Introspection
A growing number of Java object repository sites exist on the Internet in answer to the demand
for centralized deployment of applets, classes, and source code in general. Any developer who
has spent time hunting through these sites for licensable Java code to incorporate into a program
has undoubtedly struggled with issues of how to quickly and cleanly integrate code from one
particular source into an application.
The way in which introspection is implemented provides great advantages, including:
1. Portability - Everything is done in the Java platform, so you can write
components once, reuse them everywhere. There are no extra specification files
that need to be maintained independently from your component code. There are
no platform-specific issues to contend with. Your component is not tied to one
component model or one proprietary platform. You get all the advantages of the
evolving Java APIs, while maintaining the portability of your components.
Introspection API
The JavaBeans API architecture supplies a set of classes and interfaces to provide introspection.
The BeanInfo (in the API reference documentation) interface of the java.beans package
defines a set of methods that allow bean implementors to provide explicit information about their
beans. By specifying BeanInfo for a bean component, a developer can hide methods, specify an
icon for the toolbox, provide descriptive names for properties, define which properties are bound
properties, and much more.
The getBeanInfo(beanName) (in the API reference documentation) of the Introspector (in
the API reference documentation) class can be used by builder tools and other automated
environments to provide detailed information about a bean. The getBeanInfo method relies on
the naming conventions for the bean's properties, events, and methods. A call to getBeanInfo
results in the introspection process analyzing the beans classes and superclasses.
The Introspector class provides descriptor classes with information about properties, events,
and methods of a bean. Methods of this class locate any descriptor information that has been
explicitly supplied by the developer through BeanInfo classes. Then the Introspector class
applies the naming conventions to determine what properties the bean has, the events to which it
can listen, and those which it can send.
The following figure represents a hierarchy of the FeatureDescriptor classes:
Each class represented in this group describes a particular attribute of the bean. For example, the
isBound method of the PropertyDescriptor class indicates whether a PropertyChangeEvent
event is fired when the value of this property changes.
Customizers
A customizer is a user interface for customizing an entire Bean, as opposed to a single property.
The characteristics and behaviors of the Bean that can be modified by a customizer are not
limited to its exposed properties. There can be any number of other settings that are needed to
configure a Bean that are not considered properties.
The complexity of the configuration options for a Bean is another possible reason to provide a
customizer. This gives you an opportunity to present these choices in a way that makes more
sense to a user, and might even provide a mechanism which can guide the user through the
process.
If you provide a customizer, you must also provide a BeanInfo class for your Bean in order to
identify its associated customizer class. There is no standard naming convention that can be
followed, nor is there any kind of customizer manager class provided by the JDK. This is another
area that seems inconsistent. I dont see any reason why a run-time registration mechanism, like
that provided by the java.beans.PropertyEditorManager, could not have been provided for
registering Bean customizers at run-time. Likewise, I think a naming convention that appends the
string Customizer to the Bean class name would have been useful, allowing for the creation of
customizers without the need for creating a BeanInfo class. However, it is almost trivial to create
a BeanInfo class solely for the purpose of specifying an associated customizer class.
Java Bean
A Java Bean is a java class that should follow following conventions:
//Employee.java
package mypack;
public class Employee implements java.io.Serializable{
private int id;
private String name;
public Employee(){}
public void setId(int id){this.id=id;}
public int getId(){return id;}
public void setName(String name){this.name=name;}
public String getName(){return name;}
17.
18. }
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
package mypack;
public class Test{
public static void main(String args[]){
Employee e=new Employee();//object is created
e.setName("Arjun");//setting value to the object
System.out.println(e.getName());
}}
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.
package com.javatpoint;
public class Calculator{
public int cube(int n){return n*n*n;}
}
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. %>
download this example
Then verify that the content is correct by the command "jar tf SimpleBean.jar".
Step 5:
Step 6:
1. After the file selection dialog box is closed, change focus to the "ToolBox" window. You'll see
"SimpleBean" appear at the bottom of the toolbox window.
2. Select SimpleBean.jar.
3. Cursor will change to a plus. In the middle BeanBox window, you can now click to drop in what
will appear to be a colored rectangle.
Your screen should look like this:
Step 7:
Try changing the red box's color with the Properties windows.
Step 8:
Chose "Events" under the "Edit" menu in the middle window to see what events SimpleBean can send.
These events are inherited from java.awt.Canvas.