pageContext

Name Interface Tomcat Implementation
pageContext javax.servlet.jsp.PageContext org.apache.jasper.runtime.PageContextImpl

JSP implicit object pageContext example program (github)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp.ourownjava.com</title>
</head>
<body>

<!-- jsp implicit object pageContext example -->

<!-- how to set attribute into jsp implicit object pageContext -->
<% pageContext.setAttribute("attribute1", "attribute1Value"); %>
<!-- how to get attribute from pageContext -->
pageContext parameter : <% out.println(pageContext.getAttribute("attribute1")); %> <br/>
<!-- how to access request parameter using jsp implicit object pageContext -->
Request parameter who : <% out.println(pageContext.getRequest().getParameter("who")); %> <br/>
<!-- how to access session object using jsp implicit object pageContext -->
Session id : <% out.println(pageContext.getSession().getId()); %>

</body>
</html>

How to run the JSP page context sample program

http://localhost:8080/jsp-play-ground/jsp-implicit-object-pageContext-example.jsp?who=ourownjava.com
 

page

Name Interface
page javax.servlet.jsp.HttpJspPage

What is JSP implicit object page?

The page variable is an alias for ‘this’, that refers to the servlet instance.

JSP implicit object page example program (github)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp.ourownjava.com</title>
</head>
<body>

<!-- jsp implicit object page example use -->

JSP Engine : <%=((Servlet)page).getServletInfo() %> <p/>

Servlet Config : <%=((Servlet)page).getServletConfig() %> <p/>

</body>
</html>
 

out

Name Interface Tomcat Implementation
out javax.servlet.jsp.JspWriter org.apache.jasper.runtime.JspWriterImpl

How to access JSP implicit object out in a JSP file?

<%out.print("to the screen");%>

JSP implicit object out example program

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp.ourownjava.com</title>
</head>
<body>

<!-- jsp implicit object out example -->

JSP implicit object out class : <%=out.getClass().getCanonicalName() %> <p/>

<!-- the above line will return org.apache.jasper.runtime.JspWriterImpl,
its an implementation of javax.servlet.jsp.JspWriter -->

<%out.print("to the screen");%>

</body>
</html>
 

config

Name Interface Tomcat Implementation
config javax.servlet.ServletConfig org.apache.catalina.core.StandardWrapperFacade

How to access servlet config parameters in jsp?

Servlet config init parameter : <%=config.getInitParameter("servletConfigParm1")%>

JSP Implicit Object config Example (github)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp.ourownjava.com</title>
</head>
<body>

<!-- jsp implicit object config example -->

JSP implicit object config class : <%=config.getClass().getCanonicalName() %> <p/>

<!-- the above line will return org.apache.catalina.core.StandardWrapperFacade,
its an implementation of javax.servlet.ServletConfig -->

<!-- access the servlet config parameter from the web.xml -->

Servlet config init parameter : <%=config.getInitParameter("servletConfigParm1")%>

</body>
</html>

Servlet Context Parameters in web.xml (github)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

<display-name>jsp.ourownjava.com</display-name>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- servlet init parameter, for this particular servlet, jsp implicit object 'config' would make available
these parameters, class is javax.servlet.ServletConfig -->
<servlet>
<servlet-name>jspImplicitObjectConfigExample</servlet-name>
<jsp-file>/jsp-implicit-object-config-example.jsp</jsp-file>
<init-param>
<param-name>servletConfigParm1</param-name>
<param-value>servletConfigParam1Value</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>jspImplicitObjectConfigExample</servlet-name>
<url-pattern>/jsp-implicit-object-config-example.jsp</url-pattern>
</servlet-mapping>

</web-app>

How to run the example program?

 

application

Name Interface Tomcat Impelementation
application javax.servlet.ServletContext org.apache.catalina.core.ApplicationContextFacade

How to access servlet context parameters in jsp?

Servlet context parameter : <%=application.getInitParameter("param1")%>

JSP Implicit Object application Example (github)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp.ourownjava.com</title>
</head>
<body>

<!-- jsp implicit object application example -->

JSP implicit object application class : <%=application.getClass().getCanonicalName() %> <p/>

<!-- the above line will return org.apache.catalina.core.ApplicationContextFacade,
its an implementation of javax.servlet.ServletContext -->

<!-- servlet context parameters, application wide, jsp implicit object 'application' would make available
these parameters, class is javax.servlet.ServletContext
<context-param>
<param-name>param1</param-name>
<param-value>param1Value</param-value>
</context-param>
-->

<!-- access the above parameter from the web.xml -->

Servlet context parameter : <%=application.getInitParameter("param1")%>

</body>
</html>

Servlet Context Parameters in web.xml (github)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

