JSP BASICS


 

JSP BASICS

This section is about JSP & it's page components, their usage and syntax.

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:

0

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?);

1

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.

2

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

 Response.setBufferSize(?50?);

Session:

3

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?);

4

Application:

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

For e.g

5

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

PageContext:

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

6

Ads