JSP Final
JSP Final
1
JSP
JSP technology is used to create web application just
like Servlet technology. It can be thought of as an
extension to servlet because it provides more
functionality than servlet such as expression
language, jstl etc.
2) Easy to maintain
JSP can be easily managed because we can easily separate our business logic with
presentation logic. In servlet technology, we mix our business logic with the presentation
logic.
3
Life cycle of a JSP Page
The JSP pages follows these phases:
5
Creating a simple JSP Page
To create the first jsp page, write some html code as given below, and save it
by .jsp extension. We have save this file as index.jsp. Put it in a folder and paste the
folder in the web-apps directory in apache tomcat to run the jsp page.
index.jspLet's see the simple example of JSP, here we are using the scriptlet tag to
put java code in the JSP page.
6
Do I need to follow directory
structure to run a simple JSP ?
No, there is no need of directory structure if you
don't have class files or tld files. For example, put
jsp files in a folder directly and deploy that
folder.It will be running fine.But if you are using
bean class, Servlet or tld file then directory
structure is required.
7
Directory structure of JSP
The directory structure of JSP page is same as servlet. We contains the jsp
page outside the WEB-INF folder or in any directory.
8
The JSP API
9
javax.servlet.jsp package
The javax.servlet.jsp package has two interfaces and classes.The two
interfaces are as follows:
JspPage
HttpJspPage
10
11
12
JSP Scriptlet tag (Scripting
elements)
In JSP, java code can be written inside the jsp page using the
scriptlet tag. Let's see what are the scripting elements first.
13
JSP scriptlet tag
14
Example of JSP scriptlet tag that
prints the user name
In this example, we have created two files index.html and welcome.jsp. The
index.html file gets the username from the user and the welcome.jsp file prints the
username with the welcome message.
15
16
JSP 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.
17
Example of JSP expression tag
that prints current time
To display the current time, we have used the getTime() method of Calendar class.
The getTime() is an instance method of Calendar class, so we have called it after
getting the instance of Calendar class by the getInstance() method.
18
Example of JSP expression tag that
prints the user name
In this example, we are printing the username using the expression tag. The
index.html file gets the username and sends the request to the welcome.jsp file,
which displays the username.
19
JSP Declaration Tag
20
Difference between JSP Scriptlet tag
and Declaration tag
21
Example of JSP declaration tag
that declares field
22
Example of JSP declaration tag that
declares method
23
JSP directives
The jsp directives are messages that tells the web container
how to translate a JSP page into the corresponding servlet.
There are three types of directives:
page directive
include directive
taglib directive
46
JSP page directive
The page directive defines attributes that apply to an entire JSP page.
Syntax of JSP page directive
47
1)import
48
2)contentType
The contentType attribute defines the MIME(Multipurpose Internet Mail Extension)
type of the HTTP response.The default value is "text/html;charset=ISO-8859-1“
<%@ page contentType=application/msword %>
3)extends
The extends attribute defines the parent class that will be inherited by the generated
servlet.It is rarely used.
4)info
This attribute simply sets the information of the JSP page which is retrieved later by
using getServletInfo() method of Servlet interface.
<%@ page info="composed by Sonoo Jaiswal" %>
5)buffer
The buffer attribute sets the buffer size in kilobytes to handle output generated by
the JSP page.The default size of the buffer is 8Kb.
<%@ page buffer="16kb" %>
6)language
The language attribute specifies the scripting language used in the JSP page. The
default value is "java".
49
7)isELIgnored
We can ignore the Expression Language (EL) in jsp by the isELIgnored attribute. By
default its value is false i.e. Expression Language is enabled by default. We see
Expression Language later.
<%@ page isELIgnored="true" %>//Now EL will be ignored
8)isThreadSafe
Servlet and JSP both are multithreaded.If you want to control this behaviour of JSP
page, you can use isThreadSafe attribute of page directive.The value of isThreadSafe
value is true.If you make it false, the web container will serialize the multiple
requests, i.e. it will wait until the JSP finishes responding to a request before passing
another request to it.If you make the value of isThreadSafe attribute like:
<%@ page isThreadSafe="false" %>
9)errorPage
The errorPage attribute is used to define the error page, if exception occurs in the
current page, it will be redirected to the error page.
<%@ page errorPage="myerrorpage.jsp" %>
10)isErrorPage
The isErrorPage attribute is used to declare that the current page is the error page.
<%@ page isErrorPage="true" %>
50
Jsp Include Directive
51
Example of include directive
52
JSP Taglib directive
The JSP taglib directive is used to define a tag library
that defines many tags. We use the TLD (Tag Library
Descriptor) file to define the tags. In the custom tag
section we will use this tag so it will be better to learn
it in custom tag.
Syntax JSP Taglib directive
<%@ taglib uri="uriofthetaglibrary" prefix="prefixoftaglibrary" %>
53
Example of JSP Taglib
directive
suppose the custlib tag library contains a tag
called hello. If you wanted to use the hello tag with a
prefix of mytag, your tag would be <mytag:hello> and it
will be used in your JSP file as follows −
54
Exception Handling in JSP
55
Example of exception handling in jsp by the
elements of page directive
In this case, you must define and create a page to handle the
exceptions, as in the error.jsp page. The pages where may
occur exception, define the errorPage attribute of page
directive, as in the process.jsp page.
There are 3 files:
index.jsp for input values
process.jsp for dividing the two numbers and displaying the
result
error.jsp for handling the exception
56
Example
57
58
Example of exception handling in jsp
by specifying the error-page element
inThis approach is better because
web.xml file
you don't need to specify the errorPage
attribute in each jsp page. Specifying the single entry in the web.xml file
will handle the exception. In this case, either specify exception-type or
error-code with the location element. If you want to handle all the
exception, you will have to specify the java.lang.Exception in the
exception-type element. Let's see the simple example:
59
1) web.xml file if you want to
handle any exception
<web-app>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>
</web-app>
60
1) web.xml file if you want to handle the exception for a
specific error code
<web-app>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
</web-app>
61
3) process.jsp
Now, you don't need to specify the errorPage attribute of page directive in the jsp
page.
<%@ page errorPage="error.jsp" %>
<%
String num1=request.getParameter("n1");
String num2=request.getParameter("n2");
int a=Integer.parseInt(num1);
int b=Integer.parseInt(num2);
int c=a/b;
out.print("division of numbers is: "+c);
%>
4) error.jsp file is same as previous
62
JSP 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. The Jsp action tags are given below.
63
jsp:forward action tag
The jsp:forward action tag is used to forward the request to another
resource it may be jsp, html or another resource.
64
Example of jsp:forward action tag
without parameter
65
Example of jsp:forward action tag
with parameter
66
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.
67
Custom Tags in JSP
Custom tags are user-defined tags. They eliminates 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
The key advantages of Custom tags are as follows:
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.
76
Syntax to use custom tag
There are two ways to use the custom tag. They are given
below:
77
MVC in JSP
MVC stands for Model View and Controller. It is a design
pattern that separates the business logic, presentation logic
and data.
Controller acts as an interface between View and Model.
Controller intercepts all the incoming requests.
Model represents the state of the application i.e. data. It can
also have business logic.
View represents the presentaion i.e. UI(User Interface).
80
81
82
83
84
85
86
87
88
89
90
91
Database connectivity with JSP
92
93