You are on page 1of 30

Java Server Pages

Objectives
At the end of this session, you will be able to: Distinguish JSP architecture vis--vis servlets Define and use the basic JSP Elements Create and use Java Beans Work with Cookies Define Session Tracking and use the same in JSPs

What is JSP
JSP stands for Java Server Pages, a technology invented by Sun to allow the easy creation of server side HTML pages. Can be used as both a kind of Dynamic HTML and CGI replacement

JSP Request Model

The above picture brings a perspective on where JSP fits in the 3-tier web application architecture. JSP lies in the presentation tier on the web server, with the main responsibility of generating HTML content that needs to be served to the browser. It also has the additional responsibility of pass on the requests to the backend through the JavaBeans, as and when required.
5

What makes JSP so attractive?


High performance Convenient to code and maintain as against servlets Separates presentation from content Powered by Java

Has access to all Java APIs Has access to all J2EE APIs Inherent Platform independence

JSP compared to ASP


Similarities Both provide built in session tracking Designed to create interactive pages in a Web application Separate presentation logic from programming logic Differences JSPs are designed for Platform independence and server independence Open development process, has come through the JCP (Java Community Process) and hence broader acceptance JSP enables developers to extend the tags JSP uses a full fledged language like Java for scripting while ASP uses VBScript or Jscript, which are limited
7

The Architecture

Typically HTTP requests are sent to the web server from a browser client. If the request if for a static HTML page, the web server itself responds. However, if the request is for a JSP or a servlet, the add-ons to a web server viz. servlet engine or JSP engine respond. These engines require a JVM as, they are themselves Java-based.
9

The flow of JSP request

10

The flow of a JSP request:


1. The HTTP request comes to a web server. 2. The server extension receives the request (as it is not a request for static HTML but for a JSP) and passes it on to the JSP engine. 3. The JSP engine invokes the JSP parser to parse the JSP and check for any syntax errors. 4. On successful parsing, the JSP engine invokes the Java compiler to compile the JSP into an equivalent servlet class. 5. Once the class is generated, the control is passed on to a servlet engine. 11

Elements of JSP
The three basic elements of a JSP are: Scripting elements Directives Actions

12

Scripting Elements
Lets you insert Java code into the servlet that will be generated from the current JSP page There are three forms: - Expressions - Scriptlets - Declarations
13

Scripting elements
1. Declarations <%! %> 2. Scriptlets <% %> 3. Expressions <%= Object.toString() %>
14

Predefined Variables
To simplify the expressions, there are a number of predefined variables that you can use The most important ones are:

request response session out


15

Scriptlets
Are defined as any block of valid Java code that resides between <% and %> tags Code that is defined within a scriptlet can access any variable and any beans that have been declared
16

Declaration
Used to define methods or fields that get inserted into the main body of the servlet class

It has the form: <%! Java Code %>


The scope of a declaration is usually a JSP file, but if the JSP file includes other files with the include directive, the scope expands to cover the included files as well
17

Directives
Affect the overall structure of the servlet class generated from this JSP. format: <%@ directive attribute="value" %> There are two main types of directives:

page include
18

Page Directives
Defines attributes that apply to an entire JSP page

Lets you do things like:


-

Import classes Handle error messages Define if the JSP is thread-safe Define is session object is available Set the page content type
19

The Include Directive


Inserts the contents of another file in the main JSP file, where the directive is located

Useful for including copyright information, scripting language files, or anything you might want to reuse in other applications
The included file can be an HTML file, a JSP file, a text file, or a code file written in the Java programming language
20

Actions
Control the behavior of the servlet engine You can dynamically insert a file, reuse JavaBeans components Available actions include: jsp:include - Include a file at the time the page is requested.

jsp:useBean - Find or instantiate a JavaBean. jsp:setProperty - Set the property of a JavaBean. jsp:getProperty - Insert the property of a JavaBean into the output.

jsp:forward - Forward the requester to a new page.


jsp:plugin - Generate browser-specific code that makes an OBJECT or EMBED tag for the Java plug-in.

Remember that, as with XML in general, the element and attribute names are case sensitive.
21

The jsp:include Action


Lets you insert files into the page being generated

The syntax looks like this: <jsp:include page="relative URL" /> Unlike the include directive, which inserts the file at the time the JSP page is translated into a servlet, this action inserts the file at the time the page is requested
22

The jsp:forward Action


Forwards a client request to an HTML file, JSP file, or servlet for processing. Syntax <jsp:forward page= relativeURL" />
23

The jsp:useBean Action


Lets you load in a JavaBean to be used in the JSP page The simplest syntax for specifying that a bean should be used is: <jsp:useBean id="name" class="package.class" /> This usually means "instantiate an object of the class specified by class, and bind it to a variable with the name specified by id."
24

<jsp:getProperty>
Converts property names following the bean standards Has two attributes:
-

name="beanInstanceName"

The name of the Bean instance as declared in a <jsp:useBean> tag The name of the Bean property whose value you want to display
25

property="propertyName"

<jsp:setProperty>
Sets the value of one or more properties in a JavaBean component Syntax:
<jsp:setProperty name="beanInstanceName" property= "*" | property="propertyName" [param=parameterName" ] | property= "propertyName" value="{ string | <%= expression %> }" } />
26

Properties of JSP:setProperty
name property value param

27

Cookies
Allow the web server to store small pieces of data on the client that can be sent back to the server on subsequent page requests Are often used to store user IDs or basic configuration information To read cookies from the browser, use the request.getCookies() function
28

Session Management
JSP maintains session through the HttpSession object

Session information is stored on the server, and a session ID number is stored in a cookie on the client machine
Sessions are usually set by server default to expire after 30 minutes of user inactivity
29

Summary
In this section, you have learnt to: Distinguish JSP architecture vis--vis servlets Define and use the basic JSP Elements Create and use Java Beans Work with Cookies Define Session Tracking and use the same in JSPs

30

You might also like