Bael-1601: move servlet redirect vs forward to spring-boot-bootstrap module (#3792)
* move servlet redirect vs forward to this module * Added Unit test for servlets * Cleaned Unit tests * Refactored styling of the Java files.
This commit is contained in:
parent
a4a6577a4a
commit
d02147c7ee
|
@ -79,6 +79,12 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>4.0.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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"));
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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"));
|
||||
}
|
||||
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue