Professional Documents
Culture Documents
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
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
Has access to all Java APIs Has access to all J2EE APIs Inherent Platform independence
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
10
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:
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
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
Import classes Handle error messages Define if the JSP is thread-safe Define is session object is available Set the page content type
19
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.
Remember that, as with XML in general, the element and attribute names are case sensitive.
21
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
<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