You are on page 1of 51

JSP BASICS AND

ARCHITECTURE
Goals of JSP
 Simplify Creation of dynamic pages.
 Separate Dynamic and Static content .
What are JSP?
 JSP is a specification and not a product
but a specification.
 JSP More on line of the J2EE
specification.
 Can utilize the Beans to separate the code
out .
 Is compiled to Servlet the first time call is
received for the page.
THE PROCESS

Uses JSP
First <JSP Tags>
Request JSP <Java code>
<JSP Tags>
<Java code>

Client JSP
Text <JSP Tags>
Uses <Java code>

Servlet
Generates
Response
A SIMPLE JSP CODE
%@page import=“java.util.Date”%
<html>
<body>
The current time is <%=new Date().toString()
%>
</body>
</html>
The Container
 According to the JSP specifications that the
servlets implement the Servlets must extend from:
 Specified by JSP author via. Extends
command.
Or
 A container specific Implementation class
that implements javax.servlet.jsp.JSPPage
interface .
The JSP Life Cycle
 public void jspInit() used for initialization
purposes defined in javax.servlet.jsp.JSPpage
Interface.
 public void jspDestroy() used for performing
cleanup operations defined in
javax.servlet.jsp.JSPPage Interface.
 public void _jspService(HttpServletRequest
request, HttpServletResponese response) throws
ServletException,IOException.
The implementation of this method is generated by
the container and should never be provided by
Page authors.
General Rules for JSP Pages
 JSP tags are case sensitive.
 Attribute values in tags always appear
quoted,Single or double.
Eg: <somejsptag attributename=“attribute
value”> body</somejsptag>
Or
<somejsptag attributename=“attribue value” />
The Second one is valid in case when there is no
body.
General Rules for JSP Pages
 The character \ can be used as an escape
character eg: to use %.
 URL that does not start with / are interpreted
relative to the current JSP.
 URL starting with a / , called a context relative
path, is interpreted with reference to the web
application to which the JSP page belongs.
 Any white spaces with in the body text of a
document are not significant although they are
preserved during translation into a servlet.
Types of JSP Tags
 Directives.
 Scripting Elements.
 Actions.
JSP Directives
 They serve as messages to the JSP container
from the JSP.
 Used to set global values as
 Class Declarations

 Methods to be implemented.

 Output Content Type.

 They don’t produce any output to the user.


 Affects the whole file but only that file.
Types of JSP Directives
 page Directive.
 include Directive.
 taglib Directive.
page Directive
 Used to define and manipulate a number of page
dependent attributes that affect the whole JSP.
 A page can contain any number of page
directives , in any order, anywhere in the
JSP.They are assimilated during translation.
 There can be only one occurrence of
attribute/value pair defined by the page directives.
Program utilizing Page
Directives
<%@ page language="Java" session="true"

import="java.rmi.*,java.util.*" session="true"
buffer="12kb"

autoFlush="true" info="my page directive jsp"


errorPage="error.jsp"

isErrorPage="false" isThreadSafe="false" %>


Program utilizing Page
Directives
<html>
<head>
<title>Page Directive test Page</title>
</head>
<h1>Page directive test page</h1>

This is a JSP to test the page directive.


</body>
</html>
Error in the Code
This code will throw an error at present
because the language page directive has
been kept there for future use if JSP
containers support multiple languages. At
present this is not supported therefore it
will through could not locate style sheet for
JAVA error.
include Directive
 It instructs the container to include the content of
the resource in the current JSP by inserting it
inline.
 The include action occurs at runtime.
 Eg: <%@ include file=“Filename” %>
 The only available attribute file specifies the file
name to be included.
 The included file can be a static resource such as
an HTML file or another JSP.
 The code is inserted inline.
A simple Include Directive code
<html>
<head>
<title>Include Directive test page 1</title>
</head>
<body>
<h1>Include directive test page 1</h1>

<%@ include file="/copyright.html" %>


</body>
</html>
The copyright HTML file
<p>&copy; 2000 Wrox Press</p>
Output
Include directive test page 1
© 2000 Wrox Press
taglib Directive
 This directive allows the page to use tag
extensions (custom tags).
 It names the tag library that contains the
compiled Java code.
 Eg: <%@ taglib uri=“tagLibraryURI”
prefix=“tagprefix” %>
Scripting Elements
 These allow Java Code – variable or
method declarations , scriptlets (arbitrary
Java code) and expressions to be inserted
into your JSP page.
Types of Scripting Elements
They are of three types:
 Declarations
 Scriptlets
 Expressions
Declarations
 A declaration is a block of Java code that
is used to define class wide variables and
methods in the generated servlet.
 Declarations are initialized when the JSP
page is initialized .
 Syntax : <%! Java Variable and method
declaration(s) %>
Scriptlets
 A scriptlet is a block of code that is executed
during the request processing time.
 All the scriptlets in the JSP are combined in the
order they appear in the code.
 As expected all the code for the scriptlets is put
into the service() method of the servlet.
 Syntax: <% Valid Java code Statements %>
Expressions
 The expression is a shorthand notation for a
scriptlet that sends the value of Java expression
back to the client.
 The expression is evaluated at HTTP request
processing time and the result is converted to
sting and displayed.
 In case the result of the expression is an object
the conversation is done by using the objects
toString() method.
 Syntax: <%= Java expression to be evaluated %>
Standard Actions
 Standard Actions are tags that affect the runtime
behavior of the JSP and the response sent back to
the user.
 They have to provided by the container
irrespective of the usage.
 During compilation into the servlet , the
