Read Context Param Values from Web.xml in JSP-Servlet


On the Eclipse, create a Maven project

Click Next button to select Workspace Location for project

Click Next button to select Archetype for project

Click Next button and enter Project Information:

  • Group Id: LearnJSPServletWithRealApps
  • Artifact Id: LearnJSPServletWithRealApps
  • Package: com.demo

Click Finish button to finish create Maven project




<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>LearnJSPServletWithRealApps</groupId>
	<artifactId>LearnJSPServletWithRealApps</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>Learn JSP-Servlet with Real Apps</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp.jstl</groupId>
			<artifactId>javax.servlet.jsp.jstl-api</artifactId>
			<version>1.2.1</version>
		</dependency>
		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>1.1.2</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>
	<build>
		<finalName>LearnJSPServletWithRealApps</finalName>
	</build>
</project>
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>

	<context-param>
		<param-name>authToken</param-name>
		<param-value>12345678ABCD</param-value>
	</context-param>

	<context-param>
		<param-name>postUrl</param-name>
		<param-value>https://www.sandbox.paypal.com/cgi-bin/webscr</param-value>
	</context-param>

	<context-param>
		<param-name>business</param-name>
		<param-value>abc@gmail.com</param-value>
	</context-param>

	<context-param>
		<param-name>status</param-name>
		<param-value>true</param-value>
	</context-param>

	<context-param>
		<param-name>loadOnStartup</param-name>
		<param-value>1</param-value>
	</context-param>

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

</web-app>




Create new package named com.demo.servlets. In this package, create new Servlet named DemoServlet as below:

package com.demo.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/demo")
public class DemoServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public DemoServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String authtoken = request.getServletContext().getInitParameter("authToken");
		String posturl = request.getServletContext().getInitParameter("postUrl");
		String business = request.getServletContext().getInitParameter("business");
		boolean status = Boolean.parseBoolean(request.getServletContext().getInitParameter("status"));
		int loadOnStartup = Integer.parseInt(request.getServletContext().getInitParameter("loadOnStartup"));
		request.setAttribute("authtoken", authtoken);
		request.setAttribute("posturl", posturl);
		request.setAttribute("business", business);
		request.setAttribute("status", status);
		request.setAttribute("loadOnStartup", loadOnStartup);
		request.getRequestDispatcher("demo/index.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}

}

Create new folder named demo in src\main\webapp folder. In this folder, create new JSP file named index.jsp as below:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>Demo Page</title>
</head>
<body>

	<h3>Context Param with Servlet</h3>
	Authtoken: ${authtoken }
	<br>
	Posturl: ${posturl }
	<br>
	Business: ${business }
	<br>
	Status: ${status }
	<br>
	LoadonStartup: ${loadOnStartup }

	<h3>Context Param with EL</h3>
	Authtoken: ${initParam.authToken }
	<br>
	Posturl: ${initParam.postUrl }
	<br>
	Business: ${initParam.business }
	<br>
	Status: ${initParam.status }
	<br>
	LoadonStartup: ${initParam.loadOnStartup }

</body>
</html>




Create new JSP file named index.jsp in src\main\webapp folder. This file will redirect to DemoServlet as below:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<jsp:forward page="demo"></jsp:forward>

Select project, right click and select Run As/Run on Server menu

Access DemoServlet with following urls as below:

  • http://localhost:8081/LearnJSPServletWithRealApps
  • http://localhost:8081/LearnJSPServletWithRealApps/demo

Output