What is JSP?

  • JSP is a technology for developing web pages.
  • JSP are built on top of the Java Servlets API, so like Servlets, JSP also has access to all the powerful Enterprise Java APIs, including JDBC, JNDI, EJB etc.
  •  It is more convenient to write and to modify regular HTML than to have plenty of println statements that generate the HTML like in Servlets.
  • JSP pages can be used in combination with Servlets that handle the business logic
  • JSP is an integral part of J2EE, a complete platform for enterprise applications.  This means that JSP can play a part in the simplest applications to the most complex and demanding.

JSP Architecture:
The architecture of JSP is explained in diagram below:

JSP Architecture

JSP Life Cycle

  • JSP life cycle can be defined as the entire process from its creation till the destruction which is similar to a Servlet life cycle with an additional step which is required to compile a JSP into Servlet. 
  • JSP Compilation 
  • JSP Initialization -  jspInit() 
    • If you need to perform JSP-specific initialization, override the jspInit() method. 
    • jspInit() is generally used for allocating resources like initializing database connections, open files, and create lookup tables. 
  • JSP Execution - _jspService(request,response) 
  • JSP Cleanup - jspDestroy() 
    • It is good programming practice to free any allocated resources within jspDestroy(). For example to perform any cleanup operations, such as closing database connections or closing any open files, override jspDestroy() method. 
JSP Syntax
  • Scriptlet:
  • A scriptlet can contain any number of JAVA language statements
  • Following is the syntax of Scriptlet:
  • <% code fragment %>

System.out.println( "Evaluating date now" );
Date date = new Date();
Hello! The time is: <%= date %>
  • JSP Expression
  • A JSP expression element contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file.
  • The expression element can contain any expression that is valid according to the Java Language Specification but you cannot use a semicolon to end an expression.
  • Syntax

Hello! The time is: <%= new Date().toLocaleString()%>


  • Output
The time is: 11-Sep-2010 21:24:25
  • JSP Declarations
  • A declaration declares one or more variables or methods that you can use in Java code later in the JSP file. You must declare the variable or method before you use it in the JSP file.
  • Syntax
<%! ---------%>
<%! int a, b, c; %>
<%! Date date = new Date(); > 

JSP Directives

  • There are three types of JSP directive tags:
  • page directive
    • Syntax of page directive
         <%@ page import="java.util.*,java.text.*" %>
  • include directive 
    • Syntax of include Directive
Going to include hello.jsp...<BR>
<%@ include file="hello.jsp" %>
  • taglib directive 
    • Declares a tag library, containing custom actions, used in the page 
    • Syntax of taglib directive 
        <%@ taglib uri="uri" prefix="prefixOfTag" >

JSP Actions

  • JSP actions use constructs in XML syntax to control the behavior of the Servlet engine.
  • There is only one syntax for the Action element, as it conforms to the XML standard:
  • Syntax
    <jsp:action_name attribute="value" />
  • Example
          <jsp:forward attribute="value" /> 
          <jsp:useBean attribute="value" />

JSP comments

  • JSP comments are converted by the JSP engine into java comments
  • There are two types of comments in JSP 
    • Hidden comments:
This type of comment will not appear in the output.
<%-- Hidden comment --%>
    • Output comments
This type of comment will appear in the output.
<!-- Output comment>

JSP Tags

  • A JSP tag is somewhat like an HTML tag
  •  JSP tags can have a "start tag", a "tag body" and an "end tag".
Going to include hello.jsp...<BR>
<jsp:include page="hello.jsp"/>

JSP Implicit Objects

  • JSP Implicit Objects are the Java objects that the JSP Container makes available to developers in each page and developer can call them directly without being explicitly declared.
  • JSP Implicit Objects are also called pre-defined variables.
  • JSP supports nine Implicit Objects which are listed below:
  1. request
  2. response
  3. out
  4. session 
  5. application
  6. config
  7. pageContext
  8. page
  9. Exception

JSP Filters

  • Servlet and JSP Filters are Java classes that can be used in Servlet and JSP Programming for the following purposes:
    • To intercept requests from a client before they access a resource at back end.
    • To manipulate responses from server before they are sent back to the client.
  • Filters are used for monitoring requests and responses
  • Filters can modify the request or response headers and data by providing a customized version.


Cookies are text files stored on the client computer and they are kept for various information tracking purpose.

  • Setting Cookies 
           Setting cookies with JSP involves three steps:
  • Creating a Cookie object 
                     Cookie cookie = new Cookie("key","value");
  •  Setting the maximum age 
  •  Sending the Cookie into the HTTP response headers
  •  Reading Cookies
    • To read cookies, you need to create an array of javax.servlet.http.Cookie objects by calling the getCookies( ) method of HttpServletRequest.
  • Deleting Cookies
  • To delete cookies is very simple. If you want to delete a cookie then you simply need to follow up following three steps:
  • Read an already exsiting cookie and store it in Cookie object.
  • Set cookie age as zero using setMaxAge() method to delete an existing cookie.
  • Add this cookie back into response


  • HTTP is a "stateless" protocol which means each time a client retrieves a Web page, the client opens a separate connection to the Web server and the server automatically does not keep any record of previous client request.
  • Still there are following three ways to maintain session between web client and web server:
    • Cookies
    • Hidden Form Fields
    • URL Rewriting
  • Sessions are always stored in the server side whereas cookies are always stored in the client side.
  • By default, JSPs have session tracking enabled and a new HttpSession object is instantiated for each new client automatically.
  • Disabling session tracking requires explicitly turning it off by setting the page directive session attribute to false as follows:
          <%@ page session="false" %>
  • To terminate session use this syntax
           <% session.invalidate(); %>
  • Note: session is an implicit JSP object which is available to JSP programmer
JSP Expression Language (EL)
  • JSP Expression Language (EL) makes it possible to easily access application data stored in JavaBeans components.
  • Syntax: ${expr}
  • Note: JSP and JSF each has its own EL

Making a thread safe JSP page
  • JSP Thread Safe is used to send only one client request for processing
  • The page directive defines an attribute 'isThreadSafe' whose value is either true or false.
    • If the value is set to true which is the default value, the JSP container can send multiple concurrent client requests to the JSP page.
    • If the value of this attribute is set to false, then the JSP container sends client requests only one at a time to the JSP page which makes the JSP page thread safe.
  • Syntax
           <%@ page isThreadSafe="false" %>

JSP - Auto Refresh
  • The simplest way of Auto-refreshing a JSP web page is using method setIntHeader() of response object.
  • Method signature
public void setIntHeader(String header, int headerValue);

  • Example
    response.setIntHeader("Refresh", 5);

No comments:

Post a Comment