Top Banner

of 32

2 JSP-II

May 30, 2018

Download

Documents

suresh1130
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/14/2019 2 JSP-II

    1/32

    JSP Action

    tags/Standard Actions

  • 8/14/2019 2 JSP-II

    2/32

    and 5

    Scope4

    Using 3

    Java Beans2

    Standard Action Tags1

    Contents

  • 8/14/2019 2 JSP-II

    3/32

    Know

    Standard Action Tags

    What Java Beans are

  • 8/14/2019 2 JSP-II

    4/32

    Be able to

    Implement Standard Action Tags and JavaBeans

  • 8/14/2019 2 JSP-II

    5/32

    Standard Action Tags

    Actions may affect the current out stream anduse, modify and/or create objects.

    These tags translate into to certain predefined

    java code.They are used so that the JSP filedoesn't have too much embedded java code.

  • 8/14/2019 2 JSP-II

    6/32

    Standard Action Tags The JSP specification includes some actions

    that are standardand must be implemented byall conforming JSP containers;

  • 8/14/2019 2 JSP-II

    7/32

    Java Beans

    A Java Bean is a reusable software componentthat can be visually manipulated in builder tools

    A java bean is simply a java class that meet thefollowing criteria:

    1. The class must be public.

    2. The class must have a no-arguments constructor.

    3. The class must provide set and get methods toaccess variables.

    4. The class should be serializable.

  • 8/14/2019 2 JSP-II

    8/32

    public class Employee implementsjava.io.Serializable {

    private String empno;private String name;public Employee(){}public void setEmpno(String empno){

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

    public String getEmpno(){return empno;}public String getName(){return name;}}

  • 8/14/2019 2 JSP-II

    9/32

    Bean -Simple properties

    Simple property is a bean property with a single

    value whose changes are independent of

    changes in any other property.

    To add simple properties to a bean, addappropriate getXXXand setXXXmethods (or

    isXXXand setXXXmethods for a boolean

    property).

    Note that for boolean to specify accessor

    public boolean isRetired(){ return this.r; }

  • 8/14/2019 2 JSP-II

    10/32

    Bean-indexed property

    An indexed property is an array of properties Individual values

    PropertyElementgetPropertyName(int index)

    public void setPropertyName(intindex,PropertyElement element)

    Entire array

    public PropertyElement[]getPropertyName()public voidsetPropertyName(PropertyElement

    element[])

  • 8/14/2019 2 JSP-II

    11/32

    Used to instantiate or create a reference to a Java Class

    (Bean) inside a JSP page.

    The basic semantic tries to find an existing object usingid and scope.

    If the object is not found it will attempt to create theobject using the other attributes.

    body

    typeSpc :

    class=className |

    class=className type=typeName |

    type=typeName

  • 8/14/2019 2 JSP-II

    12/32

    id: The name used to identify the object instance in

    the specified scopes namespace

    The scripting variable name declared andinitialized with that object reference

    Is case sensitive and should conform to thecurrent scripting language variable-naming

    conventions

    The id attribute of a useBean tag is mandatoryand can be an arbitrary string, but must be

    unique within any one page

  • 8/14/2019 2 JSP-II

    13/32

    scope: page, request, session and application

    Default is page.

  • 8/14/2019 2 JSP-II

    14/32

    class: The fully qualified name of the class that defines

    the implementation of the object

    Name is case sensitive

    If the class and beanName attributes are not

    specified, the object must be present in the givenscope.

  • 8/14/2019 2 JSP-II

    15/32

    beanName: The name of a Bean, as expected by the

    instantiate() method of the java.beans.Beansclass.

    This attribute can accept a request-time attributeexpression as a value.

    The beanName attribute can be used to point toa file that contains a serialized bean (theextension .ser is used to indicate a serializedobject). However when using beanName with a

    serialized file the .ser extension is not included. A useBean tag can use either class or

    beanName but not both. A beanName attributemust always be accompanied by a type attribute.

  • 8/14/2019 2 JSP-II

    16/32

    type: If specified, it defines the type of the scripting variable

    defined. The type is required to be either the class itself, a

    superclass of the class, or an interface implementedby the class specified.

    The object referenced is required to be of this type,

    otherwise a java.lang.ClassCastException occurs atrequest time when the assignment of the objectreferenced to the scripting variable is attempted.

    If unspecified, the value is the same as the value ofthe class attribute.

  • 8/14/2019 2 JSP-II

    17/32

    Using

  • 8/14/2019 2 JSP-II

    18/32

    Same as the as 1 but returns an Object class.

    Translates into:

    java.lang.Objectlist=session.getAttribute("list");

    if(list==null)

    list= new java.util.Vector();

    session.setAttribute("list,list);

  • 8/14/2019 2 JSP-II

    19/32

    Scope

    page scope:

    the object is available only to this page.

    request scope:

    the object is now associated with the request.

    If the request is forwarded (using

    or tag ) to another

    JSP, the object is available. This object canbe accessed by invoking getAttribute()

    method on request object.

  • 8/14/2019 2 JSP-II

    20/32

    Scope

    session scope:

    the object will be available to all JSPs in the

    current session.

    It is a fatal translation error to attempt to use

    session scope when the JSP page so

    attempting has declared, via the directive (see later) that it does notparticipate in a session.

  • 8/14/2019 2 JSP-II

    21/32

    Scope

    application scope:

    the object will be available in any JSP page in

    the same web application. This object can beaccessed by invoking getAttribute() method

    on application object.

  • 8/14/2019 2 JSP-II

    22/32

    tag is used along with tag, to set values of thebean property.

    prop_expr

    property=property value=value

    property=property

    property=property param =paramproperty=*

    property: name of the bean property. It can also be *. If so, if requestparameters have the same name as the bean property, then the value of therequest parameter is automatically set to the bean property.

    value: Value to be set to the property.

    param:The name of the request parameter whose value the bean propertywill be set to.

    name: is the name of the bean instance which has been created using the tag.

  • 8/14/2019 2 JSP-II

    23/32

    Places the value of a Bean instance property,converted to a String, into the implicit out object,from which you can display the value as output.

    Student java

  • 8/14/2019 2 JSP-II

    24/32

    ..

    sr.Student

    -name

    -reg

    +String getName()

    +String getReg()

    Student.java

    +void setReg(String reg)

    +void setName(String nm)

    stud.setName(request.getParameter(name)

    Displays value of reg

    stud.setName(request.getParameter(nm)

    Student java stud jsp

  • 8/14/2019 2 JSP-II

    25/32

    Student.java stud.jsp

    stud.html

    On submitting the form the name

    and reg property of the stud beanis automatically set to the values

    entered by the user.

    If there is no matching request parameter

    for a bean property then it remains unset.

    You should always make sure that your

    beans default constructor sets appropriate

    default values.

    sr.Student

    -name

    -reg

    +String getName()

    +String getReg()

    +void setName(String nm)

    +void setReg(String reg)

  • 8/14/2019 2 JSP-II

    26/32

    where c.Cust is a java bean which implementsSerializable interface.

    beanName attribute example

  • 8/14/2019 2 JSP-II

    27/32

    The Web containerwill automatically attempt to converttheString into the type of the bean parameter. This conversionwill work for the following types:

    byte or Byte As indicated in java.lang.Byte.valueOf(String) boolean or Boolean As indicated in

    java.lang.Boolean.valueOf(String)

    char or CharacterAs indicated in String.charAt(0)

    double or Double As indicated injava.lang.Double.valueOf(String)

    int or IntegerAs indicated in java.lang.Integer.valueOf(String)

    float or Float As indicated in java.lang.Float.valueOf(String)

    long or Long As indicated in java.lang.Long.valueOf(String)

    short of Short As indicated in java.lang.Short.valueOf(String)

    Object As if new String(string-literal)

    Valid Value Assignments

  • 8/14/2019 2 JSP-II

    28/32

    Used to provide other tags with additional

    information in the form of the name-value pairs.

    Used in conjunction with the &

    Allows the JSP to transfer the control to another

    JSP, servlet or static HTML page. The jsp that forwards the request should not use the

    output stream that is used to communicate to theclient prior to forwarding the request.

  • 8/14/2019 2 JSP-II

    29/32

    Allows static or dynamic resource to be included in the

    current JSP page at the request processing time.

    Similar to RequestDispatchers include() method.

    The argument can be extracted by using

    request.getParameter(p1) in servlet/jsp.

  • 8/14/2019 2 JSP-II

    30/32

    and

    1. 1.

    2. Done at the request processing

    time 2. Done at the translation time.(That is,

    the contents of the included file will beincluded in the JSP source at the point

    where the tag is defined and therefore

    processed by the JSP page

    compilation procedure.

    3. Both static and dynamic contentcan be included.

    3. The included file may contain both

    static content and other JSP tags.

    E l

  • 8/14/2019 2 JSP-II

    31/32

    Example:

    Forward action test page

    Forward action test page

    Please enter your username:


    and password:

    input type=submit value=Log in>

    forward.html

    jsp/4.forward

    forward.jsp

  • 8/14/2019 2 JSP-II

    32/32

    forward.jsp

    Forward action test: Loginsuccessful!

    (h1>Forward action test: Loginsuccessful!

    Welcome,

    forward2.jsp