Java Server Pages

Java server pages is a server-side program which is similar to Java Servlets in design and functionality. JSP is written within HTML with scripting elements, directives, and actions. JSP program statements are written using Java programming language within HTML Scripting elements. JSP offers same features as Java Servlet because JSP is converted to a Java Servlet the first time JSP page is requested by the client. There are three methods that are implicitly called when the JSP page is requested.
jspInt() :
service() :
jspDestroy() :

 

 

Watch the video tutorial for detailed concepts of Java Server Pages (JSP).


Java Server Pages – Introduction, Life Cycle, Variables, Methods, Control & Loop Statements
 

The above video consists of Introduction to JSP, JSP life-cycle, JSP tags, JSP Architecture, How to write JSP programs with the practical implementation of the JSP program, variables, Methods & control statements and loops in JSP.

These three methods form the part of the JSP lifecycle. The JSP life cycle is defined as the process from its creation until the destruction and is similar to the life cycle of a servlet. The first phase of the JSP life cycle is

JSP Initialization

When a container loads a JSP it invokes the jspInit() method before servicing any requests. Typically, initialization is performed only once. jspInt() method is identical to init() method of Java Servlet and is called the first time JSP page is requested and used to initialize objects and variables that are used throughout the life of the JSP page.

JSP Execution

This phase of the JSP life cycle represents all interactions with requests until the JSP is destroyed. Whenever a browser requests a JSP and the page has been loaded and initialized, the JSP engine invokes the JSP Service() method in the JSP. The JSP service() method is similar to service() method of Java Servlet and implicitly called and retrieves a connection to HTTP. The JSP Service() method of a JSP is invoked on a request basis. This is responsible for generating the response for that request.

JSP Shutdown

The destruction phase of the JSP life cycle represents when a JSP is being removed from use by a container. The JSP Destroy() method is identical to destroy() method of Java Servlet and is automatically called when JSP page terminates normally. We can Override jspDestroy method when we need to perform any cleanup, such as releasing database connections or closing open files

 

JSP programs consist of a combination of HTML and JSP tags. JSP Tags define Java code that is executed before the output is sent to the browser. The JSP tags begin with <% followed by Java Code and end with %>

 

There are five types of JSP tags:

Comments Tag: This tag opens with <%– and closed with –%> within this tag is the description of the comment.

Declaration Tags: The declaration tag opens with <%! , followed by the declaration of variables, objects and methods. Initialization can also be performed. The declaration closed with %>

Directive Tags: The directive tag opens with <%@. This is used for performing specific tasks such as importing java packages. It closes with %>. There are three commonly used directives which we will see in the next slide.

Expressions Tag: This opens with <%= and is used for expression statements. It closes with %>

Scriptlet Tags: This opens with <% and contains java statements. It closes with %>

 

As said earlier the directive tag opens with <%@  and is used to perform specific tasks. There are three commonly used directives.

Page directive
include directive
taglib directive.

 

The page directive is used to import java packages. Include directive is used for inserting a specific file into a jsp program, Taglib directive is used to specify the file that contains a tag library

 

The usage of these directives are as follows:

For page directive, an attribute import is used which is used to specify java packages
For include directive, an attribute file is used to specify the file that is to be included in the JSP program. It can be any file such as HTML or JSP
For taglib directive, an attribute URI is used to specify the location of the tag library defined.

 

Request Object:

The JSP request is an implicit object of type HttpServletRequest i.e. created for each jsp request by the web container. It can be used to get request information such as parameter, header information, remote address, server name, server port, content type, character encoding etc

 

Response Object:

In JSP, a response is an implicit object of type HttpServletResponse. The instance of HttpServletResponse is created by the web container for each jsp request.

It can be used to add or manipulate response such as redirect response to another resource, send error etc.

 

Session Object:

In JSP, a session is an implicit object of type HttpSession.The Java developer can use this object to set, get or remove attribute or to get session information.

 

Watch the video tutorial for detailed concepts of Java Server Pages (JSP).

Java Server Pages – Implicit Objects, Request, Cookies & Sessions
 

The above video consists of JSP implicit objects, Request Object, Cookies and Sessions in JSP.