Wednesday, October 13, 2010

JSP Implicit Object in jsp


Jsp-implicit object


Introduction:
                        Implicit object means pre-defined object. Jsp provide some implicit object. Already jsp developer has created some object for later use. This object is called implicit object.

What is oops?

Object Oriented Programming is main feature of java. In java every task is performed within the method. The method is defined within the class. To call this method, using object. So we must create object of this class and use new operator to create object. Using object, we call method. So it is called object oriented programming.

A). Create class
B). Create method
C). Create object in main class
D). Using object, call the method.

But java developer feels that some object are needed often. So they created class and created object themselves. So we don’t need to create object separately. We just use object name.method name. Here we keep it in mind which method is used for which purpose.

Note:
            How to call pre defined method?

All predefined method are included within the class. And that class also is included into the package. So we import the package and create the object of the class. Using created object, we can call the method.

For example, we want to get the time. First we import java.util package. Because the method getHours() is included in java.util package. So we include java.util package and create the object for Date class.

Java.util.Date



getHours()

import java.util.*;
class Example
{
            public void display()
{
                        Date d=new Date();
                        System.out.println(d.getHours());
}
}
class Examplemain
{
            public static void main(String args[])
            {
                        Example e=new Example();
                        e.display();
            }
}

Here we import java.util and create object for Date class and using object of the Date class to call the getHours() method.

But if we use predefined boject, there is no need to create object to call method.


They defined eight pre defined object.

1). request
2). response
3). session
4). out
5). application
6). config
7). pageContext
8). page

                                                     

 

Implicit Object


 

type



request


javax.servlet.http.HttpServletRequest(For Http)

javax.servlet.ServletRequest(Not Http)

It is used to retrieve data submitted along with a request.

       

                                   


Implicit Object



type




response


javax.servlet.http.HttpServletResponse(For Http)

javax.servlet.ServletResponse(Not Http)

It is used to send an HTML output to the client.



Implicit Object




type




session



javax.servlet.http.HttpSession
·       session object is associated with request object.



Implicit Object




type





out


javax.servlet.jsp.JspWriter


·       Used for writing data to the output buffer
·       Buffer size can be adjusted or can be turned off using buffer attribute in page directive
·       out object is used in scriptlets
        


Implicit Object




type





exception



java.lang.Throwable


·       refer to the runtime exception



Implicit Object




type




page

java.lang.Object


  • Represent the this object for the current instance of the jsp



Implicit Object




type





pageContext



Javax.servlet.jsp.PageContext


·       represent the PageContext object for this page.



Implicit Object




type





config


Javax.servlet.ServletConfig


·       Contain servlet configuration data

 

Implicit Object




type





application


javax.servlet.ServletContext


·        It is used to share information among all usrs of a currently active application





Tuesday, October 12, 2010

JSP- Custom Tag- Body with Attributes


JSP- Custom Tag- Body with Attributes

Introduction:

From Previous post, we have understood how to create custom body tag. From this post, we will learn how to create custom tag with body and attributes. If we create attribute, we will add attribute name when we create tag Handler class.

Procedure:

Step 1:
            Create Project CustomTagProject.

Step 2:
            Create TLD file named as ABody.tld


Successfully Abody.tld file created.
 
Step 3:
            Create Tag handler Class named as Abody

1). Put Tag Handler name i.e. class name as Abody

2). BodyTagSupport in Tag Support Class to Extend


























Click Next

1). Select TLD file.
2). Body Content as JSP
3) To add attribute.

               Click New

                        Add New Attribute window will open.

                        Where you give Attribute name as username

                        And select Attribute Type as java.lang.String

            Select required attribute checkbox


Successfully Tag Handler class file created.
 
Step 4:

            Made some changes in ABody.java

package javaclass;

import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class ABody extends BodyTagSupport
{
    private String username;

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public int doAfterBody()throws JspException
    {
        try
        {
            BodyContent bc=getBodyContent();
            String body=bc.getString();
            JspWriter out=bc.getEnclosingWriter();
            out.println(getUsername().toUpperCase());
            out.println(body.toUpperCase());
        }
        catch(Exception e)
        {

        }
        return SKIP_BODY;
    }
}
Step 5:

            Create jsp file named as ABody.jsp


Step 6:

Run the jsp file named ABody

Step 7:

Output

Structure of the CustomTagProject



































Monday, October 11, 2010

JSP- Custom Tag- Body with Attributes


JSP- Custom Tag- Body with Attributes

Introduction:

From Previous post, we have understood how to create custom body tag. From this post, we will learn how to create custom tag with body and attributes. If we create attribute, we will add attribute name when we create tag Handler class.

Procedure:

Step 1:
            Create Project CustomTagProject.

Step 2:
            Create TLD file named as ABody.tld

JSP Custom Tag with Body


JSP- Custom Tag with Body

Introduction:

Custom tags include jsp components such as text, jsp scriptlets, jsp expression within its body tag are called custom tags with body.

< good:GoodMorning >
suresh goodmoring
< /good:GoodMorning >

The output is the body content is converted into the UpperCase.

We specify custom tag contain body when we are creating TLD file. When we create TLD file, we specify < body-content > element of the TLD as JSP. When we create Custom Tag .we specify < body-content > as empty. Now we are going to create custom tag with body so we specify < body-content > as JSP to indicate that container evaluate the body content of the tag.




Note:
We specify < body-content > as tagdependent, the jsp container should not evaluate the body of the tag but instead pass it to the tag handler for evaluation.



Problem:

< good:GoodMorning >
suresh goodmoring
< /good:GoodMorning >

The output is the body content is converted into the UpperCase.

Procedure:

Step 1:

Create Project named as CustomTagProject (Already we have Created CustomTagProject)

If you are new to Custom Tag, Please refer Custom tag Empty Tag Creation.

Custom Tag – Example For Empty Tag


Step 2:

Create TLD file. Named good.tld

How to create TLD, please refer 

























Important:


No need to include coding into the TLD file. The content of the TLD file will be automatically included into the TLD file when we create Tag handler Class.



Step 3:

            Create Tag Handler Class named as GoodMorning.

Select BodyTagSupport because we are going to create Custom tag with body.





















































Note: we have selected Body Content as JSP.

Step 4:

            Create jsp file named as good.jsp.

< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< %@taglib uri="/WEB-INF/tlds/good" prefix="good" % >
< html >
< head >
< title > JSP good Page < /title >
< /head >
< body >
< h1 > Hello World! < /h1 >
< good:GoodMorning >
sureshkumar
< /good:GoodMorning >
< /body >
< /html >

Step 5:

            Run the good.jsp

Step 6:

            Get Output



Step 7:

            Structure of the CustomTagProject

JSP Custom Tag With Attributes


JSP- Custom Tag with Attributes


Introduction:
                       Custom tag contains many attributes. For every attribute of custom tag, we need to create a property. We use getXXX() and setXXX() methods to access and set the value for the property in the Tag handler Class.

                       Example:


< hello:Hello username=”sureshkumar”/ >

                      
                       

Here, username is a property of the custom tag. And the value of the username is sureshkumar.


Let us go to create custom tag with attributes.

We use TagSupport class to implement custom tag with attributes.

We include username attributes into the Tag Handler Class.

We also setXXX() for username as setUsername().

We also getXXX() for username as getUsername().

To define a custom tag with attribute in a TLD file, we need to specify that the custom tags contain attributes in the attribute elements of the TLD file. The attribute elemtn contains the following sub-elements.

name: specify the name of the attribute.
required: specify that the attribute is required or optional. If you specify required as true,the attribute value must be present in the JSP page.
rtexprvalue:specify that run-time expression, for example JSP expression can be used for this attribute. Possible  values include true,false,yes,no.


Procedure:

Step 1:
            Create project (Already we have create project named as CustomTagProject)

            You want to know how to create Project to develop the jsp page.
            Refer Following Link

 

Create Custom Tag – Empty Tag Example




Step 2:
            Create TLD file. Named hello.tld


























Step 3:
Create Tag Handler Class named as Hello

Already we have created Welcome Tag Handler Class in creating Empty custom tag example. Follow same steps to create Tag Handler Class.


Below display window is a Tag Handler window. Where you give Tag Handler class name such as Hello.

Tag Support Class to Extend:

Select SimpleTagSupport



Next

Below display window is a New File window, where you select TLD file.

Select Body content as Empty.

Add attributes from here

Click New button right side of the Attribute.

Below window display






















OK





 Finish. Tag Handler named Hello will be successfully created.
Note: Made some changes in Hello.java.
Extends TagSupport class.
Add doStartTag() and doEndTag()

Hello.java


package javaclass;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import javax.servlet.jsp.tagext.TagSupport;

public class Hello extends TagSupport
{
    private String username;

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }
    public int doStartTag() throws JspException
    {
        try
        {
           JspWriter out=pageContext.getOut();
           out.println("Welcome"+getUsername());
        }
        catch(Exception e)
        {

        }
        return SKIP_BODY;
    }
    public int doEndTag() throws JspException
    {
        return SKIP_PAGE;
    }
}
                 
