diff --git a/javax-servlet-init-params/pom.xml b/javax-servlet-init-params/pom.xml new file mode 100644 index 0000000000..4f766a7e2c --- /dev/null +++ b/javax-servlet-init-params/pom.xml @@ -0,0 +1,56 @@ + + + 4.0.0 + com.baeldung.javax-servlet-init-params + javax-servlet-init-params + 1.0 + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + junit + junit + 4.12 + test + + + org.assertj + assertj-core + 3.9.1 + test + + + org.hamcrest + hamcrest-core + 1.3 + test + + + org.mockito + mockito-core + 2.18.3 + test + + + javax + javaee-web-api + 7.0 + provided + + + jstl + jstl + 1.2 + + + javax.el + el-api + 2.2 + + + \ No newline at end of file diff --git a/javax-servlet-init-params/src/main/java/com/baeldung/servlets/UserServlet.java b/javax-servlet-init-params/src/main/java/com/baeldung/servlets/UserServlet.java new file mode 100644 index 0000000000..e195aa092a --- /dev/null +++ b/javax-servlet-init-params/src/main/java/com/baeldung/servlets/UserServlet.java @@ -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); + } +} \ No newline at end of file diff --git a/javax-servlet-init-params/src/main/webapp/WEB-INF/jsp/result.jsp b/javax-servlet-init-params/src/main/webapp/WEB-INF/jsp/result.jsp new file mode 100644 index 0000000000..01cc54858e --- /dev/null +++ b/javax-servlet-init-params/src/main/webapp/WEB-INF/jsp/result.jsp @@ -0,0 +1,16 @@ +<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> +<%@ page contentType="text/html" pageEncoding="UTF-8"%> + + + + + User Data + + +

User Information

+

Name: ${name}

+

Email: ${email}

+

Province: ${province}

+

Country: ${country}

+ + diff --git a/javax-servlet-init-params/src/main/webapp/WEB-INF/web.xml b/javax-servlet-init-params/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..1b801171b3 --- /dev/null +++ b/javax-servlet-init-params/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,14 @@ + + + + province + Mendoza + + + country + Argentina + + diff --git a/javax-servlet-init-params/src/main/webapp/index.jsp b/javax-servlet-init-params/src/main/webapp/index.jsp new file mode 100644 index 0000000000..ca41942e5d --- /dev/null +++ b/javax-servlet-init-params/src/main/webapp/index.jsp @@ -0,0 +1,19 @@ +<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> +<%@ page contentType="text/html" pageEncoding="UTF-8"%> + + + + Context and Initialization Servlet Parameters + + + +

Please fill the form below:

+
+ + + + + +
+ + diff --git a/javax-servlet-init-params/src/test/java/com/baeldung/test/UserServletUnitTest.java b/javax-servlet-init-params/src/test/java/com/baeldung/test/UserServletUnitTest.java new file mode 100644 index 0000000000..ef3d877dd7 --- /dev/null +++ b/javax-servlet-init-params/src/test/java/com/baeldung/test/UserServletUnitTest.java @@ -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"); + } +} \ No newline at end of file