diff --git a/spring-boot-bootstrap/pom.xml b/spring-boot-bootstrap/pom.xml index 21c0ea60a8..eb97d6d426 100644 --- a/spring-boot-bootstrap/pom.xml +++ b/spring-boot-bootstrap/pom.xml @@ -79,6 +79,12 @@ test + + javax.servlet + javax.servlet-api + 4.0.0 + + diff --git a/spring-boot-bootstrap/src/main/java/org/baeldung/Application.java b/spring-boot-bootstrap/src/main/java/org/baeldung/Application.java index f7e7bb0347..ba1b444e44 100644 --- a/spring-boot-bootstrap/src/main/java/org/baeldung/Application.java +++ b/spring-boot-bootstrap/src/main/java/org/baeldung/Application.java @@ -3,9 +3,11 @@ package org.baeldung; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +@ServletComponentScan @SpringBootApplication @ComponentScan("org.baeldung") @EnableJpaRepositories("org.baeldung.persistence.repo") diff --git a/spring-boot-bootstrap/src/main/java/org/baeldung/ForwardedServlet.java b/spring-boot-bootstrap/src/main/java/org/baeldung/ForwardedServlet.java new file mode 100644 index 0000000000..e42da22852 --- /dev/null +++ b/spring-boot-bootstrap/src/main/java/org/baeldung/ForwardedServlet.java @@ -0,0 +1,20 @@ +package org.baeldung; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; + +@WebServlet("/forwarded") +public class ForwardedServlet extends HttpServlet { + + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + PrintWriter out = resp.getWriter(); + out.write("In forwarded servlet page."); + out.write("\nWelcome:" + req.getParameter("name")); + } + +} diff --git a/spring-boot-bootstrap/src/main/java/org/baeldung/HelloServlet.java b/spring-boot-bootstrap/src/main/java/org/baeldung/HelloServlet.java new file mode 100644 index 0000000000..63673143d6 --- /dev/null +++ b/spring-boot-bootstrap/src/main/java/org/baeldung/HelloServlet.java @@ -0,0 +1,19 @@ +package org.baeldung; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +@WebServlet("/hello") +public class HelloServlet extends HttpServlet { + + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + RequestDispatcher dispatcher = req.getRequestDispatcher("/forwarded"); + dispatcher.forward(req, resp); + } + +} diff --git a/spring-boot-bootstrap/src/main/java/org/baeldung/RedirectedServlet.java b/spring-boot-bootstrap/src/main/java/org/baeldung/RedirectedServlet.java new file mode 100644 index 0000000000..6d4b1ebce9 --- /dev/null +++ b/spring-boot-bootstrap/src/main/java/org/baeldung/RedirectedServlet.java @@ -0,0 +1,20 @@ +package org.baeldung; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; + +@WebServlet("/redirected") +public class RedirectedServlet extends HttpServlet { + + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + PrintWriter out = resp.getWriter(); + out.write("In redirected servlet page."); + out.write("\nWelcome:" + req.getParameter("name")); + } + +} diff --git a/spring-boot-bootstrap/src/main/java/org/baeldung/WelcomeServlet.java b/spring-boot-bootstrap/src/main/java/org/baeldung/WelcomeServlet.java new file mode 100644 index 0000000000..b4a51fc4dc --- /dev/null +++ b/spring-boot-bootstrap/src/main/java/org/baeldung/WelcomeServlet.java @@ -0,0 +1,18 @@ +package org.baeldung; + + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +@WebServlet("/welcome") +public class WelcomeServlet extends HttpServlet { + + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.sendRedirect(req.getContextPath() + "/redirected"); + } + +} diff --git a/spring-boot-bootstrap/src/test/java/org/baeldung/HelloServletTest.java b/spring-boot-bootstrap/src/test/java/org/baeldung/HelloServletTest.java new file mode 100644 index 0000000000..93e52cf0be --- /dev/null +++ b/spring-boot-bootstrap/src/test/java/org/baeldung/HelloServletTest.java @@ -0,0 +1,25 @@ +package org.baeldung; + +import org.junit.Test; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; + +import javax.servlet.ServletException; +import java.io.IOException; + +import static org.junit.Assert.assertEquals; + +public class HelloServletTest { + @Test + public void whenRequested_thenForwardToCorrectUrl() throws ServletException, IOException { + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hello"); + request.addParameter("name", "Dennis"); + MockHttpServletResponse response = new MockHttpServletResponse(); + HelloServlet servlet = new HelloServlet(); + + servlet.doGet(request, response); + + assertEquals("/forwarded", response.getForwardedUrl()); + assertEquals(200, response.getStatus()); + } +} diff --git a/spring-boot-bootstrap/src/test/java/org/baeldung/WelcomeServletTest.java b/spring-boot-bootstrap/src/test/java/org/baeldung/WelcomeServletTest.java new file mode 100644 index 0000000000..a52b2a5c01 --- /dev/null +++ b/spring-boot-bootstrap/src/test/java/org/baeldung/WelcomeServletTest.java @@ -0,0 +1,25 @@ +package org.baeldung; + +import org.junit.Test; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; + +import javax.servlet.ServletException; +import java.io.IOException; + +import static org.junit.Assert.assertEquals; + +public class WelcomeServletTest { + @Test + public void whenRequested_thenRedirectedToCorrectUrl() throws ServletException, IOException { + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome"); + request.addParameter("name", "Dennis"); + WelcomeServlet servlet = new WelcomeServlet(); + MockHttpServletResponse response = new MockHttpServletResponse(); + + servlet.doGet(request, response); + + assertEquals("/redirected", response.getRedirectedUrl()); + assertEquals(302, response.getStatus()); + } +}