Home Tutorial Java Jsp JSP BASICS

 
 

Share on Google+Share on Google+
JSP BASICS
Posted on: June 9, 2010 at 12:00 AM
Advertisement
This section is about JSP & it's page components, their usage and syntax.

JSP BASICS

What is JSP ?

JSP Stands for "Java Server Pages". Using "JSP" we can use both, static HTML with dynamically-generated HTML. Web pages created using CGI programs are mostly static , dynamic part is limited to a few small locations. But using CGI and servlet , you can generate entire page through one program. Using JSP, you can built two parts separately.
                          JSP is the product of Sun Microsystems Inc.JSP has more advance features than Servlet . JSP separates the presentation logic from the business logic ,provide facility to developers to work separately without any trouble.JSP have the properties of Cold fusion and ASP and hence provide the flexibility to embed the business logic efficiently within the HTML content (presentation logic). 

Component of JSP pages :
  • Directives
  • Declarations
  • Scriplets
  • Expressions
  • Standard Actions

1. DIRECTIVES

A directive element in a JSP page provides global information about a particular JSP page and is of three types:

  • Page directive
  • Include directive
  • Taglib directive

The syntax for defining a directive is :  <%@ directive attribute=?value? %>

 

  • Page directive :

Defines attributes that notify the Web container about the general settings of a JSP page.

The syntax of the page directive is  :  <%@ page attribute_list %>

The following table describe the attribute supported by the page directive :

Attribute name             Description
Language

Defines the scripting language of the JSP page

extends Defines the extended parent class of the JSP
generated servlet
imports Imports the list of packages, classes, or interfaces into
 the generated servlet.
session Specifies if the generated servlet can access the session
 or not. An implicit object, session, is generated if the
value is set to true. The default value of session attribute
 is true.
buffer Specifies the size of the out buffer. If size is set to none,
 no buffering is performed. The default value of buffer
size is 8 KB.
autoFlush Specifies that the out buffer be flushed automatically if the
 value is set to true. If the value is set to false, an exception
 is raised when the buffer is full. The default value of
autoFlush attribute is true.
isThreadSafe Specifies whether a JSP page is thread safe or not.
errorPage Specifies that any un-handled exception generated will be
directed to the URL.
isErrorPage Specifies that the current JSP page is an error page, if
the attribute value is set to true. The default value of 
isErrorPage attribute is false
contentType Defines the Multipurpose Internal Mail Extension (MIME)
 type for a response. The default value of the
contentType attribute is text/html.
  • Include directive :

Specifies the names of the files to be inserted during the compilation of the JSP page The included file can be an HTML file, a JSP file, a text file, or a code file written in the Java programming language. Creates the contents of the included files as part of the JSP page.

The syntax of the include directive is:      <%@ include file = ?relativeURL? %>  

  • Taglib directive

 Imports a custom tag into the current JSP page.

  • Associates itself with a URI to uniquely identify a custom tag.

  • Associates a tag prefix string that distinguishes a custom tag with the other tag library used in a JSP page.

  • The syntax to import a taglib directive in the JSP page is       <%@ taglib uri=?tag_lib_URI? prefix=?prefix? %>

The following table describe the attribute of the taglib directive :

Attribute Name Description
uri Locates the TLD file of a custom tag.
prefix Defines a prefix string to be used for
distinguishing a custom tag instance
2. DECLARATION : Provide a mechanism to define variables and methods. Declarative statements are placed within <%! and %> symbols and always end with a semicolon.
                         Syntax:   <%!   Declare  all the variables here   %>

3.SCRIPTLETS :
Consists of valid Java code snippets that are enclosed within <% and %> symbols. The syntax to declare JSP scriptlets to include valid Java code is:       <% Java code %>

4. EXPRESSION :

Insert values directly into the output. The syntax to include a JSP expressions in the JSP file is:  
<%= expression%>.

5.STANDARD ACTIONS :

Syntax:

Include :   <jsp:include page =?<filename>? />

This inclusion of file is dynamic and the specified file is included in the JSP file at run-time i.e. out put of the included file is inserted into the JSP file.

Forward : <jsp:forward page=?<filename>? />

This will redirect to the different page without notifying browser.

6. IMPLICIT OBJECTS

JSP Implicit Objects are:

? Pre-defined variables that can be included in JSP expressions and scriptlets.

? Implemented from servlet classes and interfaces.

OBJECT                CLASS

Out                          JSP writer

Request                   HttpServletRequest

Response                HttpServletRespose

Session                    HttpSession

Application              ServletContext

Config                     Sevlet Config

Page                       Object

PageContext           Page Context => is responsible
                               for generating all other implicit objects.


Out:

This object is instantiated implicitly from JSP Writer class and can be used for displaying anything within delimiters.

For e.g. out.println(?Hi Buddy?);

Request:

It is also an implicit object of class HttpServletRequest class and using this object request parameters can be accessed.

For e.g. in case of retrieval of parameters from last form is as follows:

request.getParameters(?Name?);

Where ?Name? is the form element.         

Response:

It is also an implicit object of class HttpServletResponse class and using this object response(sent back to browser) parameters can be modified or set.

For e.g. in case of modifying the HTTP headers you can use this object.

 Response.setBufferSize(?50?);

Session:

Session object is of class HttpSession and used to maintain the session information. It stores the information in Name-Value pair.

For e.g.

 session.setValue(?Name?,?Jakes?);
 session.setValue(?Age?,?22?);

Application:

This object belongs to class SevletContext and used to maintain certain information throughout the scope of Application.

For e.g

Application.setValue(?servername?,?www.myserver.com?); 

PageContext:

This object is of class pageContext and is utilized to access the other implicit objects.

Display "Hello JSP" using Jsp expression
In this section, We will simply output "Hello JSP" in a web browser using Jsp expression.
 
Display current time & system properties using JSP expressions
In this Section ,we will display current time and system properties using JSP expressions.
 
JSP Scriptlets Example
In this section, we will understand Scriptlets with the help of an Example.
 
Language' attribute of JSP page directive
In this Section , we will discuss about the "language" attribute of a JSP page & its use in JSP page.
 
"import" Attribute of page directive
In this Section , we will discuss about the "import" attribute of a JSP & its use in JSP page.
 
Mixing Scriptlet and HTML
In this section, we will discuss about Scriptlet and Html together with an example
 
The 'session' Attribute of JSP page Directive
This section, illustrate about 'session' attribute of page directive with help of a example. It specifies if the generated servlet can access the session or not
 
The 'buffer' and 'autoFlush' Attribute of JSP page directive
This Section illustrate about the 'buffer' & 'autoFlush' attribute of JSP page directive.
 
The "isThreadSafe" & "info" Attribute of JSP page directive
In this section, we will learn about "isThreadSafe" & "info "attribute of JSP page directive
 
The "errorPage" & "isErrorPage" Attribute of Page Directive
In this Section, we will discuss about the "errorPage" & "isErrorPage" attribute of Page Directive.
 
The "contentType" Attribute of Page Directive
In this Section, we will discuss about the "contentType" attribute of page directive with an example.
 
The "pageEncoding" attribute of JSP page directive
In this section, we will discuss about how to set "pageEncoding" attribute & it's uses with example.
 
The "include" directive of JSP
In this Section, we will discuss about "include" directive of JSP with an example.
 
JSP Standard Actions
In this section, we will learn about JSP standard Action & their elements with some examples.
 
COMMENT & HIDDEN COMMENT IN JSP
In this section , we will learn how to implements comment & hidden comment in JSP.
 
DECLARATION IN JSP
In this Section, we will discuss about declaration of variables & method in JSP using declaration tags.
 
JSP Standard Action 'jsp:useBean'
In this Section, we will discuss about some standard actions :"jsp:usebean" with a small example.
 
JSP Standard Actions 'jsp:setProperty' & 'jsp:getProperty'
In this section , we will discuss about Standard actions - 'jsp:setProperty' & 'jsp:getProperty' with an example.
 
The "taglib" directive in JSP
In this section, we will discuss about JSP "taglib" directive with a small example.
 
Standard Action "jsp:plugin"
In this Section, we will discuss about standard action "jsp:plugin" & their implementation using a example.
 
Standard Action "jsp:param"
In this Section, we will discuss about "jsp:param" standard action & their utilization with a example.
 
JSP Implicit object "session"
In this Section , we will discuss about JSP implicit object "session" with an example.
 
JSP implicit object "request"
In this section, we will discuss about the implicit object "request" and its uses with an example.
 
JSP implicit object out
In this Section, we will discuss about implicit object out & it's methods.
 
JSP implicit object "response"
In this Section, we will discuss about implicit object out & it's methods.
 
JSP implicit object "application"
In this section, we will discuss about JSP implicit object 'application' and it's uses with an example.
 
JSP implicit object "pagecontext"
In this Section,we will discuss about "pagecontext" implicit object and their uses with an example.
 
Cookies in JSP
In this section, we will discuss about handling cookies in JSP with an example.
 
Insert a row in 'Mysql' table using JSP Code
In this section, we will discuss about how to insert data in Mysql database using JSP code.
 
Deleting Cookie in JSP
In this section, we will discuss about deleting cookies in JSP with an example.
 
Form processing using Bean
In this section, we will create a JSP form using bean ,which will use a class file for processing.
 
JSP custom tags (user define tags)
In this section , we will discuss about how to create "custom" tags & use it in JSP programs.
 
Display Current Date using JSP Custom(user define) Tag
In this section, we will discuss about how to display current date & time using custom(user define) tag.
 
Change Background color using Custom Tag
In this program, We will create a Custom Tag which changes the background color.
 
Insert Blob(Image) in Mysql table using JSP
In this Section,we will insert blob data(image) in Mysql database table using JSP code.
 
Display Blob(Image) from Mysql table using JSP
In this section, we will display blob data(image) from Mysql database table using JSP code.
 
Jstl c:if tag in jsp
In this Section, we will discuss about how to use JSTL tag ' c:if ' in JSP with a simple example.
 
Jstl c:when,c:choose,c:otherwise tag in jsp
In this Section, we will discuss about how to use JSTL tag ' c:when ' in JSP with a simple example.
 
Track user's session using 'session' object in JSP
This section is about tracking user identity across different JSP pages using 'session' object.
 
Tracking Session using URL rewriting
In this section we will discuss about tracking user's session using URL rewriting using a simple example.
 
Tracking User's Session using Hidden Form Fields
In this Section, We will discuss about tracking user's session using Hidden form field.
 
Embed Flash file in JSP
In this Section, we will discuss about how to insert a flash file in JSP.
 
Replace a String with another using "fn:replace" tag of JSTL
In this Section, we will replace a String with another string using "fn:replace" tag of JSTL
 
The getQueryString() method of JSP
In this Section, we will learn about using "getQueryString() " method to retrieve the 'query string'.
 
The fn:toUpperCase & fn:toLowerCase tags of JSTL
In this section ,we will learn how to use fn:toUpperCase and fn:toLowerCase Tag of JSTL.
 
Advertisement

Related Tags for JSP BASICS :


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 9, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com