Context and Servlet Initialization Parameters (#4594)
* Initial Commit * Update pom.xml
This commit is contained in:
parent
2d6136b475
commit
92acea607e
|
@ -0,0 +1,56 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.baeldung.javax-servlet-init-params</groupId>
|
||||||
|
<artifactId>javax-servlet-init-params</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>3.9.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hamcrest</groupId>
|
||||||
|
<artifactId>hamcrest-core</artifactId>
|
||||||
|
<version>1.3</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<version>2.18.3</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax</groupId>
|
||||||
|
<artifactId>javaee-web-api</artifactId>
|
||||||
|
<version>7.0</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>jstl</groupId>
|
||||||
|
<artifactId>jstl</artifactId>
|
||||||
|
<version>1.2</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.el</groupId>
|
||||||
|
<artifactId>el-api</artifactId>
|
||||||
|
<version>2.2</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
|
@ -0,0 +1,49 @@
|
||||||
|
package com.baeldung.servlets;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.annotation.WebInitParam;
|
||||||
|
import javax.servlet.annotation.WebServlet;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
@WebServlet(name = "UserServlet", urlPatterns = {"/userServlet"}, initParams={
|
||||||
|
@WebInitParam(name="name", value="Not provided"),
|
||||||
|
@WebInitParam(name="email", value="Not provided")})
|
||||||
|
public class UserServlet extends HttpServlet {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||||
|
throws ServletException, IOException {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||||
|
throws ServletException, IOException {
|
||||||
|
processRequest(request, response);
|
||||||
|
forwardRequest(request, response, "/WEB-INF/jsp/result.jsp");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
|
||||||
|
throws ServletException, IOException {
|
||||||
|
request.setAttribute("name", getRequestParameter(request, "name"));
|
||||||
|
request.setAttribute("email", getRequestParameter(request, "email"));
|
||||||
|
request.setAttribute("province", getContextParameter("province"));
|
||||||
|
request.setAttribute("country", getContextParameter("country"));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String getRequestParameter(HttpServletRequest request, String name) {
|
||||||
|
String param = request.getParameter(name);
|
||||||
|
return !param.isEmpty() ? param : getInitParameter(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String getContextParameter(String name) {
|
||||||
|
return getServletContext().getInitParameter(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void forwardRequest(HttpServletRequest request, HttpServletResponse response, String path)
|
||||||
|
throws ServletException, IOException {
|
||||||
|
request.getRequestDispatcher(path).forward(request, response);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
|
||||||
|
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||||
|
<title>User Data</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h2>User Information</h2>
|
||||||
|
<p><strong>Name:</strong> ${name}</p>
|
||||||
|
<p><strong>Email:</strong> ${email}</p>
|
||||||
|
<p><strong>Province:</strong> ${province}</p>
|
||||||
|
<p><strong>Country:</strong> ${country}</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
|
||||||
|
version="3.1">
|
||||||
|
<context-param>
|
||||||
|
<param-name>province</param-name>
|
||||||
|
<param-value>Mendoza</param-value>
|
||||||
|
</context-param>
|
||||||
|
<context-param>
|
||||||
|
<param-name>country</param-name>
|
||||||
|
<param-value>Argentina</param-value>
|
||||||
|
</context-param>
|
||||||
|
</web-app>
|
|
@ -0,0 +1,19 @@
|
||||||
|
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
|
||||||
|
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Context and Initialization Servlet Parameters</title>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h2>Please fill the form below:</h2>
|
||||||
|
<form action="${pageContext.request.contextPath}/userServlet" method="post">
|
||||||
|
<label for="name"><strong>Name:</strong></label>
|
||||||
|
<input type="text" name="name" id="name">
|
||||||
|
<label for="email"><strong>Email:</strong></label>
|
||||||
|
<input type="text" name="email" id="email">
|
||||||
|
<input type="submit" value="Send">
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -0,0 +1,52 @@
|
||||||
|
package com.baeldung.test;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.mockito.Mockito.atLeast;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
public class UserServletUnitTest {
|
||||||
|
|
||||||
|
private static HttpServletRequest request;
|
||||||
|
private static HttpServletResponse response;
|
||||||
|
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setUpHttpServletRequestMockInstance() {
|
||||||
|
request = mock(HttpServletRequest.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setUpHttpServletResponsetMockInstance() {
|
||||||
|
response = mock(HttpServletResponse.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenHttpServletRequestMockInstance_whenCalledgetParameter_thenCalledAtLeastOnce() {
|
||||||
|
request.getParameter("name");
|
||||||
|
verify(request, atLeast(1)).getParameter("name");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenHttpServletRequestMockInstance_whenCalledgetParameter_thenOneAssertion() {
|
||||||
|
when(request.getParameter("name")).thenReturn("username");
|
||||||
|
assertThat(request.getParameter("name")).isEqualTo("username");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenHttpServletResponseMockInstance_whenCalledgetContentType_thenCalledAtLeastOnce() {
|
||||||
|
response.getContentType();
|
||||||
|
verify(response, atLeast(1)).getContentType();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenHttpServletResponseMockInstance_whenCalledgetContentType_thenOneAssertion() {
|
||||||
|
when(response.getContentType()).thenReturn("text/html");
|
||||||
|
assertThat(response.getContentType()).isEqualTo("text/html");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue