Advanced Java Programming: Topic: Java Server Pages (JSP)
Advanced Java Programming: Topic: Java Server Pages (JSP)
By
Ravi Kant Sahu
Asst. Professor, LPU
Contents…
Introduction
Life cycle of a JSP Page
JSP Directory structure
JSP API
JSP Scripting elements
scriptlet tag
expression tag
declaration tag
JSP Implicit Objects
JSP Action Tags
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Java Server Pages
JavaServer Pages (JSP) is a Java technology for combining
Java with HTML to provide dynamic content for Web pages.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
JSP vs Servlet
JSP technology is the extension to Servlet technology with
all the features of the Servlet. In addition to, it provides
implicit objects, predefined tags, expression language and
Custom tags, that makes JSP development easy.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Directory Structure
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Steps to run a JSP Page
Start the server
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
JSP API
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
JSP API
The JSP API consists of two packages:
javax.servlet.jsp
javax.servlet.jsp.tagext
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
javax.servlet.jsp package
It has two interfaces:
JspPage
HttpJspPage
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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.
public void jspInit(): invoked only once during the life cycle of
the JSP when JSP page is requested firstly. It is used to
perform initialization.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
HttpJspPage Interface
The HttpJspPage interface provides the one life cycle method
of JSP.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
JSP Tags
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
JSP 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. So it doesn't get
memory at each request.
Syntax:
<%! field or method declaration %>
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
<html>
<body>
<%!
int result = 10;
int multiply (int a, int b){
return a*b;
}
%>
</body>
</html>
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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.
Syntax:
<%= statement %>
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
<html>
<body>
<%= “This is My JSP Page” %>
</body>
</html>
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
JSP Scriptlet Tag
A scriptlet tag is used to execute java source code in JSP
Syntax:
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
<html>
<body>
<% String name=request.getParameter("uname");
out.print("welcome "+name);
%>
</body>
</html>
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
JSP Implicit Objects
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Implicit Objects
There are 9 JSP implicit objects. These objects are created by
the web container that are available to all the JSP pages.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
JspWriter
abstract void clear()
abstract void close()
abstract void flush()
abstract void print(String s)
abstract void println(Object x)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
ServletRequest
Object getAttribute(String name)
Enumeration getAttributeName()
ServletInputStream getInputStream()
String getParameter(String name)
String getLocalAddr()
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
ServletRequest
Enumeration getParameterNames()
ServletContext getServletContext()
void removeAttribute(String name)
void setAttribute(String name, Object o)
RequestDispatcher getRequestDispatcher(String arg0)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
HttpServletRequest interface
Cookies getCookies()
String getQueryString()
HttpSession getSession()
String getMethod()
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
ServletResponse
PrintWriter getWriter()
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
HttpServletResponse Interface
void addCookie(Cookie cookie)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
HttpSession
public void setAttribute(String name, Object value)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
RequestDispatcher Interface
The RequestDispatcher Interface is used for dispatching the
request to a resource
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
sendRedirect() in Servlet
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Servlet: sendRedirect() and Request Dispatcher
sendRedirect() makes the client(browser) create a new request
to get to the resource, the user can see the new URL
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
JSP Directives
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
JSP Directives
The jsp directives are messages that tells the web container
how to translate a JSP page into the corresponding servlet.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
JSP page directive
The page directive defines attributes that apply to an entire JSP
page.
import
contentType
extends: defines the parent class that will be inherited by the
generated servlet.
Info
buffer
session
pageEncoding
errorPage
isErrorPage
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
import Example
<html>
<body>
<%@ page import="java.util.Date" %>
Today is: <%= new Date() %>
</body>
</html>
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
contentType Example
contentType attribute defines the MIME(Multipurpose Internet
Mail Extension) type of the HTTP response.
The default value is "text/html;charset=ISO-8859-1".
<html>
<body>
<%@ page contentType=application/msword %>
<%@ page import="java.util.Date" %>
Today is: <%= new Date() %>
</body>
</html>
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
info Example
The web container will create a method getServletInfo() in the
resulting servlet.
<html>
<body>
<%@ page info=“This is Advanced Java Programming"
%>
<%@ page import="java.util.Date" %>
Today is: <%= new Date() %>
</body>
</html>
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
buffer Example
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.
<html>
<body>
<%@ page buffer="16kb" %>
<%@ page import="java.util.Date" %>
Today is: <%= new Date() %>
</body>
</html>
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
errorPage Example
<html>
<body>
<%@ page errorPage="myerrorpage.jsp" %>
<%= 100/0 %>
</body>
</html>
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
isErrorPage Example
<html>
<body>
<%@ page isErrorPage="true" %>
Sorry an exception occured!<br/>
The exception is: <%= exception %>
</body>
</html>
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
JSP Include Directive
The include directive is used to include the contents of any
resource it may be jsp file, html file or text file.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Include Directive Example
<html>
<body>
</body>
</html>
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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.
Syntax:
<%@ taglib uri= “uri_of_tag_library”
prefix= “prefix_of_tag_library” %>
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Exception Handling
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Exception handling in JSP
<html>
<body>
<%@ page errorPage="myerrorpage.jsp" %>
<%= 100/0 %>
</body>
</html>
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Exception handling in JSP
In JSP, exception is an implicit object of Throwable. This
object can be used to print the exception. But it can only be
used in error pages.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Session Tracking
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Session Tracking
Session simply means a particular interval of time.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Session Tracking using HttpSession
The HttpServletRequest interface provides two methods to get
the object of HttpSession:
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods of HttpSession
1. public String getId(): Returns a string containing the unique
identifier value.
2. public long getCreationTime(): Returns the time when this
session was created, measured in milliseconds since midnight
January 1, 1970 GMT.
3. public long getLastAccessedTime(): Returns the last time the
client sent a request associated with this session, as the
number of milliseconds since midnight January 1, 1970
GMT.
4. public void invalidate(): Invalidates this session then unbinds
any objects bound to it.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MVC in JSP
MVC stands for Model View and Controller. It is a design
pattern that separates the business logic, presentation logic and
data.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
MVC in JSP
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Advantage of MVC
Navigation Control is centralized
Easy to maintain the large application
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
CSE406: Code Based
Test
Instructions:
1.Fill the Correct Options in the GRID given on the top of the
question paper in UPPER CASE.
2.Don’t
Overwrite in the GRID because it will be counted as
INCORRECT Answer.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)