evolution of jsp€¦ · package the javax.servlet.jsp.jsppage interface contains 2 methods public...

103
Evolution of JSP CGI-Common Gateway Interface Complex Process management Not Scalable Servlet Java code JSP No java code Embed with HTML Anand & Bhautik

Upload: others

Post on 25-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Evolution of JSP

CGI-Common Gateway Interface Complex Process management

Not Scalable

Servlet Java code

JSP No java code

Embed with HTML

Anand & Bhautik

Page 2: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

JSP

Java Server Page is a server-side program

that is similar in design and functionality to a

Java Servlet.

JSP file has extension .jsp

JSP tags <% %>

Anand & Bhautik

Page 3: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

How JSP file is executed?

Web server

Servlet Engine

JSP Engine

JSP documents

Compiled

servlet

HTML

documents

HTTP Request HTTP Response

Anand & Bhautik

Page 4: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Steps:

The client sends request to the browser

The server sends JSP file to JSP engine

It converts to servlet, compile for the first time and then load.

The servlet runs and generates HTML

The server sends HTML to browser

Anand & Bhautik

Page 5: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

JSP vs ASP

JSP are translated once to byte code.

They are interpreted when file is

modified.

ASP are compiled every time

JSP runs on all web servers

ASP runs on Microsoft web server

cc

Anand & Bhautik

Page 6: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

JSP Vs Servlet

JSP is on top of servlet API.

Servlet provides dynamic contents

using java code

JSP also provides dynamic contents

but is simpler to write than servlet

Anand & Bhautik

Page 7: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

JSP Life Cycle

The lifecycle of JSP is controlled by three

methods which are automatically called

when a JSP is requested and when the

JSP terminates normally. These are:

jspInit ()

_jspService()

jspDestroy()

Anand & Bhautik

Page 8: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Init()

jspInit() method is identical to the init()

method in a Java Servlet and in applet.

It is called first when the JSP is requested

and is used to initialize objects and

variables that are used throughout the life

of the JSP.

Anand & Bhautik

Page 9: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Service()

_jspService() method is automatically

called and retrieves a connection to HTTP.

It will call doGet or doPost() method of

servlet created.

Anand & Bhautik

Page 10: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Destroy()

jspDestroy() method is identical to the destroy() method in Servlet.

The destroy() method is automatically called when the JSP terminates normally.

It isn’t called by JSP when it terminates abruptly.

It is used for cleanup where resources used during the execution of the JSP ate released, such as disconnecting form database.

Anand & Bhautik

Page 11: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Package

The javax.servlet.jsp.JspPage interface contains 2 methods

public void jspInit()

It is invoked when JSP is first initialized.

public void jspDestroy()

It is invoked when JSP is about to be destroyed by the container.

The javax.servlet.jsp.HttpJspPage interface contains 1 method

