Skip to content

Commit 92acea6

Browse files
Alejandro GervasioKevinGilmore
authored andcommitted
Context and Servlet Initialization Parameters (eugenp#4594)
* Initial Commit * Update pom.xml
1 parent 2d6136b commit 92acea6

6 files changed

Lines changed: 206 additions & 0 deletions

File tree

‎javax-servlet-init-params/pom.xml‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung.javax-servlet-init-params</groupId>
5+
<artifactId>javax-servlet-init-params</artifactId>
6+
<version>1.0</version>
7+
8+
<parent>
9+
<groupId>com.baeldung</groupId>
10+
<artifactId>parent-modules</artifactId>
11+
<version>1.0.0-SNAPSHOT</version>
12+
</parent>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>junit</groupId>
17+
<artifactId>junit</artifactId>
18+
<version>4.12</version>
19+
<scope>test</scope>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.assertj</groupId>
23+
<artifactId>assertj-core</artifactId>
24+
<version>3.9.1</version>
25+
<scope>test</scope>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.hamcrest</groupId>
29+
<artifactId>hamcrest-core</artifactId>
30+
<version>1.3</version>
31+
<scope>test</scope>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.mockito</groupId>
35+
<artifactId>mockito-core</artifactId>
36+
<version>2.18.3</version>
37+
<scope>test</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>javax</groupId>
41+
<artifactId>javaee-web-api</artifactId>
42+
<version>7.0</version>
43+
<scope>provided</scope>
44+
</dependency>
45+
<dependency>
46+
<groupId>jstl</groupId>
47+
<artifactId>jstl</artifactId>
48+
<version>1.2</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>javax.el</groupId>
52+
<artifactId>el-api</artifactId>
53+
<version>2.2</version>
54+
</dependency>
55+
</dependencies>
56+
</project>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.servlets;
2+
3+
import java.io.IOException;
4+
import javax.servlet.ServletException;
5+
import javax.servlet.annotation.WebInitParam;
6+
import javax.servlet.annotation.WebServlet;
7+
import javax.servlet.http.HttpServlet;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
11+
@WebServlet(name = "UserServlet", urlPatterns = {"/userServlet"}, initParams={
12+
@WebInitParam(name="name", value="Not provided"),
13+
@WebInitParam(name="email", value="Not provided")})
14+
public class UserServlet extends HttpServlet {
15+
16+
@Override
17+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
18+
throws ServletException, IOException {
19+
}
20+
21+
@Override
22+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
23+
throws ServletException, IOException {
24+
processRequest(request, response);
25+
forwardRequest(request, response, "/WEB-INF/jsp/result.jsp");
26+
}
27+
28+
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
29+
throws ServletException, IOException {
30+
request.setAttribute("name", getRequestParameter(request, "name"));
31+
request.setAttribute("email", getRequestParameter(request, "email"));
32+
request.setAttribute("province", getContextParameter("province"));
33+
request.setAttribute("country", getContextParameter("country"));
34+
}
35+
36+
protected String getRequestParameter(HttpServletRequest request, String name) {
37+
String param = request.getParameter(name);
38+
return !param.isEmpty() ? param : getInitParameter(name);
39+
}
40+
41+
protected String getContextParameter(String name) {
42+
return getServletContext().getInitParameter(name);
43+
}
44+
45+
protected void forwardRequest(HttpServletRequest request, HttpServletResponse response, String path)
46+
throws ServletException, IOException {
47+
request.getRequestDispatcher(path).forward(request, response);
48+
}
49+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
2+
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
3+
<!DOCTYPE html>
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7+
<title>User Data</title>
8+
</head>
9+
<body>
10+
<h2>User Information</h2>
11+
<p><strong>Name:</strong> ${name}</p>
12+
<p><strong>Email:</strong> ${email}</p>
13+
<p><strong>Province:</strong> ${province}</p>
14+
<p><strong>Country:</strong> ${country}</p>
15+
</body>
16+
</html>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
5+
version="3.1">
6+
<context-param>
7+
<param-name>province</param-name>
8+
<param-value>Mendoza</param-value>
9+
</context-param>
10+
<context-param>
11+
<param-name>country</param-name>
12+
<param-value>Argentina</param-value>
13+
</context-param>
14+
</web-app>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
2+
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
3+
<!DOCTYPE html>
4+
<html>
5+
<head>
6+
<title>Context and Initialization Servlet Parameters</title>
7+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8+
</head>
9+
<body>
10+
<h2>Please fill the form below:</h2>
11+
<form action="${pageContext.request.contextPath}/userServlet" method="post">
12+
<label for="name"><strong>Name:</strong></label>
13+
<input type="text" name="name" id="name">
14+
<label for="email"><strong>Email:</strong></label>
15+
<input type="text" name="email" id="email">
16+
<input type="submit" value="Send">
17+
</form>
18+
</body>
19+
</html>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.test;
2+
3+
import javax.servlet.http.HttpServletRequest;
4+
import javax.servlet.http.HttpServletResponse;
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
import static org.mockito.Mockito.atLeast;
9+
import static org.mockito.Mockito.mock;
10+
import static org.mockito.Mockito.verify;
11+
import static org.mockito.Mockito.when;
12+
13+
public class UserServletUnitTest {
14+
15+
private static HttpServletRequest request;
16+
private static HttpServletResponse response;
17+
18+
19+
@BeforeClass
20+
public static void setUpHttpServletRequestMockInstance() {
21+
request = mock(HttpServletRequest.class);
22+
}
23+
24+
@BeforeClass
25+
public static void setUpHttpServletResponsetMockInstance() {
26+
response = mock(HttpServletResponse.class);
27+
}
28+
29+
@Test
30+
public void givenHttpServletRequestMockInstance_whenCalledgetParameter_thenCalledAtLeastOnce() {
31+
request.getParameter("name");
32+
verify(request, atLeast(1)).getParameter("name");
33+
}
34+
35+
@Test
36+
public void givenHttpServletRequestMockInstance_whenCalledgetParameter_thenOneAssertion() {
37+
when(request.getParameter("name")).thenReturn("username");
38+
assertThat(request.getParameter("name")).isEqualTo("username");
39+
}
40+
41+
@Test
42+
public void givenHttpServletResponseMockInstance_whenCalledgetContentType_thenCalledAtLeastOnce() {
43+
response.getContentType();
44+
verify(response, atLeast(1)).getContentType();
45+
}
46+
47+
@Test
48+
public void givenHttpServletResponseMockInstance_whenCalledgetContentType_thenOneAssertion() {
49+
when(response.getContentType()).thenReturn("text/html");
50+
assertThat(response.getContentType()).isEqualTo("text/html");
51+
}
52+
}

0 commit comments

Comments
 (0)