JSP

JavaServer Pages (JSP) Basics Notes



1. What is JSP?

JavaServer Pages (JSP) is a server-side technology used to create dynamic, data-driven web pages by inserting Java code directly into HTML, XML, or other document types.


  • Purpose: Simplifies the creation of views (UI) compared to writing raw HTML strings inside Servlets.
  • MVC Role: Acts primarily as the View component in classic Model-View-Controller architecture.
  • Servlet Relationship: Every JSP page is automatically translated into a Java Servlet by the web container before execution.

2. JSP Lifecycle

When a client requests a JSP page, the web container manages it through five key phases:


[ JSP File ] ──► Translation ──► Compilation ──► Loading & Instantiation ──► Execution (_jspService)


  • Translation: The container parses the JSP and generates equivalent Java Servlet code.
  • Compilation: The generated .java servlet file is compiled into a .class bytecode file.
  • Loading & Instantiation: The container loads the class and creates a servlet instance.
  • Initialization (jspInit()): Called once to initialize resources.
  • Execution (_jspService()): Executed for every request to process inputs and return responses.
  • Destruction (jspDestroy()): Called when the page is removed from service.

3. JSP Scripting Elements

Scripting elements allow you to embed Java directly into HTML markup.












Element Syntax Description Example
Scriptlet <% ... %> Executes arbitrary Java code <% String user = "Sandeep"; %>
Expression <%= ... %> Evaluates and prints value directly to output <%= user %>
Declaration <%! ... %> Declares class-level variables and methods <%! int count = 0; %>
Directive <%@ ... %> Gives instructions to the JSP container <%@ page import="java.util.Date" %>
Comment <%-- ... --%> Hidden server-side comment (not visible in page source) <%-- DB logic here --%>

4. JSP Directives

Directives provide instructions to the container during translation.


Page Directive (<%@ page ... %>): Defines attributes for the entire page (imports, content type, error pages).

<%@ page contentType="text/html;charset=UTF-8" import="java.util.List" errorPage="error.jsp" %>

Include Directive (<%@ include ... %>): Merges content from another file during translation (static include).

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

Taglib Directive (<%@ taglib ... %>): Imports custom tag libraries like JSTL.

<%@ taglib prefix="c" uri="jakarta.tags.core" %>

5. Implicit Objects

JSP provides 9 pre-defined objects accessible directly within scriptlets and expressions without explicit declaration:


  • request: The HttpServletRequest instance.
  • response: The HttpServletResponse instance.
  • out: The JspWriter output stream used to send response text.
  • session: The HttpSession instance for user session tracking.
  • application: The ServletContext shared across all users.
  • config: The ServletConfig object for initialization parameters.
  • pageContext: Provides access to page attributes and environment details.
  • page: Reference to the current servlet instance (this).
  • exception: Captures thrown errors (available only in error pages where isErrorPage="true").

6. JSP Standard Actions

JSP standard actions control behavior using XML-style tags instead of raw Java scriptlets.


<jsp:include>: Dynamically includes another resource at request time.

<jsp:include page="footer.jsp" />

<jsp:forward>: Forwards the request to another page or servlet.

<jsp:forward page="login.jsp" />

<jsp:useBean> / <jsp:setProperty> / <jsp:getProperty>: Instantiates and interacts with JavaBeans.

<jsp:useBean id="user" class="com.example.UserBean" scope="session" />
<jsp:setProperty name="user" property="username" value="Sandeep" />
<p>Welcome, <jsp:getProperty name="user" property="username" /></p>

7. Expression Language (EL) & JSTL

Modern JSP development discourages raw Java scriptlets (<% %>) in favor of Expression Language (EL) and the JSP Standard Tag Library (JSTL) to keep views clean.


Expression Language (EL): Simplified syntax (${...}) to access attributes and properties.

<!-- Replaces <%= request.getAttribute("username") %> -->
<p>Hello, ${username}</p>

JSTL (Core tags): Replaces Java loops and conditional statements with standard tags.

<%@ taglib prefix="c" uri="jakarta.tags.core" %>

<c:if test="${not empty username}">
<h2>Welcome back, ${username}!</h2>
</c:if>

<c:forEach items="${productList}" var="item">
<p>${item.name} - ${item.price}</p>
</c:forEach>
Code Snippets 2
Hello JSP world
Basic

Hello JSP world. Save the code as hello.jsp under HelloServlet\src\main\webapp and open URL http://localhost:9090/HelloServlet/hello.jsp

<%@page language = "java" contentType = "text/html; charset=ISO-8859-1" pageEncoding = "ISO-8859-1" %><! DOCTYPE html >< html >< head >< meta charset = "ISO-8859-1" >< title > Insert title here </ title ></ head >< body > Hello JSP World </ body ></ html >

Scriptlet Demo
Basic

Scriptlet Demo

<%@page contentType = "text/html; charset=UTF-8" pageEncoding = "UTF-8" %>< html >< body >< div class = "val" ><% out.print("welcome to jsp");
%></ div ></ body ></ html >
Quiz

No quiz for this topic.

Discussion

Login to join the discussion.


No comments yet.