public void _jspService(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException

Anand & Bhautik

Page 12: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Life cycle

Initialize

public void jspInit()

Destroy

public void jspDestroy()

Handles client requests and generate responses

void _jspService(HttpServletRequest req,HttpServletResponse)

throws ServletException, IOException

Anand & Bhautik

Page 13: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

JSP Elements

Element is that part of code which is

processes on the server. There are three

types of JSP Elements:

Directive Elements

Scripting Elements

Action Elements

Anand & Bhautik

Page 14: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Directive Elements:

JSP directive serve as message to the JSP

container for the JSP.

They are used to set global values such as class

declaration, methods to be implemented, output

content type etc.

They do not produce any output to the client.

All directives have scope of the entire JSP file.

Anand & Bhautik

Page 15: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Syntax

Directives are characterized by @ character

within the tag.

Syntax:

<%@ directivename attribute=”value”

attribute=”value” %>

Anand & Bhautik

Page 16: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Types of Directive

The two directives are:

The page directive

The include directive

Anand & Bhautik

Page 17: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Page Directive

The page directive defines a number of

important attributes that affect the whole page.

A single JSP can contain multiple page

directives.

There can be only one occurrence of any

attribute/value pair defines by page directive

except import attribute.

<% @ page ATTRIBUTES %>

Anand & Bhautik

Page 18: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Page directive Attributes

Attribute Description Default

Language Defines the scripting language to be

used.

“java”

Extends It is fully qualified class name of super class

that the generated class must extend

Omitted

Import Comma separated list of packages or classes. Omitted

Session Specifies if the page participated in an

HTTP session

“true”

Buffer If value is “none” then no buffering occurs

and all o/p is written directly. If buffer size is

specified then o/p is buffered with buffer size

not less than that specifed

8KB

Anand & Bhautik

Page 19: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Contd.

Autoflush If true, buffer is flushed automatically

when it is full If false, a runtime exception

is thrown to indicate buffer overflow

“true”

isThreadSafe If “false”, it processes them one at a time

implementing

singleThreadModel interface

“true”

Info Defines informative string Omitted

isErrorPage If “true”, then implicit object exception is

available which is instance of class

java.lang.Throwable

“false”

ErrorPage Define a URL to another JSP that is

invoked if an unchecked runtime

exception is thrown omitted

Omitted

contentType Define MIME type for the response of the

JSP page.

text/html

Anand & Bhautik

Page 20: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Example

<%@page language=”java”

import=”java.sql.*” buffer=”12kb”

errorPage=”error.jsp” %>

Anand & Bhautik

Page 21: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

The include Directive

This directive notifies the container to include the content of the resource in the current JSP, inline, at the specified place.

The contents of the included file are parsed by the JSP and this happens only at translation time.

The included page should not be another dynamic page.

<%@ include file=”filename” %>

Anand & Bhautik

Page 22: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Attributes Description

file The static filename to include

E.g.:

<%@ include file=”header.html” %>

Anand & Bhautik

Page 23: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Scripting Elements

Scripting elements are used to include

scripting code within the JSP.

They allow users to:

declare variables and methods

include arbitrary scripting code

evaluate an expression.

Anand & Bhautik

Page 24: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Types of scripting elements

The three types of scripting elements are:

Declaration <%! %>

Scriplets <% %>

Expression <%= %>

Anand & Bhautik

Page 25: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Declaration

A declaration is a block of java code in a JSP that

is used to define class wide variable and method in

generated class file.

Declarations are initialized when the JSP page is

initialized and have “class” expression or code.

A declaration block is enclosed between <%! %>

<%! Java variable and method declaration (s) %>

Anand & Bhautik

Page 26: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Example

<html>

<body>

<%!int i=0;

public mymethod();%>

</body>

</html>

Anand & Bhautik

Page 27: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Scriplet

A scriplet is a block of code that is executed at request processing time

.

A scriplet is enclosed between <% and %>

JSP get compiled into a servlet and all the code appearing between <% and %> in the JSP, put into the _jspService() method to this servlet.

<% Java code %>

Anand & Bhautik

Page 28: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Example

<html>

<body>

<%

for(int I=0;I<10;I++)

{

out.println(“<b> Hello”+ i +”</b>”);

}

%>

</body>

</html>

Anand & Bhautik

Page 29: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Expression

An expression is a shorthand notation for a

scriplet that outputs a value in the response

stream to the client.

When the expression is evaluated, the result is

converted to a string and displayed.

An expression is enclosed within <%= %>.

<%= Java expression to be evaluated %>

Anand & Bhautik

Page 30: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Example

<html>

<body>

<%! int i=0;%>

<%

i++;

%>

<%= “This page is accessed “ + i +”times” %>

</body>

</html>

Anand & Bhautik

Page 31: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Action Elements

Actions are specific tags that affect the runtime behavior of the JSP and affect the response sent back to the client.

The standard action types are:

<jsp:param>

<jsp:include>

<jsp:forward>

<jsp:plugin>

Anand & Bhautik

Page 32: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

<jsp:param>

The <jsp:param> action is used to

provide other tags with additional

information in the form of name value

pairs.

This action is used in conjunction with

jsp:include, jsp:forward and jsp:plugin

actions.

Anand & Bhautik

Page 33: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Attributes

Attribute Description

Name

value

The key associated with the attribute

The value of the attribute

<jsp:param name=”t1” value=”abc” >

Anand & Bhautik

Page 34: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

<jsp:include>

This action allows a static or dynamic resource to be include in the current JSP at request time.

The resource is specified using the URL format as include directive.

As included page has access only to the JspWriter object and it cannot set header and cookies. A request time exception is thrown if this is attempted.

If page is buffered then the buffer is flushed prior to the inclusion.

<jsp:include page= “filename” flush = “true” />

Anand & Bhautik

Page 35: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

<jsp:include page= “/header.jsp” flush = “true” >

<jsp:param name=”t1” value=”abc” >

</jsp:include>

A jsp:include action may have one or more jsp:param tags in its body, to provide additional name-value pairs.

The included page can access the original request object, with both the original and the new parameters.

If the parameter names are same, the old values are kept intact, but the new values take precedence over existing values. (t1=abc,def,xyz).

Anand & Bhautik

Page 36: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Attributes

Attribute Description

Filename

flush

The resource to include

The value must be true

Anand & Bhautik

Page 37: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Attributes

Include Syntax Done when Included

content

Parsing

Directive

Action

<%@include file=”filename”%>

<jsp:include page=”filename”%>

Compilation

time

Request

Processing

time

Static

Static

or dynamic

Container

Not

Parsed but

included in

container

Anand & Bhautik

Page 38: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

<jsp:forward>

The jsp:forward action allows the request to be forwarded to another JSP, a servlet or a static resource.

<jsp:forward page= “url” />

or

<jsp:forward page=”url”>

<jsp:parame name=”paramname” value=”val”/>

</jsp:forward>

Anand & Bhautik

Page 39: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

example

Execution in current JSP stops when it encounters jsp:forward tag.

The buffer is cleared. If output stream was not bufferd and some output has been written to it, a jsp:forward action will throw a java.lang.IllegalStateException.

Example

<jsp:forward page=”/search/results.jsp” />

Anand & Bhautik

Page 40: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

<jsp:plugin>

The jsp:plugin action is used to generate client browser specific HTML tags that ensures java plug-in software is available, followed by execution of the applet or Java Bean component specified in the tag.

The jsp:plugin tag can have two optional tags

<jsp:param> to pass additional parameters

<jsp:fallback> to specify the contents to be displayed if plug-in cannot be started.

Anand & Bhautik

Page 41: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Attribute Details Required

Type Applet/bean yes

code Name of class file Yes

codebase Path where class file is saved No

align Alignment on screen No

height Height of window No

width Width of window No

hspace Horizontal space No

vspace Vertical Space No

title Title of window No

name Name of window No

nspluginurl URL where JRE plugin can be downloaded for

Netscape Navigator

Only if java plugin s/w needs to

be installed

iepluginurl URL where JRE plugin can be downloaded for

internet explorer

Only if java plugin s/w needs to

be installed

Anand & Bhautik

Page 42: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Example

<html>

<body>

<jsp:plugin type=”applet” code=”app” codebase=”/classes/applet” height=”100” width=”100”>

<jsp:params>

<jsp:param name=’color” value=”black”/>

</jsp:params>

<jsp:fallback>Browser could not support</jsp:fallback>

</jsp:plugin>

</body>

</html>

Anand & Bhautik

Page 43: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Comments and Template

Data

Comments

Comments are useful to people building and maintaining pages.

They are useful to authors but useless for readers.

HTML comments are regular part of the document, it shows up if users use “view source” in the browser

This makes the download time a tiny fraction of second longer. A more serious concern is that sometimes HTML comments contain implementation details that might be confidential.

Anand & Bhautik

Page 44: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

JSP’s solution is to replace the above HTML comments with a JSP comments, like

<%-- --%>

When JSP engine sees this tag, it recognizes it as comment and does not put into the servlet it builds.

The comments will thus never be sent to the user and will not show up when the user does a view source.

Anand & Bhautik

Page 45: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Template

The index page consists of three sections:

The header

The navigation area

The content area.

Different pages have different contents but it is reasonable to except that header and navigation will be repeated over the site.

It would be headache because developers need to place everything in its place. The cure of this nightmare is called Templating.

Anand & Bhautik

Page 46: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Template is HTML page with some spaces where

text should be, and somewhere to indicate where

text can be found.

This makes it possible to keep header in exactly

one file and let each page have a space that this

file should fill.

<%@ inlcude file=/header.jsp”<tr>

Anand & Bhautik

Page 47: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Implicit Objects

JSP tries to simplify the work of developers and provides certain implicit objects accessible within the JSP.

These objects do not need to be declared or instantiated by the JSP author, but are provided by the container in the implementation class.

All implicit objects are available only to scriplets or expression and are not available in the declaration. They are only available in _jspService() method of generated servlet.

Anand & Bhautik

Page 48: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Implicit Objects

request

response

out

session

config

exception

application

Anand & Bhautik

Page 49: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

request

The request object has request scope, and

is the instance of

javax.servlet.ServletRequest.

It encapsulates the request coming from the

client and being processed by the JSP. It is

passed to the JSP by the container, as a

parameter to the _jspService() method.

Anand & Bhautik

Page 50: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

HttpServletRequest

getParameter()

getParameterNames()

getParameterValues()

getHeaders()

getQueryString()

getRemoteAddr()

getRemoteHost()

getAttribute()

setAttribute()

getAttributeNames()

getCookies()

getSession()

Anand & Bhautik

Page 51: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

response

The response object has page scope, and is the instance of class javax.servlet.ServletResponse

It encapsulates the response generated by the JSP, to be sent back to the client in response to the request.

It is generated by the container and passed it the JSP as parameter to the _jspService() method, where it is modified by the JSP.

Anand & Bhautik

Page 52: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

HttpServletResponse

getWriter()

setContentType()

addCookie()

encodeURL()

sendRedirect()

setHeader()

setStatus()

Anand & Bhautik

Page 53: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

out

The out object has page scope, and is an instance of class javax.servlet.jsp.JspWriter.

It represents the output stream opened back to the client. This is the PrintWriter used to send output to the client.

However in order to make the response object useful, this is buffered version of PrintWriter called JspWriter

Anand & Bhautik

Page 54: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

session

The session object has session scope, and is an instance of class javax.servlet.http.HttpSession.

It represents the session created for the requesting client, and is only valid for HTTP requests.

Sessions are created automatically, so this variable exists even if there was no incoming session reference.

The one exception is if user uses session attribute of the page directive to turn sessions off, in which case attempts to reference the session variable cause translation errors for the JSP.

<%@page session=“true”%>

Anand & Bhautik

Page 55: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

HttpSession

setAttribute()

getAttribute()

getAttributeNames()

removeAttribute()

getId()

getCreationTime()

getLastAccessedTime()

setMaxInactiveInterval()

isNew()

invalidate()

Anand & Bhautik

Page 56: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

config

The config object has page scope, and is the instance of class javax.servlet.ServletConfig.

It represents the servlet configuration.

Example

<%

ServletContext sc=config.getServletContext();

RequestDispatcher rd=sc.getRequestDispatcher(“/new.jsp”);

rd.forward(request,response);

%>

Anand & Bhautik

Page 57: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

ServletConfig

It is used to pass initialization and context

information to servlets

Methods

public abstract String getInitParameter(String name)

public abstract Enumeration getInitParameterNames()

Public abstract ServletContext getServletContext()

Anand & Bhautik

Page 58: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

application

The application object has application

scope, and is the instance of class

javax.servlet.ServletContext.

It represents the context within which the

JSP is executing

Anand & Bhautik

Page 59: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

ServletContext

getContext()

getServerInfo()

getInitParameter()

getInitParameterNames()

getAttribute()

setAttribute()

removeAttribute()

getRequestDispatcher()

Anand & Bhautik

Page 60: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Objects in JSP

Objects

request

response

out

session

config

application

interface

ServletRequest

ServletResponse

JspWriter

HttpSession

ServletConfig

ServletContext

Anand & Bhautik

Page 61: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Error handling in JSP

Types of error and exception

There are 2 types of exception

Translation time errors

Run Time Exception

Translation error occurs during page compilation, this results in an Internal Server Error (500).

An exception on other hand occurs when page is compiled and servlet is running.

Anand & Bhautik

Page 62: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Exception handling

The J2EE platform offers a great deal of control

over how to format error information when a web

component throws an unexpected exception.

Exception can be handled in JSP in three ways:

Dealing with exception with page directive

Dealing with exception in Deployment

Descriptor

Java Exception Handling mechanism

Anand & Bhautik

Page 63: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Dealing with exception

with page directive

The error page can be used to display a

more user-friendly error message. Page

Directive can be used to handle exception.

Two attributes of page directive errorPage

and isErrorPage are used to deal with

exception

Anand & Bhautik

Page 64: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Attributes

isErrorPage If “true”, then implicit object exception is available which is instance of class java.lang.Throwable

errorPage Define a URL to another JSP that is invoked if an unchecked runtime exception is thrown

Anand & Bhautik

Page 65: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Following methods are used with exception

object of class Throwable

public String getMessage()

public String getLocalizedMessage()

public StackTrace printStackTrace( )

Anand & Bhautik

Page 66: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

exception

The exception object has page scope, and is

the instance of class java.lang.Throwable.

It refers to the runtime exception that

resulted in the error page being invoked, and

is available only in the error page (a page in

which isErrorPage=true)

Anand & Bhautik

Page 67: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

example

<@page isErrorPage=”true”%>

<html>

<body>

The server has encountered following error

Exception<%=exception%>

Message<%=exception.getMessage()%>

Local Message<%= exception.getLocalizedMessage()%>

</body>

<html>

Anand & Bhautik

Page 68: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Dealing with exception in

Deployment Descriptor

The Web application deployment descriptor (web.xml) uses the <error-page> tag to define Web components that handle errors.

When an error occurs Web Server returns an HTML page that displays

either the HTTP error code or a page containing the Java error message.

You can define your own HTML page to be displayed in place of these default error pages or in response to a Java exception.

Anand & Bhautik

Page 69: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Three elements can be used with error-page:

<exception-type> It is an optional element. A fully

qualified class name of java exception type.

<error-code> It is also an optional element and

contains a valid HTTP error code.

<location> It is required field. It specifies the

location of resource to display in response to the

error

Anand & Bhautik

Page 70: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Example

<error-page>

<exception-type>java.lang.NullPointerException </exception-type>

<location>/error.html</location>

</error-page>

//error.html

<html><body>

<h1>There is some internal problem is a server</h1>

</body>

</html>

Anand & Bhautik

Page 71: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Using JSP error page

JSP pages, being web component, allow user to report not only an error that has occurred, but also what went wrong.

<error-page>

<exception-type>java.lang.SQLException</exception-type>

<location>/error.jsp</location>

</error-page>

Anand & Bhautik

Page 72: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

error.jsp

<@page isErrorPage=”true”%>

<html><body>

The server has encountered following error

<%=exception%>

<%=exception.getMessage%>

<%= exception.getLocalizedMessage%>

<%= exception.printStackTrace()%>

</body>

<html>

Anand & Bhautik

Page 73: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Java Exception Handling

mechanism

The jsp1.1 specification added two java.lang.Exception subclasses JspException

• The JspException class represents a generic exception for the JSP packages. It can be found in the javax.servlet.jsp package.

JspError• The JspError class in the javax.servlet.jsp package is the

only predefined subclass of JspExcepton. It is meant to represent an unrecoverable error.

Anand & Bhautik

Page 74: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Class Hierarchy

Throwable

Error Exception

JspException

JspError

Anand & Bhautik

Page 75: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Example

<%

try

{

//jsp code

}

catch(JspException e)

{

out.println(e);

}

%>

Anand & Bhautik

Page 76: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Scope of JSP variable

Scope is the region in which variable is

accessible

Page scope

Request Scope

Session scope

Application scope

Anand & Bhautik

Page 77: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Page scope

Any object whose scope is the page will disappear as soon as the current page finishes generating.

It is equivalent to local scope but it is implemented differently.

An object with page scope may be modified as often as desired within the page, but all these changes will be lost when page exists.

Anand & Bhautik

Page 78: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Request scope

The second kind of scope is associated with request object.

The first page can place any information into the request scope, and the second page can look for the information and act on it.

One way to put data into the request scope is to do it manually by setting value using setAttribute() method and retrieves the value on forwarded page using getAttribute() method of HttpServletRequest.

Anand & Bhautik

Page 79: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Example

<%

request.setAttribute(“name”,request.getParameter(“t1”))

%>

<jsp:forward page=”/login.jsp”/>

name variable is used as long as request is chained

login.jsp

<html>

<body>

User id:<%=request.getAttribute(“name”)%> </body>

</html>

Anand & Bhautik

Page 80: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Session scope

Since user, not pages are really the focus of the site,

E.g.: Multiple users can see the same checkout page of shopping site, but each user sees his own selection.

In JSP terms data associated wit ha user is in the session scope.

A session does not correspond directly to a user; rather, it corresponds with a particular period of time the user spends at a site.

Anand & Bhautik

Page 81: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

example

<%

session.setAttribute(“name”,request.getParameter(“t1”))

%>

<jsp:forward page=”/login.jsp”/>

name variable is used until user sign out

login.jsp

<html>

<body>

User id:<%=session.getAttribute(“name”)%> </body>

</html>

Anand & Bhautik

Page 82: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Application scope

There is one scope even bigger than

session scope, the application scope,

which extends across all users and all

pages.

It is useful for keeping data that multiple

users will see and appear on multiple

pages.

Anand & Bhautik

Page 83: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

example

<%

application.setAttribute(“name”,request.getParameter(“t1”))

%>

<jsp:forward page=”/login.jsp”/>

name variable is used until user closes site

login.jsp

<html>

<body>

User id:<%=application.getAttribute(“name”)%> </body>

</html>

Anand & Bhautik

Page 84: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Implicit object scope

Objects

request

response

out

session

config

application

exception

Scope

request

Page

Page

Session

page

Application

page

Anand & Bhautik

Page 85: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Expression

language

Introduction

EL

Anand & Bhautik

Page 86: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Why EL?

Debugging and Maintaining site was

difficult.

Mix of HTML and scriplets (no separation

between presentation and logic)

MVC (Model-view-Controller)

EJB JSP BLTo create multiple views on same code

there is a need to separate logic and

view

Anand & Bhautik

Page 87: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

JSP 2.0 incorporates the Expression

Language (EL) first introduced in JSTL

1.0.

EL makes it easier to integrate server-side

state with presentation output.

Anand & Bhautik

Page 88: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

JSP 2.0 EL

The JSP 2.0 Expression Language (EL) is a

much-needed feature for Web developers who

want to build scriptless JSP pages.

It was designed as a part of the JSP Standard

Tag Library (JSTL) 1.0, and then JavaServer

Pages (JSP) 2.0 adopted and enhanced the EL.

Anand & Bhautik

Page 89: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

JSP 2.0 EL

The expression language is an extension of the JSTL 1.0 EL,

that adds several new features.

The syntax of the expression language is simpler than a

custom tag, and usually requires no associated Java code.

Also, the expression language automatically handles

typecasting, null values, and error handling.

Anand & Bhautik

Page 90: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

The EL defines an easy-to-use syntax for

accessing:

JavaBean properties

Scoped attributes

Initialization and request parameters

HTTP headers and cookies without using Java

scriptlets in JSP pages.

This makes the code more readable and

improves the maintainability of the Web pages.

Properties

Anand & Bhautik

Page 91: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

EL has predefined Set of:

EL expression

Variable

Implicit objects

Literals and keywords

Operators

Anand & Bhautik

Page 92: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

EL Expression

Expressions used to set attribute values are evaluated in the context of an expected type.

A JSP 2.0 EL expression is always written between the delimiters ${ and }.

If the result of the expression evaluation does not match the expected type exactly, a type conversion will be performed.

Anand & Bhautik

Page 93: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

EL Variables

Variables are accessed by name.

Type conversion is automatic from the

variable's native type to a type that is

compatible with the requested operation.

Anand & Bhautik

Page 94: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Implicit objects

The JSP expression language defines a set of implicit objects:

pageContext: The context for the JSP page. Provides access to various objects including

servletContext: The context for the JSP page's servlet and any web components contained in the same application.

session: The session object for the client.

request: The request triggering the execution of the JSP page..

response: The response returned by the JSP page.

Anand & Bhautik

Page 95: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

param: Maps a request parameter name to a single value

paramValues: Maps a request parameter name to an array of values

header: Maps a request header name to a single value

headerValues: Maps a request header name to an array of values

cookie: Maps a cookie name to a single cookie

initParam: Maps a context initialization parameter name to a single value

Anand & Bhautik

Page 96: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Scope Object

pageScope: Maps page-scoped variable names to their values

requestScope: Maps request-scoped variable names to their values

sessionScope: Maps session-scoped variable names to their values

applicationScope: Maps application-scoped variable names to their values

Anand & Bhautik

Page 97: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

“.” operator

The "." operator is shorthand for calling a JavaBeans property

accessor for the property whose name is on the right-hand side

of the operator.

For example, the following expression in the sample page:

${pageContext.servletContext.servletContextName

}

actually executes as:

pageContext.getServletContext().getServletContext

Name()

Anand & Bhautik

Page 98: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

[] operator

The [] operator is a polymorphic indexing operator that can

be used to index collections

The value inside the brackets is used as a key into a map, or

as a List or array index.

Eg:

${header["host"]} localhost:8080

${header["user-agent"]} Mozilla/5.0 (Macintosh;...)

Anand & Bhautik

Page 99: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

Literals and Keywords

The JSP expression language defines the following literals:

Boolean: true and false

Integer: as in Java

Floating point: as in Java

String: with single and double quotes; " is escaped as \", ' is

escaped as \', and \ is escaped as \\.

Null: null

Reserved Words

The following words are reserved for the JSP expression

language and should not be used as identifiers.

and eq gt true instanceof or ne le false empty

not lt ge null div mod

Anand & Bhautik

Page 100: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

operators

Arithmetic: +, - (binary), *, / and div, % and mod, - (unary)

Logical: and, &&, or, ||, not, !

Relational: ==, eq, !=, ne, <, lt, >, gt, <=, ge, >=, le.

Empty: The empty operator is a prefix operation that can be used to determine whether a value is null or empty.

Conditional: A ? B : C. Evaluate B or C, depending on the result of the evaluation of A.

Anand & Bhautik

Page 101: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

The precedence of

operators

The precedence of operators highest to lowest, left to right is as follows:

[] .

() - Used to change the precedence of operators.

- (unary) not ! empty

* / div % mod

+ - (binary)

< > <= >= lt gt le ge

== != eq ne

&& and

|| or

? :

Anand & Bhautik

Page 102: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

EL Expression Result

${1 > (4/2)} false

${4.0 >= 3} true

${100.0 == 100} true

${(10*10) ne 100} false

${'a' < 'b'} true

${'hip' gt 'hit‘} false

${4 > 3} true

${1.2E4 + 1.4} 12001.4

${3 div 4} 0.75

${10 mod 4} 2

${empty param.t1} True if the request parameter named t1 is null or an empty string

${param[‘t1']} The value of the request parameter named t1

${header["host"]} The host

Anand & Bhautik

Page 103: Evolution of JSP€¦ · Package The javax.servlet.jsp.JspPage interface contains 2 methods public void jspInit() It is invoked when JSP is first initialized. public void jspDestroy()

EL API

The javax.servlet.jsp.el API consists of two

classes

• Expression ExpressionEvaluator

Two interfaces

• VariableResolver and FunctionMapper

Two exceptions

• ELException and ELParseException

It works with JSP 2.0 and Tomcat 5 and

above

Anand & Bhautik