1. UNIT - 3UNIT - 3
• JSP Basic Syntax,
•HTMLText, HTML comments,
•TemplateText,
•JSP Comment, JSPExpression,
•JSP Scriptlet,
•JSP Declaration, JSP Directives,
•JSP Action, JSP Expression Language Element,
•CustomTag (Custom Action),
•EscapedTemplateText, Using JSP Scripting
2. JSP (JavaServer Page)JSP (JavaServer Page)
JSP is a server side technology that does all the
processing at server.
It is used for creating dynamic web applications,
using java as programming language.
JSP’s allow us to separate the dynamic content of webpage
from static presentation content.
A JSP page consists of HTML tags and JSP tags.
HTML tags are used to create static page content and JSP
tags are used to add dynamic content to web pages.
JSP pages are compiled into a Java Servlet by a JSP
translator.
A JSP page consists of HTML tags and JSP tags. The jsp
pages are easier to maintain than servlet because we can
separate designing and development.
It provides some additional features such as Expression
Language, Custom Tag etc.
3. Basically, any html file can be converted to
JSP file by just changing the file extension
from “.html” to “.jsp”, it would run just fine.
JSP tag is … <% … %>
What differentiates JSP from HTML is the
ability to use java code inside HTML.
In JSP, you can embed Java code in HTML
using JSP tags. for e.g. run the code below,
every time you run this, it would display the
current time.
That is what makes this code dynamic.
4. Save it with .jsp & put inside a folderSave it with .jsp & put inside a folder
and put that folder in webapp dirand put that folder in webapp dir
<HTML>
<BODY>
Hello BeginnersBook
Readers!
Current time is: <%=
new java.util.Date()
%>
</BODY>
</HTML>
<html>
<body>
<% out.print(2*5); %>
</body>
</html>
6. Jsp vs htmlJsp vs html
HTML JSP
1
Html is given by w3c (World Wide
Web Consortium).
JSP is given by SunMicro System.
2 Html generated static web pages. JSP generated dynamic web pages.
3
It do not allow to place java code
inside Html pages.
JSP allows to place java code inside JSP
pages.
4 It is Client side technology It is a Server side technology.
5
Need Html Interpreter to execute
these code.
Need JSP container to execute jsp
code.
6
It does not allow to place custom
tag or third party tag.
It allow to place custom tag or third
party tag.
7. JSP SERVLET
1
JSP is a webpage scripting language that
can generate dynamic content.
Servlets are Java programs that are already
compiled which also creates dynamic web
content.
2
JSP run slower compared to Servlet as it
takes compilation time to convert into
Java Servlets.
Servlets run faster compared to JSP.
3
It’s easier to code in JSP than in Java
Servlets.
Its little much code to write here.
4 In MVC, jsp act as a view. In MVC, servlet act as a controller.
5
JSP are generally preferred when there is
not much processing of data required.
servlets are best for use when there is more
processing and manipulation involved.
6
The advantage of JSP programming over
servlets is that we can build custom tags
which can directly call Java beans.
There is no such custom tag facility in
servlets.
7
We can achieve functionality of JSP at
client side by running JavaScript at client
side.
There are no such methods for servlets.
8 JSP is java in html. Servlet is html in java
8. Life Cycle of JSPLife Cycle of JSP
JSP pages follow these phases:
Translation of JSP Page
Compilation of JSP Page
Classloading (class file is loaded by the
classloader)
Instantiation (Object of the Generated
Servlet is created).
Initialization ( jspInit() method is invoked
by the container).
Reqeust processing ( _jspService() method
is invoked by the container).
Destroy ( jspDestroy() method is invoked
by the container).
9.
10. As depicted in the above diagram, JSP page is
translated into servlet by the help of JSP
translator.
The JSP translator is a part of webserver that
is responsible to translate the JSP page into
servlet.
Afterthat Servlet page is compiled by the
compiler and gets converted into the class
file.
Moreover, all the processes that happens in
servlet is performed on JSP later like
initialization, committing response to the
browser and destroy.
11. JSP Page Translation:In the first step, the web
container translates the JSP file into a Java source
file that contains a servlet class definition. The web
container validates the correctness of JSP pages and
tag files.
JSP Page Compilation:In the second step, the
web container compiles the servlet source code
into a Java class file.
JSP Page Class Loading:In the third step, the
servlet class byte code is loaded into the web
container’s JVM software using class loader.
JSP Page Servlet Instance:In the fourth step,
the web container creates an instance of the
servlet class.
12.
13. JSP Compilation:JSP Compilation:
When a browser asks for a JSP, the JSP
engine first checks to see whether it
needs to compile the page
The compilation process involves three
steps:
◦ Parsing the JSP.
◦ Turning the JSP into a servlet.
◦ Compiling the servlet.
16. JSP Cleanup:JSP Cleanup:
The destruction phase of the JSP life cycle
represents when a JSP is being removed from
use by a container.
The jspDestroy() method is the JSP equivalent
of the destroy method for servlets
17. JSP Scriptlet tagJSP Scriptlet tag
(Scripting elements)(Scripting elements)
In JSP, java code can be written inside the
jsp page using the scriptlet tag
18. JSP Scripting elementsJSP Scripting elements
The scripting elements provides the
ability to insert java code inside the jsp.
There are three types of scripting
elements:
1. scriptlet tag <% … %>
2. expression tag <%= … %>
3. declaration tag <%! … %>
19. JSP scriptlet tagJSP scriptlet tag
A scriptlet tag is used to execute java
source code in JSP.
Syntax is as follows:
◦ <% java source code %>
20.
21. Example of JSP scriptlet tagExample of JSP scriptlet tag
22.
23. JSP expression tagJSP expression tag
The code placed within JSP expression
tag is written to the output stream of the
response
So you need not write out.print() to
write data.
It is mainly used to print the values of
variable or method.
24.
25.
26.
27. JSP Declaration TagJSP Declaration Tag
The JSP declaration tag is used to
declare fields and methods.
The code written inside the jsp
declaration tag is placed outside the
service() method of auto generated
servlet
28.
29. JSP Scriptlet tag vs DeclarationJSP Scriptlet tag vs Declaration
tagtag
30.
31.
32.
33.
34. JSP Action TagsJSP Action Tags
There are many JSP action tags or
elements. Each JSP action tag is used to
perform some specific tasks.
The action tags are used to control the
flow between pages and to use Java Bean
35.
36. Using Standard Actions TagsUsing Standard Actions Tags
1. <jsp:plugin>
2. <jsp:forward>
3. <jsp:include>
37. <jsp:include> action Tag<jsp:include> action Tag
The jsp:include action tag is used to
include the content of another resource
it may be jsp, html or servlet.
The jsp include action tag includes the
resource at request time so it is better
for dynamic pages because there might
be changes in future.
The jsp:include tag can be used to include
static as well as dynamic pages
38. Advantage
◦ Code reusability :
◦ We can use a page many times such as
including header and footer pages in all pages.
So it saves a lot of time
47. <jsp:plugin> Action tag<jsp:plugin> Action tag
The plugin action is used to insert Java
components into a JSP page.
It determines the type of browser and inserts
the <object> or <embed> tags as needed
The <jsp:plugin> action tag is used to embed
applet in the jsp file.
The <jsp:plugin> action tag downloads plugin at
client side to execute an applet or bean
48. When a jsp wants to display response in
a graphics format in a browser, then a jsp
page will integrates with an applet
49.
50.
51. <jsp:param><jsp:param>
<jsp:param> tag is used to represent
parameter value during jsp forward or include
action this should be the sub tag of
<jsp:forward> or <jsp:include>
When an include or forward element is
invoked, the original request object is provided
to the target page.
If you wish to provide additional data to that
page, you can append parameters to the
request object by using the jsp:param element
52.
53.
54.
55. JavaBeansJavaBeans
In computing based on the JavaPlatform,
◦ JavaBeans are classes that encapsulate
many objects into a single object (the bean).
◦ They are serializable, have a zero-argument
constructor, and allow access to properties
using getter and setter methods
The name "Bean" was given to encompass this
standard, which aims to
create reusable software components for Java
56. JavaBeans is
◦ an object-oriented programming interface from Sun
Microsystems
◦ that lets you build re-useable applications or program
building blocks called components
◦ that can be deployed in a network on any major
operating system platform
57.
58.
59. jsp:useBean action tagjsp:useBean action tag
The jsp:useBean action tag is used to
locate or instantiate a bean class.
If bean object of the Bean class is already
created, it doesn't create the bean
depending on the scope.
But if object of bean is not created, it
instantiates the bean
60.
61.
62. jsp:setPropertyjsp:setProperty
and jsp:getProperty action tagsand jsp:getProperty action tags
The jsp:setProperty action tag sets a
property value or values in a bean using
the setter method
The jsp:getProperty action tag returns
the value of the property.
64. Jsp expression language elementJsp expression language element
JSP Expression Language (EL) makes it
possible to easily access application data
stored in JavaBeans components.
JSP EL allows you to create expressions
both (a) arithmetic and (b) logical.
Within a JSP EL expression, you can use
integers, floating point numbers,
strings, the built-in constants true and
false for boolean values, and null.
65. Syntax for Expression LanguageSyntax for Expression Language
(EL)(EL)
${ expression }
Here, expr specifies the expression
itself.
The most common operators in JSP EL
are . and [].
These two operators allow you to access
various attributes of Java Beans and built-
in JSP objects.
66. Custom Tag (Custom Action)
Custom tags are user-defined tags. They reduces the
possibility of scriptlet tag and separates the business logic
from the JSP page.
The same business logic can be used many times by the use of
custom tag.
Advantages of Custom Tags
Eliminates the need of scriptlet tag The custom tags
eliminates the need of scriptlet tag which is considered bad
programming approach in JSP.
Separation of business logic from JSP The custom tags
separate the the business logic from the JSP page so that it
may be easy to maintain.
Re-usability The custom tags makes the possibility to reuse
the same business logic again and again.
67. To create a custom tag we need three
things:
1) Tag handler class: In this class we
specify what our custom tag will do when
it is used in a JSP page.
2) TLD file: Tag descriptor file where
we will specify our tag name, tag handler
class and tag attributes.
3) JSP page: A JSP page where we will
be using our custom tag.
68.
69. Tag handler class:
A tag handler class should implement
Tag/IterationTag/ BodyTag interface or it
can also extend
TagSupport/BodyTagSupport/SimpleTagSu
pport class.
All the classes that support custom tags are
present inside javax.servlet.jsp.tagext.
In the below we are extending the class
SimpleTagSupport.
70.
71. TLD File
This file should present at the location: Project
Name/WebContent/WEB-INF/ and it
should have a .tld extension.
Note:
<name> tag: custom tag name.
In this example we have given it as MyMsg
<tag-class> tag: Fully qualified class name. Our
tag handler class Details.java is in package
beginnersbook.com
so we have given the value as
beginnersbook.com.Details.
72.
73. Using custom tag in JSP
Above we have created a custom tag named MyMsg. Here
we will be using it.
Note: taglib directive should have the TLD file path in uri
field.
Above we have created the message.tld file so we have
given the path of that file.
Choose any prefix and specify it in taglib directive’s prefix
field.
Here we have specified it as myprefix.
Custom tag is called like this: <prefix:tagName/>.
Our prefix is myprefix and tag name is MyMsg so we have
called it as <myprefix:MyMsg/> in the below JSP page.
74.
75. Predefined Variables in JSPPredefined Variables in JSP
These variables can be used with servlets and Java beans. Following are
the list of predefined variables.
request:
This variable specifies the data included in a http request. This variable
takes value from the clients' browser to pass it over to the
server.reponse:
This variable specifies the data included in the http response. It is used
with cookies and also in http headers.
out:
This variable specifies the output stream otherwise known as
printwriter in a page context.
session:
This variable specifies the data associated with httpsession object with
a specific session of a user. The main purpose of this object is to use
the session information to maintain multiple page requests.
76. application:
This variable is used to share data with all
application pages.
Config:
This variable refers the java servlet configuration
value.
pagecontext:
This variable is used to store the environment for
the page like page attributes, access to the
request, response and session objects.
page:
This variable is used to store the instance of the
generated java servlet.
77. Scope of JSP ObjectsScope of JSP Objects
The availability of a JSP object for use from a particular place of the
application is defined as the scope of that JSP object.
Every object created in a JSP page will have a scope. Object scope in
JSP is segregated into four parts and they are page, request, session and
application.
page
‘page’ scope means, the JSP object can be accessed only from within
the same page where it was created.
The default scope for JSP objects created using <jsp:useBean> tag is
page.
JSP implicit objects out, exception, response, pageContext, config and
page have ‘page’ scope.
request
A JSP object created using the ‘request’ scope can be accessed from
any pages that serves that request.
More than one page can serve a single request. The JSP object will be
bound to the request object.
Implicit object request has the ‘request’ scope.
78. session
‘session’ scope means, the JSP object is accessible from pages that
belong to the same session from where it was created.
The JSP object that is created using the session scope is bound to
the session object.
Implicit object session has the ‘session’ scope.
application
A JSP object created using the ‘application’ scope can be accessed
from any pages across the application.
The JSP object is bound to the application object.
Implicit object application has the ‘application’ scope.