Context and Servlet Initialization Parameters (#4656)
* Initial Commit * Add source files * Add readme.md * Add web.xml * Update pom.xml * Update pom.xml * Update pom.xml * Remove project folder * Update pom.xml
This commit is contained in:
parent
acf731377e
commit
ca1a1d70fa
|
@ -1,56 +0,0 @@
|
|||
<?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>
|
|
@ -1,14 +0,0 @@
|
|||
<?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>
|
|
@ -1,79 +1,104 @@
|
|||
<?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>
|
||||
<artifactId>javax-servlets</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- File Uploading -->
|
||||
<dependency>
|
||||
<groupId>commons-fileupload</groupId>
|
||||
<artifactId>commons-fileupload</artifactId>
|
||||
<version>1.3.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.6</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Servlet -->
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>4.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet.jsp.jstl</groupId>
|
||||
<artifactId>jstl-api</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet.jsp</groupId>
|
||||
<artifactId>javax.servlet.jsp-api</artifactId>
|
||||
<version>2.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>${org.apache.httpcomponents.version}</version>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>${gson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring-test.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<org.apache.httpcomponents.version>4.5.3</org.apache.httpcomponents.version>
|
||||
<spring-test.version>5.0.5.RELEASE</spring-test.version>
|
||||
<gson.version>2.8.2</gson.version>
|
||||
</properties>
|
||||
</project>
|
||||
<?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-servlets</groupId>
|
||||
<artifactId>javax-servlets</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
<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>
|
||||
|
||||
<!-- File Uploading -->
|
||||
<dependency>
|
||||
<groupId>commons-fileupload</groupId>
|
||||
<artifactId>commons-fileupload</artifactId>
|
||||
<version>1.3.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.6</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Servlet -->
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>4.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet.jsp.jstl</groupId>
|
||||
<artifactId>jstl-api</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet.jsp</groupId>
|
||||
<artifactId>javax.servlet.jsp-api</artifactId>
|
||||
<version>2.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>${org.apache.httpcomponents.version}</version>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>${gson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring-test.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<javax.servlet.version>3.1.0</javax.servlet.version>
|
||||
<org.apache.httpcomponents.version>4.5.3</org.apache.httpcomponents.version>
|
||||
<spring-test.version>5.0.5.RELEASE</spring-test.version>
|
||||
<gson.version>2.8.2</gson.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -1,49 +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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -1,9 +1,17 @@
|
|||
<?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_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>
|
||||
<error-page>
|
||||
<error-code>404</error-code>
|
||||
<location>/error-404.html</location> <!-- /src/main/webapp/error-404.html-->
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Context and Initialization Servlet Parameters</title>
|
||||
<title>Context and Servlet Initialization Parameters</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
</head>
|
||||
<body>
|
|
@ -1,52 +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");
|
||||
}
|
||||
package com.baeldung.servlets;
|
||||
|
||||
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