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>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 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>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>
<%@ 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>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>
<%@ 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>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>
<?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>
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.
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.
<%@ 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>ourownjava.com</title>
</head>
<body>
<%
Date date = new Date();
%>
Current time is : <%= date %>
</body>
</html>
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.