<display-name>jsp.ourownjava.com</display-name>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- servlet context parameters, application wide, jsp implicit object 'application' would make available
these parameters, class is javax.servlet.ServletContext -->
<context-param>
<param-name>param1</param-name>
<param-value>param1Value</param-value>
</context-param>

<!-- servlet init parameter, for this particlar servlet, jsp implicit object 'config' would make available
these parameters, class is javax.servlet.ServletConfig -->
<servlet>
<servlet-name>jspImplicitObjectApplicationExample</servlet-name>
<jsp-file>/jsp-implicit-object-application-example.jsp</jsp-file>
<init-param>
<param-name>servletConfigParm1</param-name>
<param-value>servletConfigParam1Value</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>jspImplicitObjectApplicationExample</servlet-name>
<url-pattern>/jsp-implicit-object-application-example.jsp</url-pattern>
</servlet-mapping>

</web-app>

How to run the example program?

 

Implicit Objects

JSP implicit objects are a collection of java objects that the JSP/Servlet container makes available to developers in each and every jsp page.

How to access the JSP implicit objects?

  1. JSP implicit objects can be accessed as variables via any JSP scripting elements.
  2. JSP implicit objects can be accessed programmatically by JavaBeans and Servlets.

JSP Implicit Objects

Object Class/Interface Description
application javax.servlet.ServletContext Application wide shared state
config javax.servlet.ServletConfig Configuration for a servlet
out javax.servlet.jsp.JspWriter Output stream for page context
page javax.servlet.jsp.HttpJspPage JSP page's servlet instance
pageContext javax.servlet.jsp.PageContext Gateway to all the namespaces associated with a JSP page and access to several page attributes
request javax.servlet.http.HttpServletRequest HTTP Request
response javax.servlet.http.HttpServletResponse HTTP Response
session javax.servlet.http.HttpSession User session data
 

Expressions

Expression is used to insert the value of a java language expression. Expression values are converted into String object and would be put into the implicit out object.

JSP Expression Syntax

<%= scripting-language-expression %>

JSP Expression Sample Program (github)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp.ourownjava.com</title>
</head>
<body>
<%
final String who;
who = request.getParameter("who");
%>

<h4>Hello <%= who %>!</h4>
</body>
</html>

How to run JSP Expression Sample Program

http://localhost:8080/jsp-play-ground/jsp-expressions-example.jsp?who=ourownjava.com

 

Declarations

Declaration is used to declare variables and methods in a JSP page’s scripting language. All variables and methods in JSP declaration would be translated into declaration inside respective Servlet class.

JSP declaration syntax

<%! scripting language declaration %>

JSP declaration sample code (github)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp.ourownjava.com</title>
</head>
<body>

<!-- jsp declaration example-->
<%! 
public int sum(final int x, final int y){
    return x + y;
}
%>

<!-- calling method -->
<% out.println("sum of 10, 10 is "+sum(10, 10));%>

</body>
</html>

How to run the JSP declaration sample program?

 

Directives

JSP directives tells the web container how to translate a JSP page into a Servlet.

Types of directives

Name Syntax Note
Page Directive <%@page attribute="value"%> Defines attributes applicable to an entire JSP page.
Include Directive <%@include file="resource name"%> To include the contents of any resource.
Taglib Directive <%@taglib uri="uri of tag lib" prefix="prefix for tag lib"%> Used to define a tag library that may contains many tags.

Sample code for page directive (github)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!-- page import directive -->
<%@ page import="java.util.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp.ourownjava.com</title>
</head>
<body>
<%
Date date = new Date();
%>
Current time is : <%= date %>
</body>
</html>

Sample code for include directive (github)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp.ourownjava.com</title>
</head>
<body>
<h4>This is the header.jsp</h4>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp.ourownjava.com</title>
</head>
<body>
<!-- include directive example -->
<%@ include file="header.jsp" %>
</body>
</html>

Taglib directive example (github)

<!-- taglib directive example -->
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp.ourownjava.com</title>
</head>
<body>

<!-- taglib directive example -->
<c:out value="output from c out tag"/>

</body>
</html>

How to run the sample programs

Take a quiz on JSP directives here

 

Scriptlet

Scriptlet let developers write java code within jsp file. Though this is not a recommended practice from the code maintenance perspective its widely used in the initial version of jsp applications.

JSP Scriptlet Syntax

<% java code fragment %>

Simple Example (github)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp.ourownjava.com</title>
</head>
<body>
<!-- scriptlet example -->
<%out.println("Today is "+new java.util.Date()); %>
</body>
</html>

JSP Scriptlet sample program

https://github.com/sanjuthomas/jsp-play-ground

Run JSP Scriptlet example program