diff --git a/javax-servlet-init-params/pom.xml b/javax-servlet-init-params/pom.xml deleted file mode 100644 index 4f766a7e2c..0000000000 --- a/javax-servlet-init-params/pom.xml +++ /dev/null @@ -1,56 +0,0 @@ - - - 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/webapp/WEB-INF/web.xml b/javax-servlet-init-params/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index 1b801171b3..0000000000 --- a/javax-servlet-init-params/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - province - Mendoza - - - country - Argentina - - diff --git a/javax-servlets/pom.xml b/javax-servlets/pom.xml index f64ce67a1f..fadf0216ce 100644 --- a/javax-servlets/pom.xml +++ b/javax-servlets/pom.xml @@ -1,79 +1,104 @@ - - - 4.0.0 - javax-servlets - 1.0-SNAPSHOT - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - - - - - - commons-fileupload - commons-fileupload - 1.3.3 - - - commons-io - commons-io - 2.6 - - - - - javax.servlet - javax.servlet-api - 4.0.1 - - - javax.servlet.jsp.jstl - jstl-api - 1.2 - - - javax.servlet.jsp - javax.servlet.jsp-api - 2.3.1 - - - javax.servlet - jstl - 1.2 - - - - org.apache.httpcomponents - httpclient - ${org.apache.httpcomponents.version} - test - - - commons-logging - commons-logging - - - - - com.google.code.gson - gson - ${gson.version} - - - org.springframework - spring-test - ${spring-test.version} - test - - - - - 4.5.3 - 5.0.5.RELEASE - 2.8.2 - - \ No newline at end of file + + + 4.0.0 + com.baeldung.javax-servlets + javax-servlets + 1.0-SNAPSHOT + war + + 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 + + + + + commons-fileupload + commons-fileupload + 1.3.3 + + + commons-io + commons-io + 2.6 + + + + + javax.servlet + javax.servlet-api + 4.0.1 + + + javax.servlet.jsp.jstl + jstl-api + 1.2 + + + javax.servlet.jsp + javax.servlet.jsp-api + 2.3.1 + + + javax.servlet + jstl + 1.2 + + + + org.apache.httpcomponents + httpclient + ${org.apache.httpcomponents.version} + test + + + commons-logging + commons-logging + + + + + com.google.code.gson + gson + ${gson.version} + + + org.springframework + spring-test + ${spring-test.version} + test + + + + 3.1.0 + 4.5.3 + 5.0.5.RELEASE + 2.8.2 + + diff --git a/javax-servlet-init-params/src/main/java/com/baeldung/servlets/UserServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/UserServlet.java similarity index 97% rename from javax-servlet-init-params/src/main/java/com/baeldung/servlets/UserServlet.java rename to javax-servlets/src/main/java/com/baeldung/servlets/UserServlet.java index e195aa092a..269cee3922 100644 --- a/javax-servlet-init-params/src/main/java/com/baeldung/servlets/UserServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/UserServlet.java @@ -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); + } } \ No newline at end of file diff --git a/javax-servlet-init-params/src/main/webapp/WEB-INF/jsp/result.jsp b/javax-servlets/src/main/webapp/WEB-INF/jsp/result.jsp similarity index 100% rename from javax-servlet-init-params/src/main/webapp/WEB-INF/jsp/result.jsp rename to javax-servlets/src/main/webapp/WEB-INF/jsp/result.jsp diff --git a/javax-servlets/src/main/webapp/WEB-INF/web.xml b/javax-servlets/src/main/webapp/WEB-INF/web.xml index c9a06ac52d..383be7ff25 100644 --- a/javax-servlets/src/main/webapp/WEB-INF/web.xml +++ b/javax-servlets/src/main/webapp/WEB-INF/web.xml @@ -1,9 +1,17 @@ + - + + province + Mendoza + + + country + Argentina + 404 /error-404.html diff --git a/javax-servlet-init-params/src/main/webapp/index.jsp b/javax-servlets/src/main/webapp/user.jsp similarity index 90% rename from javax-servlet-init-params/src/main/webapp/index.jsp rename to javax-servlets/src/main/webapp/user.jsp index ca41942e5d..c6e1bb69f9 100644 --- a/javax-servlet-init-params/src/main/webapp/index.jsp +++ b/javax-servlets/src/main/webapp/user.jsp @@ -3,7 +3,7 @@ - Context and Initialization Servlet Parameters + Context and Servlet Initialization Parameters diff --git a/javax-servlets/src/test/java/com/baeldung/servlets/FormServletLiveTest.java b/javax-servlets/src/test/java/com/baeldung/test/FormServletLiveTest.java similarity index 100% rename from javax-servlets/src/test/java/com/baeldung/servlets/FormServletLiveTest.java rename to javax-servlets/src/test/java/com/baeldung/test/FormServletLiveTest.java diff --git a/javax-servlet-init-params/src/test/java/com/baeldung/test/UserServletUnitTest.java b/javax-servlets/src/test/java/com/baeldung/test/UserServletUnitTest.java similarity index 95% rename from javax-servlet-init-params/src/test/java/com/baeldung/test/UserServletUnitTest.java rename to javax-servlets/src/test/java/com/baeldung/test/UserServletUnitTest.java index ef3d877dd7..d4c93791d2 100644 --- a/javax-servlet-init-params/src/test/java/com/baeldung/test/UserServletUnitTest.java +++ b/javax-servlets/src/test/java/com/baeldung/test/UserServletUnitTest.java @@ -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"); + } } \ No newline at end of file