JSP Directives

Embed Size (px)

Citation preview

  • 8/10/2019 JSP Directives

    1/8

    JSP directivesThejsp directivesare 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

    Syntax of JSP Directive1.

    JSP page directiveThe page directive defines attributes that apply to an entire JSP page.

    Syntax of JSP page directive1.

    Attributes of JSP page 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 similarto import keyword in java class or interface.

    Example of import attribute1.

    2. 3.

    4.

    5. Today is:

    6.

    7. 8.

    2)contentTypeThe contentType attribute defines the MIME(Multipurpose Internet Mail Extension) type of the

    HTTP response.The default value is "text/html;charset=ISO-8859-1".

    Example of contentType attribute1.

    2.

    3.4.

    5. Today is:

    6.

  • 8/10/2019 JSP Directives

    2/8

    7.

    8.

    3)extendsThe extends attribute defines the parent class that will be inherited by the generated servlet.It israrely used.

    4)infoThis attribute simply sets the information of the JSP page which is retrieved later by using

    getServletInfo() method of Servlet interface.

    Example of info attribute1.

    2.

    3.4.

    5. Today is:

    6.

    7. 8.

    The web container will create a method getServletInfo() in the resulting servlet.For example:

    1. public String getServletInfo() {

    2.

    return "composed by Sonoo Jaiswal";3. }

    5)bufferThe buffer attribute sets the buffer size in kilobytes to handle output generated by the JSP

    page.The default size of the buffer is 8Kb.

    Example of buffer attribute1.

    2.

    3.

    4. 5. Today is:

    6.

    7. 8.

    6)languageThe 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. //Now EL will be ignored

    8)isThreadSafeServlet 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 ofisThreadSafe attribute like:

    The web container in such a case, will generate the servlet as:

    1. public class SimplePage_jsp extends HttpJspBase

    2. implements SingleThreadModel{ ....... }

  • 8/10/2019 JSP Directives

    3/8

    9)errorPageThe errorPage attribute is used to define the error page, if exception occurs in the current page, it

    will be redirected to the error page.

    Example of errorPage attribute1. //index.jsp2.

    3.

    4.

    5.

    6.

    7. 8.

    9.

    10.

    10)isErrorPageThe 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.

    Example of isErrorPage attribute1. //myerrorpage.jsp

    2.

    3. 4.

    5.

    6.7. Sorry an exception occured!

    8. The exception is:

    9.

    10.11.

    Introspectionis the automatic process of analyzing a bean's design patterns to reveal the bean'sproperties, events, and methods. This process controls the publishing and discovery of bean

    operations and properties. This lesson explains the purpose of introspection, introduces the

    Introspection API, and gives an example of introspection code.

    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 oneparticular 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 writecomponents 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 onecomponent model or one proprietary platform. You get all the advantages of the

    evolving Java APIs, while maintaining the portability of your components.

  • 8/10/2019 JSP Directives

    4/8

    2. Reuse- By following the JavaBeans design conventions, implementing the

    appropriate interfaces, and extending the appropriate classes, you provide yourcomponent with reuse potential that possibly exceeds your expectations.

    Introspection API

    The JavaBeans API architecture supplies a set of classes and interfaces to provide introspection.

    TheBeanInfo(in the API reference documentation) interface of the java.beanspackagedefines a set of methods that allow bean implementors to provide explicit information about theirbeans. 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.

    ThegetBeanInfo(beanName)(in the API reference documentation) of theIntrospector(in

    the API reference documentation) class can be used by builder tools and other automated

    environments to provide detailed information about a bean. The getBeanInfomethod relies on

    the naming conventions for the bean's properties, events, and methods. A call to getBeanInforesults in the introspection process analyzing the beans classes and superclasses.

    The Introspectorclass 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 BeanInfoclasses. Then the Introspectorclassapplies 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 FeatureDescriptorclasses:

    Each class represented in this group describes a particular attribute of the bean. For example, the

    isBoundmethod of thePropertyDescriptorclass 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

    http://java.sun.com/javase/7/docs/api/java/beans/BeanInfo.htmlhttp://java.sun.com/javase/7/docs/api/java/beans/BeanInfo.htmlhttp://java.sun.com/javase/7/docs/api/java/beans/BeanInfo.htmlhttp://java.sun.com/javase/7/docs/api/java/beans/Introspector.html#getBeanInfo%28java.lang.Class%29http://java.sun.com/javase/7/docs/api/java/beans/Introspector.html#getBeanInfo%28java.lang.Class%29http://java.sun.com/javase/7/docs/api/java/beans/Introspector.html#getBeanInfo%28java.lang.Class%29http://java.sun.com/javase/7/docs/api/java/beans/Introspector.htmlhttp://java.sun.com/javase/7/docs/api/java/beans/Introspector.htmlhttp://java.sun.com/javase/7/docs/api/java/beans/Introspector.htmlhttp://java.sun.com/javase/7/docs/api/java/beans/PropertyDescriptor.htmlhttp://java.sun.com/javase/7/docs/api/java/beans/PropertyDescriptor.htmlhttp://java.sun.com/javase/7/docs/api/java/beans/PropertyDescriptor.htmlhttp://java.sun.com/javase/7/docs/api/java/beans/PropertyDescriptor.htmlhttp://java.sun.com/javase/7/docs/api/java/beans/Introspector.htmlhttp://java.sun.com/javase/7/docs/api/java/beans/Introspector.html#getBeanInfo%28java.lang.Class%29http://java.sun.com/javase/7/docs/api/java/beans/BeanInfo.html
  • 8/10/2019 JSP Directives

    5/8

    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 moresense 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 toidentify 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 anotherarea 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 forregistering Bean customizers at run-time. Likewise, I think a naming convention that appends the

    string Customizerto the Bean class name would have been useful, allowing for the creation ofcustomizers without the need for creating a BeanInfo class. However, it is almost trivial to createa BeanInfo class solely for the purpose of specifying an associated customizer class.

    Java BeanA 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.

    Simple example of java bean class

    1.

    //Employee.java

    2.

    3.

    package mypack;

    4.

    public class Employee implements java.io.Serializable{

    5.

    private int id;

    6.

    private String name;

    7.

    8.

    public Employee(){}

    9.

    10.

    public void setId(int id){this.id=id;}

    11.

    12.

    public int getId(){return id;}

    13.

    14.

    public void setName(String name){this.name=name;}

    15.

    16.

    public String getName(){return name;}

  • 8/10/2019 JSP Directives

    6/8

    17.

    18.

    }

    How to access the java bean class?

    To access the java bean class, we should use getter and setter methods.

    1.

    package mypack;

    2.

    public class Test{

    3.

    public static void main(String args[]){

    4.

    5.

    Employee e=new Employee();//object is created

    6.

    7.

    e.setName("Arjun");//setting value to the object

    8.

    9.

    System.out.println(e.getName());

    10.

    11.

    }}

    jsp:useBean action tag1.

    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 tag1.

    4.

    Attributes and Usage of jsp:useBean action tag1.

    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 tagIn this example, we are simply invoking the method of the Bean class.

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

    http://www.javatpoint.com/jsp-useBean-actionhttp://www.javatpoint.com/jsp-useBean-actionhttp://www.javatpoint.com/jsp-useBean-action#jspusesynhttp://www.javatpoint.com/jsp-useBean-action#jspusesynhttp://www.javatpoint.com/jsp-useBean-action#jspuseattrhttp://www.javatpoint.com/jsp-useBean-action#jspuseattrhttp://www.javatpoint.com/jsp-useBean-action#jspuseex1http://www.javatpoint.com/jsp-useBean-action#jspuseex1http://www.javatpoint.com/jsp-useBean-action#jspuseex1http://www.javatpoint.com/jsp-useBean-action#jspuseattrhttp://www.javatpoint.com/jsp-useBean-action#jspusesynhttp://www.javatpoint.com/jsp-useBean-action
  • 8/10/2019 JSP Directives

    7/8

    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.

    2.

    3.

    download this example

    Let's Create a Simple Bean!This example is from JavaSoft's"Creating a Minimal Bean."

    Step 1: Put this source code into a file named "SimpleBean.java"import java.awt.*;import java.io.Serializable;public class SimpleBean extends Canvas

    implements Serializable{//Constructor sets inherited properties

    public SimpleBean(){setSize(60,40);setBackground(Color.red);

    }

    }What will the above code look like when displayed in the bean box?

    Step 2: Compile the file:javac SimpleBean.java

    Step 3: Create a manifest file, named "manifest.tmp":Name: SimpleBean.classJava-Bean: True

    Step 4: Create the JAR file, named "SimpleBean.jar":jar cfm SimpleBean.jar manifest.tmp SimpleBean.class

    Then verify that the content is correct by the command "jar tf SimpleBean.jar".

    Step 5:

    http://www.javatpoint.com/src/jsp/usebeanaction.ziphttp://www.javatpoint.com/src/jsp/usebeanaction.ziphttp://java.sun.com/beans/docs/javaBeansTutorial-Nov97/javabeans/beanbox/creatingBean.htmlhttp://java.sun.com/beans/docs/javaBeansTutorial-Nov97/javabeans/beanbox/creatingBean.htmlhttp://java.sun.com/beans/docs/javaBeansTutorial-Nov97/javabeans/beanbox/creatingBean.htmlhttp://java.sun.com/beans/docs/javaBeansTutorial-Nov97/javabeans/beanbox/creatingBean.htmlhttp://www.javatpoint.com/src/jsp/usebeanaction.zip
  • 8/10/2019 JSP Directives

    8/8

    1.

    Start the Bean Box.

    CD to c:\Program Files\BDK1.1\beanbox\.

    Then type "run".

    2.

    Load JAR into Bean Box by selecting "LoadJar..." under the Filemenu.

    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 whatwill 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.

    Let's have fun with our bean... Let's add a property!As an exercise to try in class right now, let's add a colored rectangle inside of the red square. We'll add a

    property named "Color" and give it a setter and a getter method to change the interior rectangle from

    its default color of green. The needed code is shown below, so you just have to go through the stepsshown above to make it show up in the Bean Box!

    import java.awt.*;import java.io.Serializable;public class MediumBean extends Canvas

    implements Serializable {private Color color = Color.green;//property getter methodpublic Color getColor(){

    return color;}//Property setter method. Sets new inside color and repaints.public void setColor(Color newColor){

    color = newColor;repaint();

    }public void paint(Graphics g) {g.setColor(color);g.fillRect(20, 5, 20, 30);}//Constructor sets inherited propertiespublic MediumBean(){setSize(60,40);setBackground(Color.red);}

    }