0% found this document useful (0 votes)
10 views

Advanced Java Programming: Topic: Java Server Pages (JSP)

Java server pages

Uploaded by

Piyush Verma
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Advanced Java Programming: Topic: Java Server Pages (JSP)

Java server pages

Uploaded by

Piyush Verma
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 57

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.

 A JSP page consists of HTML tags and JSP tags.

 It is an advanced version of Servlet Technology.

 JSP is first converted into servlet by JSP container before


processing the client’s request.

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.

 JSP can be easily managed because we can easily separate


our business logic with presentation logic. In Servlet,
business logic is mixed with the presentation logic.

 Fast Development: No need to recompile and redeploy.

 Less code than Servlet


JSP Life Cycle
JSP Life Cycle
The JSP pages follow these phases:

Translation of JSP Page


Compilation of JSP Page
Class loading (the classloader loads class file)
Instantiation (Object of the Generated Servlet is created).
Initialization (the container invokes jspInit() method).
Request processing (the container invokes _jspService() method).
Destroy (the container invokes jspDestroy() method).

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

 Put the JSP file in a folder and deploy on the server

 Visit the browser using the URL


http://localhost:portno/contextRoot/jspfile

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

It has following classes:


JspWriter
PageContext
JspFactory
JspEngineInfo
JspException
JspError

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.

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.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
javax.servlet.jsp package
It has two interfaces:
JspPage
HttpJspPage

It has following classes:


JspWriter
PageContext
JspFactory
JspEngineInfo
JspException
JspError

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
HttpJspPage Interface
 The HttpJspPage interface provides the one life cycle method
of JSP.

 It extends the JspPage interface.

public void _jspService (): It is invoked each time when request


for the JSP page comes to the container.
It is used to process the request.

[The underscore _ signifies that we can’t override this method]

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.

 It is mainly used to print the values of variable or method.

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:

<% java source code %>

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)

 String getHeader(String name)

 void setHeader(String name, String value)

 void sendRedirect(String arg0) throws IOException

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
HttpSession
 public void setAttribute(String name, Object value)

 public Object getAttribute(String name)

 public Enumeration getAttributeNames()

 public void removeAttribute(String name)

 void setMaxInactiveInterval(int interval)

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
RequestDispatcher Interface
The RequestDispatcher Interface is used for dispatching the
request to a resource

public void forward(ServletRequest request,ServletResponse


response)throws ServletException,java.io.IOException

public void include(ServletRequest request,ServletResponse


response)throws ServletException,java.io.IOException

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
sendRedirect() in Servlet

 sendRedirect() method redirects the response to another


resource.

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

 request dispatch get the resource in same request and URL


does not changes.

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.

 It is used to specify core functionalities for our JSP files.

 There are three types of directives:


 page directive
 include directive
 taglib directive

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.

 The include directive includes the original content of the


included resource at page translation time.

 It ensures Code Reusability.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Include Directive Example
<html>
<body>

<%@ include file="header.html" %>


Today is: <%= java.util.Calendar.getInstance().getTime() %>

</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

 Error can be handle by errorPage and isErrorPage attributes of


page directive.

<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.

<%@ page isErrorPage="true" %>


<html>
<body>
Sorry following exception occurred:<%= exception %>
</body>
</html>

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.

 Session Tracking (Session Management) is a way to maintain


state (data) of an user.

 Http protocol is a stateless so we need to maintain state using


session tracking techniques. Each time user requests to the
server, server treats the request as the new request. So we need
to maintain the state of an user to recognize to particular user.

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:

1.public HttpSession getSession(): Returns the current session


associated with this request, or if the request does not have a
session, creates one.
2.public HttpSession getSession(boolean create): Returns the
current HttpSession associated with this request or, if there is no
current session and create is true, returns a new session.

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.

 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).

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.

3.There is 25% Negative Marking for every WRONG Answer.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

You might also like