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?

Leave a comment

Your email address will not be published. Required fields are marked *