Department of Computer Science and Engineering: Certification Course
Department of Computer Science and Engineering: Certification Course
CERTIFICATION COURSE
ON
COURSE MATERIAL
JAVA SERVER PAGES
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. 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 Tags, etc.
There are many advantages of JSP over the Servlet. They are as follows:
1) Extension to Servlet
JSP technology is the extension to Servlet technology. We can use all the features of the Servlet
in JSP. In addition to, we can use implicit objects, predefined tags, expression language and
Custom tags in JSP, that makes JSP development easy.
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.
If JSP page is modified, we don't need to recompile and redeploy the project. The Servlet code
needs to be updated and recompiled if we have to change the look and feel of the application.
In JSP, we can use many tags such as action tags, JSTL, custom tags, etc. that reduces the code.
Moreover, we can use EL, implicit objects, etc.
jspInit(), _jspService() and jspDestroy() are the life cycle methods of JSP.
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 the web server which is responsible for translating the JSP page into
Servlet. After that, Servlet page is compiled by the compiler and gets converted into the class file.
Moreover, all the processes that happen in Servlet are performed on JSP later like initialization,
committing response to the browser and destroy.
To create the first JSP page, write some HTML code as given below, and save it by .jsp extension.
We have saved 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.jsp
Let's see the simple example of JSP where we are using the scriptlet tag to put Java code in the
JSP page. We will learn scriptlet tag later.
<html>
<body>
<% out.print(2*5); %>
</body>
</html>
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. However, if you are
using Bean class, Servlet or TLD file, the directory structure is required.
The directory structure of JSP page is same as Servlet. We contain the JSP page outside the WEB-
INF folder or in any directory.
JSP Index
JSP Tutorial
9 Implicit Objects
JSP Out
o JSP Request
o JSP Response
o JSP Config
o JSP Application
o JSP Session
o JSP PageContext
o JSP Page
o JSP Exception
Action Elements
o jsp:forward
o jsp:include
o Java Bean class
o jsp:useBean
o set & getProperty
o Displaying applet in JSP
Expression Language
MVC in JSP
JSTL
JSP Pagination
JSP CRUD
Development in JSP
o Registration Form
o Login Form
o Uploading File
o Downloading File
1. javax.servlet.jsp
2. javax.servlet.jsp.tagext
javax.servlet.jsp package
The javax.servlet.jsp package has two interfaces and classes.The two interfaces are as follows:
1. JspPage
2. HttpJspPage
o JspWriter
o PageContext
o JspFactory
o JspEngineInfo
o JspException
o JspError
The JspPage interface
According to the JSP specification, all the generated servlet classes must implement the JspPage
interface. It extends the Servlet interface. It provides two life cycle methods.
1. public void jspInit(): It is invoked only once during the life cycle of the JSP when JSP
page is requested firstly. It is used to perform initialization. It is same as the init() method
of Servlet interface.
2. public void jspDestroy(): It is invoked only once during the life cycle of the JSP before
the JSP page is destroyed. It can be used to perform some clean up operation.
The HttpJspPage interface provides the one life cycle method of JSP. It extends the JspPage
interface.
For creating a dynamic web project click on File Menu -> New -> dynamic web project -> write
your project name e.g. first -> Finish.
2) Create the JSP file in eclipse IDE
For creating a jsp file explore the project by clicking the + icon -> right click on WebContent ->
New -> jsp -> write your jsp file name e.g. index -> next -> Finish.
Now JSP file is created, let's write some code.
3) Start the server and deploy the project:
For starting the server and deploying the project in one step Right click on your project -> Run As
-> Run on Server -> choose tomcat server -> next -> addAll -> finish.
If you are using Eclipse IDE first time, you need to configure the tomcat server First. Click for How
to configure tomcat server in eclipse IDE
For starting the server and deploying the project in one step Right click on your project -> Run As
-> Run on Server -> choose tomcat server -> next -> addAll -> finish.
Yes, Let's see JSP is successfully running now.
JSP Scriptlet tag (Scripting elements)
1. Scripting elements
2. JSP scriptlet tag
3. Simple Example of JSP scriptlet tag
4. Example of JSP scriptlet tag that prints the user name
In JSP, java code can be written inside the jsp page using the scriptlet tag. Let's see what are the
scripting elements first.
The scripting elements provides the ability to insert java code inside the jsp. There are three types
of scripting elements:
o scriptlet tag
o expression tag
o declaration tag
A scriptlet tag is used to execute java source code in JSP. Syntax is as follows:
<html>
<body>
<% out.print("welcome to jsp"); %>
</body>
</html>
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.
File: index.html
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
File: welcome.jsp
<html>
<body>
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
</form>
</body>
</html>
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.
In this example of jsp expression tag, we are simply displaying a welcome message.
<html>
<body>
<%= "welcome to jsp" %>
</body>
</html>
Note: Do not end your statement with semicolon in case of expression tag.
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.
index.jsp
<html>
<body>
Current Time: <%= java.util.Calendar.getInstance().getTime() %>
</body>
</html>
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.
File: index.jsp
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname"><br/>
<input type="submit" value="go">
</form>
</body>
</html>
File: welcome.jsp
<html>
<body>
<%= "Welcome "+request.getParameter("uname") %>
</body>
</html>
The code written inside the jsp declaration tag is placed outside the service() method of auto
generated servlet.
The jsp scriptlet tag can only declare The jsp declaration tag can declare variables as
variables not methods. well as methods.
The declaration of scriptlet tag is placed The declaration of jsp declaration tag is placed
inside the _jspService() method. outside the _jspService() method.
In this example of JSP declaration tag, we are declaring the field and printing the value of the
declared field using the jsp expression tag.
index.jsp
<html>
<body>
<%! int data=50; %>
<%= "Value of the variable is:"+data %>
</body>
</html>
In this example of JSP declaration tag, we are defining the method which returns the cube of given
number and calling this method from the jsp expression tag. But we can also use jsp scriptlet tag
to call the declared method.
index.jsp
<html>
<body>
<%!
int cube(int n){
return n*n*n*;
}
%>
<%= "Cube of 3 is:"+cube(3) %>
</body>
</html>
There are 9 jsp implicit objects. These objects are created by the web container that are available
to all the jsp pages.
The available implicit objects are out, request, config, session, application etc.
Object Type
out JspWriter
request HttpServletRequest
response HttpServletResponse
config ServletConfig
application ServletContext
session HttpSession
pageContext PageContext
page Object
exception Throwable
For writing any data to the buffer, JSP provides an implicit object named out. It is the object of
JspWriter. In case of servlet you need to write:
PrintWriter out=response.getWriter();
index.jsp
<html>
<body>
<% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %>
</body>
</html>
Output
The JSP request is an implicit object of type HttpServletRequest i.e. created for each jsp request
by the web container. It can be used to get request information such as parameter, header
information, remote address, server name, server port, content type, character encoding etc.
It can also be used to set, get and remove attributes from the jsp request scope.
Let's see the simple example of request implicit object where we are printing the name of the user
with welcome message.
index.html
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
welcome.jsp
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
Output
3) JSP response implicit object
It can be used to add or manipulate response such as redirect response to another resource, send
error etc.
Let's see the example of response implicit object where we are redirecting the response to the
Google.
Example of response implicit object
index.html
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
welcome.jsp
<%
response.sendRedirect("http://www.google.com");
%>
Output
In JSP, config is an implicit object of type ServletConfig. This object can be used to get
initialization parameter for a particular JSP page. The config object is created by the web container
for each jsp page.
Generally, it is used to get initialization parameter from the web.xml file.
index.html
<form action="welcome">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
web.xml file
<web-app>
<servlet>
<servlet-name>sonoojaiswal</servlet-name>
<jsp-file>/welcome.jsp</jsp-file>
<init-param>
<param-name>dname</param-name>
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>sonoojaiswal</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
welcome.jsp
<%
out.print("Welcome "+request.getParameter("uname"));
String driver=config.getInitParameter("dname");
out.print("driver name is="+driver);
%>
Output
5) JSP application implicit object
The instance of ServletContext is created only once by the web container when application or
project is deployed on the server.
This object can be used to get initialization parameter from configuaration file (web.xml). It can
also be used to get, set or remove attribute from the application scope.
index.html
<form action="welcome">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
web.xml file
<web-app>
<servlet>
<servlet-name>sonoojaiswal</servlet-name>
<jsp-file>/welcome.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>sonoojaiswal</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
<context-param>
<param-name>dname</param-name>
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
</context-param>
</web-app>
welcome.jsp
<%
out.print("Welcome "+request.getParameter("uname"));
String driver=application.getInitParameter("dname");
out.print("driver name is="+driver);
%>
Output
6) session implicit object
In JSP, session is an implicit object of type HttpSession.
The Java developer can use this object to set,get or remove attribute or to get session information.
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
welcome.jsp
<html>
<body>
<%
String name=request.getParameter("uname");
out.print("Welcome "+name);
session.setAttribute("user",name);
%>
</body>
</html>
second.jsp
<html>
<body>
<%
String name=(String)session.getAttribute("user");
out.print("Hello "+name);
%>
</body>
</html>
Output
7) pageContext implicit object
In JSP, pageContext is an implicit object of type PageContext class.The pageContext object can be used to set,get
remove attribute from one of the following scopes:
o page
o request
o session
o application
index.html
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
welcome.jsp
<html>
<body>
<%
String name=request.getParameter("uname");
out.print("Welcome "+name);
pageContext.setAttribute("user",name,PageContext.SESSION_SCOPE);
%>
</body>
</html>
second.jsp
<html>
<body>
<%
String name=(String)pageContext.getAttribute("user",PageContext.SESSION_SCOPE);
out.print("Hello "+name);
%>
</body>
</html>
Output