container comes across the this tag and replaces it
with Java code that corresponds to the required
predefined task.
Types of the Standard Action
 <jsp:usebean>
 <jsp:setProperty>
 <jsp:getProperty>
 <jsp:param>
 <jsp:include>
 <jsp:forward>
 <jsp:plugin>
<jsp:useBean>
 This tag is used to instantiate a Java bean ,
or locate bean instance and assign it to a
variable name (or id).
 We can also specify the lifetime of an
object by giving it a specific name.
 <jsp:useBean id=“name”
scope=“scopeName” beanDetails/>
<jsp:setProperty>
 It is used with the <jsp:useBean> action to set the
value of bean properties.
 The properties in a bean can be set either:
 At request time from parameters in the

request object.
 At request time from an evaluated

expression.
 From a specified string.

 Syntax:
<jsp:setProperty name=“help” property=“word”
/>
<jsp:getProperty>
 It is used to access the properties of a
bean.
 It accesses a property , converts it into a
String, and prints it into the output stream
of the client.
 Syntax:
<jsp:getProperty name=“name”
property=“propertyName” />
Example utilizing <jsp:useBean>
<jsp:setProperty> <jsp:getProperty>
 For this example we will first of all create an
HTML file beans.html in which user can insert
his name and choose his favorite language from
the drop down menu . Another file beans.jsp will
be used to set the bean properties as per the values
entered by the user and then by utilizing the
methods defined in the LanguageBean.class it
retrieves the information and displays it to the
user.
beans.html
<html>
<head>
<title>useBean action test page</title>
</head>
<body>
<h1>useBean action test page</h1>
<form method="post" action="beans.jsp">
<p>Please enter your user name :
<input type="text" name="name">
<br>What is your favorite programming language?
beans.html continued..
<select name="language">
<option value="Java">Java
<option value="c++">c++
<option value="Perl">Perl
</select>
</p>
<p><input type="submit" value="submit information">
</form>

</body>
</html>
beans.jsp
<jsp:useBean id="languageBean" scope="page"
class="LanguageBean">
<jsp:setProperty name="languageBean"
property="*" />
</jsp:useBean>

<html>
<head>
<title>useBean action test result</title>
</head>
beans.jsp continued…
<body>
<h1>useBean action test result</h1>
<p> Hello, <jsp:getProperty name="languageBean"
property="name"/>.</p>

<p>Your favorite language is


<jsp:getProperty name="languageBean"
property="language"/>.</p>
beans.jsp continued…
<p>My comments on your favorite
language:</p>
<p><jsp:getProperty name="languageBean"
property="languageComments" />
</p>
</body>
</html>
LanguageBean.class
public class LanguageBean{

private String name;


private String language;

public LanguageBean() {}

public void setName(String name){


this.name=name;
}
LanguageBean.class continued..

public String getName(){


return name;
}

public void setLanguage(String language){


this.language=language;
}
LanguageBean.class continued..
public String getLanguage(){
return language;
}

public String getLanguageComments(){


if(language.equals("Java")){
return "The king of OOP languages.";
}
LanguageBean.class continued..
else if(language.equals("c++")){
return "Rather too complex for some folks' liking.";
}
else if (language.equals("perl")){
return "OK if you like incomprehensible code.";
}else{
return "Sorry, i have never heard of" +language+".";
}
}
}
Output of the Code
useBean action test result
Hello, Navdeep Mahajan.
Your favorite language is Java.
My comments on your favorite language:
The king of OOP languages.
<jsp:param>
 It is used to provide other tags with
additional information in form of name
value pairs.
 Syntax:
<jsp:param name=“paramname”
value=“paramvalue” />
<jsp:include>
 This action allows the static or dynamic resource
, specified by the URL to be included in the
current JSP at request processing time.
 The included page has access to only JspWriter
object.
 It cannot set headers and cookies.
 It include page cannot have jsp tags.
 If the page output is buffered then the buffer is
flushed prior to the inclusion.
 It has a small penalty on the efficiency.
<jsp:include>
 Syntax:
<jsp:include page=“URL” flush=“true”>
<jsp:param name=“paramname”
value=“paramvalue” />
……
</jsp:include>
<jsp:forward>
 It allows the request to be forwaded to the
another JSP, to a servlet , or to a static
resource .
 Execution in the current JSP stops when it
encounters the <jsp:forward> tag,the buffer
is cleared , and the request is modified..
<jsp:forward>
 Syntax:
<jsp:forward page=“URL”>
<jsp:param name=“paramname”
value=“paramvalue” />
…..
</jsp:forward>
<jsp:plugin>
 It is used in pages to generate client
browser specific HTML tags like
<OBJECT> or <EMBED> that result in
the download of the Java Plug-in Software
, if required,followed by the execution of
the applet or JavaBeans component that is
specified in the tag.
<jsp:plugin> supported tags
 It supports two additional support tags:
 <jsp:params>,to pass additional parameters
to the applet or the java beans component.
 <jsp:fallback>, to specify the content to be
displayed in the client browser if the plugin
cannot be started because the generated
tags are not supported.
Syntax
<jsp:plugin type=“bean|applet”
code=“objectcode”
codebase=“objectcodebase”
align=“alignment” archive=“archivelist”
height=“height” hspace=“hspace”
jreversion=“jreversion”
name=“componentname” vspace=“vspace”
width=“width” nspluginurl=“url”
iepluginurl=“url”>
Syntax continued..
<jsp:params>
<jsp:param name=“name” value=
“paramvalue” />
………
</jsp:params>
<jsp:fallback>Alternate text to display
</jsp:fallback>
</jsp:plugin>

You might also like