Step 4:
             Create jsp file named as Hello.jsp



< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< %@taglib uri="/WEB-INF/tlds/hello" prefix="hello" % >
< html >
< head >
< title > Hello JSP Page < /title >
< /head >
< body >
< h1 > Hello World! < /h1 >
< hello:Hello username="sureshkumar"/ >
< /body >
< /html >


Step 5:

Run the Hello.jsp

Step 6:

Get Output




















Structure of the CustomTagProject



Sunday, October 10, 2010

JSP Custom Tag Empty Custom Tag


JSP- Custom Tag- Empty Tag.

Introduction:

                        Empty custom tag means that does not have any attributes or body.


< mytag:hello/ >


Creating Custom Tag-Empty Tag:


To create custom tag, three components need.

Ø      The tag library descriptor file that maps the XML elements to the tag implementation.

Ø      The tag handler class that defines tags behavior.


Ø      The JSP file that uses the tag library
                

Procedure:

Step 1:
                       Create Project named as CustomTagProject.
                 

Note:Already we have seen how to create the project for developing the web application.


Step 2:
Create two Folders. One for storing jsp files. Another one for storing   Tag Handler class.


Note: Already we have seen how to create Folder in the previous post. Also we have seen where to create Folder for storing jsp file and where to create Folder for storing tag handler class.



Structure of the CustomTagProject




















You can save jsp file under jsp folder. And also you can save the tag handler class under javaclass Folder.


Task 1:Tag Library Descriptor file (TLD file)

First task is to identify the java class for the server and to associate it with a particular XML tag name. Let us create TLD file.

Let us see how to create TLD files.

We give TLD file name as myown-taglib.tld

Step1:
Right click on WEB-INF and select New and Other





























New File window will open.

Select web under Categories


Also select Tag Library Descriptor(TLD) under File Types

 

Next


New Tag Library Descriptor window will open.

You give TLD name as welcome.tld

Also give prefix name as wel

























After creaing welcome.tld. you can see welcome.tld file like below.

welcome.tld


< ?xml version="1.0" encoding="UTF-8"? >
< taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd >
< tlib-version > 1.0 < /tlib-version >
< short-name > wel < /short-name >
< uri > /WEB-INF/tlds/welcome < /uri >

< /taglib >


















Task 2:Tag Handler Class


Ø      The second task is to define a java class that tells the JSP ENGINE what to do when it sees the tag.

Ø      This class must implements javax.servlet.jsp.tagext.Tag interface.

Ø      Extends TagSupport or BodyTagSupport

Let us go to create java class that display “welcome India”
Right click on javaclass. And select New and Other.































New File window will open.

Where you select web under categories

And also select Tag Handler under File types

























Next


New Tag Handler window will open.
You give Tag Handler name as Welcome.



Next


New File window will open.



































Where you select TLD

Browse and select TLD under WEB-INF\tlds\welcome.tld

Select Body Content: empty

Finish


Welcome.java file is successfully created.

Welcome.java



package javaclass;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import javax.servlet.jsp.tagext.TagSupport;

public class Welcome extends TagSupport
{

    public int doStartTag() throws JspException
    {
        try
        {
            JspWriter out=pageContext.getOut();
            out.println("Welcome India");
        }
        catch(Exception e)
        {

        }
        return SKIP_BODY;
    }
    public int doEndTag() throws JspException
    {
        return SKIP_PAGE;
    }

}


Create JSP file


Create jsp file  -- Home.jsp

Let us see how to create Home.jsp

Right click on jsp Folder.



































New File window will open.

Where you select web under categories and select JSP under File Types.


























Next

New JSP File window will open.




































Finish

Home.jsp file is successfully created.

In Home.jsp, we include taglib directive.

How to include Taglib directive.

< %@taglib uri="/WEB-INF/tlds/welcome" prefix="wel" % >

We create jsp file. We define taglib tag to include custom tag into jsp.


Include following jsp coding into the Home.jsp

< %@taglib uri="/WEB-INF/tlds/welcome" prefix="wel" % >
< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >

< html >
< head >
< title > JSP Home Page < /title >
< /head >
< body >
< h1 > Hello World! < /h1 >
< wel:Welcome/ >
< /body >
< /html >


Run the Home.jsp

Right click on Home.jsp and select Run File.


























You get output
























Strutcture Of The CustomTagProject