From 65c5535c7d6ef788d819b8c13ce03528be0b2def Mon Sep 17 00:00:00 2001 From: Shubham Aggarwal Date: Sun, 27 Nov 2016 11:42:32 +0530 Subject: [PATCH 1/3] Added BAEL-425 Intro to Servets module named javax-servlets --- javax-servlets/pom.xml | 34 +++++++++++++++++++ .../src/main/java/com/root/FormServlet.java | 19 +++++++++++ .../test/java/com/root/FormServletTest.java | 34 +++++++++++++++++++ javax-servlets/web/WEB-INF/web.xml | 7 ++++ javax-servlets/web/index.jsp | 30 ++++++++++++++++ pom.xml | 1 + 6 files changed, 125 insertions(+) create mode 100644 javax-servlets/pom.xml create mode 100644 javax-servlets/src/main/java/com/root/FormServlet.java create mode 100644 javax-servlets/src/test/java/com/root/FormServletTest.java create mode 100644 javax-servlets/web/WEB-INF/web.xml create mode 100644 javax-servlets/web/index.jsp diff --git a/javax-servlets/pom.xml b/javax-servlets/pom.xml new file mode 100644 index 0000000000..2d2e3a4b8a --- /dev/null +++ b/javax-servlets/pom.xml @@ -0,0 +1,34 @@ + + + 4.0.0 + + com.root + ServletmavenExample + 1.0-SNAPSHOT + + + + + javax.servlet + javax.servlet-api + 3.1.0 + + + junit + junit + 4.12 + test + + + org.mockito + mockito-all + 1.10.19 + test + + + + + + \ No newline at end of file diff --git a/javax-servlets/src/main/java/com/root/FormServlet.java b/javax-servlets/src/main/java/com/root/FormServlet.java new file mode 100644 index 0000000000..c8e8212f30 --- /dev/null +++ b/javax-servlets/src/main/java/com/root/FormServlet.java @@ -0,0 +1,19 @@ +package com.root; + +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(name = "FormServlet", urlPatterns = "/informationServlet") +public class FormServlet extends HttpServlet { + + protected void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + String userName = request.getParameter("userName"); + String userPlanet = request.getParameter("userPlanet"); + } +} diff --git a/javax-servlets/src/test/java/com/root/FormServletTest.java b/javax-servlets/src/test/java/com/root/FormServletTest.java new file mode 100644 index 0000000000..12545317c6 --- /dev/null +++ b/javax-servlets/src/test/java/com/root/FormServletTest.java @@ -0,0 +1,34 @@ +package com.root; + +import org.junit.Test; +import org.mockito.Mockito; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.PrintWriter; +import java.io.StringWriter; + +import static org.junit.Assert.*; + +/** + * Created by shubham on 27/11/16. + */ +public class FormServletTest extends Mockito { + + @Test + public void testFormServlet() throws Exception { + + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + + when(request.getParameter("userName")).thenReturn("baeldung"); + when(request.getParameter("userPlanet")).thenReturn("Mars"); + + PrintWriter writer = new PrintWriter(new StringWriter()); + when(response.getWriter()).thenReturn(writer); + + new FormServlet().doPost(request, response); + + assertTrue(request.getParameter("userName").contains("baeldung")); + } +} diff --git a/javax-servlets/web/WEB-INF/web.xml b/javax-servlets/web/WEB-INF/web.xml new file mode 100644 index 0000000000..66934d8fd3 --- /dev/null +++ b/javax-servlets/web/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/javax-servlets/web/index.jsp b/javax-servlets/web/index.jsp new file mode 100644 index 0000000000..4054b1acf0 --- /dev/null +++ b/javax-servlets/web/index.jsp @@ -0,0 +1,30 @@ +<%-- + Created by IntelliJ IDEA. + User: shubham + Date: 26/11/16 + Time: 8:52 PM + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Form + + + +
+ + Name:

+ Planet: + +

+ + +
+ + + diff --git a/pom.xml b/pom.xml index 77cf615a98..eacedf1e6a 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,7 @@ jackson java-cassandra + javax-servlets javaxval jee7 jjwt From 115f70c9173590414c8a5dca946a758aa0d66e49 Mon Sep 17 00:00:00 2001 From: Shubham Aggarwal Date: Mon, 28 Nov 2016 09:36:42 +0530 Subject: [PATCH 2/3] RequestDispatcher added to servlet --- .../META-INF/MANIFEST.MF | 5 +++ .../WEB-INF/web.xml | 7 ++++ .../index.jsp | 26 ++++++++++++++ .../src/main/java/com/root/FormServlet.java | 31 +++++++++++++++-- javax-servlets/web/index.jsp | 34 ++++++++----------- 5 files changed, 81 insertions(+), 22 deletions(-) create mode 100644 javax-servlets/classes/artifacts/ServletmavenExample_war_exploded/META-INF/MANIFEST.MF create mode 100644 javax-servlets/classes/artifacts/ServletmavenExample_war_exploded/WEB-INF/web.xml create mode 100644 javax-servlets/classes/artifacts/ServletmavenExample_war_exploded/index.jsp diff --git a/javax-servlets/classes/artifacts/ServletmavenExample_war_exploded/META-INF/MANIFEST.MF b/javax-servlets/classes/artifacts/ServletmavenExample_war_exploded/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..e8d1783f02 --- /dev/null +++ b/javax-servlets/classes/artifacts/ServletmavenExample_war_exploded/META-INF/MANIFEST.MF @@ -0,0 +1,5 @@ +Manifest-Version: 1.0 +Built-By: shubham +Created-By: IntelliJ IDEA +Build-Jdk: 1.8.0_91 + diff --git a/javax-servlets/classes/artifacts/ServletmavenExample_war_exploded/WEB-INF/web.xml b/javax-servlets/classes/artifacts/ServletmavenExample_war_exploded/WEB-INF/web.xml new file mode 100644 index 0000000000..66934d8fd3 --- /dev/null +++ b/javax-servlets/classes/artifacts/ServletmavenExample_war_exploded/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/javax-servlets/classes/artifacts/ServletmavenExample_war_exploded/index.jsp b/javax-servlets/classes/artifacts/ServletmavenExample_war_exploded/index.jsp new file mode 100644 index 0000000000..5a2e018a41 --- /dev/null +++ b/javax-servlets/classes/artifacts/ServletmavenExample_war_exploded/index.jsp @@ -0,0 +1,26 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Calculate BMI + + + +
+ + + + + + + + + + + + +
Your Weight (kg) :
Your Height (m) :
+

${bmi}

+
+ + + diff --git a/javax-servlets/src/main/java/com/root/FormServlet.java b/javax-servlets/src/main/java/com/root/FormServlet.java index c8e8212f30..5c11a586d3 100644 --- a/javax-servlets/src/main/java/com/root/FormServlet.java +++ b/javax-servlets/src/main/java/com/root/FormServlet.java @@ -1,5 +1,6 @@ package com.root; +import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; @@ -7,13 +8,37 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -@WebServlet(name = "FormServlet", urlPatterns = "/informationServlet") +@WebServlet(name = "FormServlet", urlPatterns = "/calculateServlet") public class FormServlet extends HttpServlet { + @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - String userName = request.getParameter("userName"); - String userPlanet = request.getParameter("userPlanet"); + String height = request.getParameter("height"); + String weight = request.getParameter("weight"); + + try { + Double bmi = calculateBMI(Double.parseDouble(weight), Double.parseDouble(height)); + + request.setAttribute("bmi", bmi); + RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp"); + dispatcher.forward(request, response); + } catch (Exception e) { + + response.sendRedirect("index.jsp"); + } + } + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + // do something else here + } + + private Double calculateBMI(Double weight, Double height) { + + return weight / (height * height); } } diff --git a/javax-servlets/web/index.jsp b/javax-servlets/web/index.jsp index 4054b1acf0..5a2e018a41 100644 --- a/javax-servlets/web/index.jsp +++ b/javax-servlets/web/index.jsp @@ -1,29 +1,25 @@ -<%-- - Created by IntelliJ IDEA. - User: shubham - Date: 26/11/16 - Time: 8:52 PM - To change this template use File | Settings | File Templates. ---%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> - Form + Calculate BMI -
+ - Name:

- Planet: - -

- - + + + + + + + + + + + +
Your Weight (kg) :
Your Height (m) :
+

${bmi}

From de8038fe4d2b9ed8e4cd51cbbb2589c9533605f6 Mon Sep 17 00:00:00 2001 From: Shubham Aggarwal Date: Mon, 28 Nov 2016 11:03:13 +0530 Subject: [PATCH 3/3] Test made using Mockito and JUnit --- .../src/main/java/com/root/FormServlet.java | 17 +++---- .../test/java/com/root/FormServletTest.java | 45 +++++++++++++------ 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/javax-servlets/src/main/java/com/root/FormServlet.java b/javax-servlets/src/main/java/com/root/FormServlet.java index 5c11a586d3..be83cece8f 100644 --- a/javax-servlets/src/main/java/com/root/FormServlet.java +++ b/javax-servlets/src/main/java/com/root/FormServlet.java @@ -7,6 +7,7 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.io.PrintWriter; @WebServlet(name = "FormServlet", urlPatterns = "/calculateServlet") public class FormServlet extends HttpServlet { @@ -19,9 +20,12 @@ public class FormServlet extends HttpServlet { String weight = request.getParameter("weight"); try { - Double bmi = calculateBMI(Double.parseDouble(weight), Double.parseDouble(height)); - + double bmi = calculateBMI(Double.parseDouble(weight), Double.parseDouble(height)); request.setAttribute("bmi", bmi); + + PrintWriter out= response.getWriter(); + out.write(String.valueOf(bmi)); + RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp"); dispatcher.forward(request, response); } catch (Exception e) { @@ -30,15 +34,8 @@ public class FormServlet extends HttpServlet { } } - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - // do something else here - } - private Double calculateBMI(Double weight, Double height) { return weight / (height * height); } -} +} \ No newline at end of file diff --git a/javax-servlets/src/test/java/com/root/FormServletTest.java b/javax-servlets/src/test/java/com/root/FormServletTest.java index 12545317c6..2d6ce22db5 100644 --- a/javax-servlets/src/test/java/com/root/FormServletTest.java +++ b/javax-servlets/src/test/java/com/root/FormServletTest.java @@ -1,34 +1,51 @@ package com.root; +import org.junit.Before; import org.junit.Test; -import org.mockito.Mockito; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import javax.servlet.RequestDispatcher; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.PrintWriter; import java.io.StringWriter; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; -/** - * Created by shubham on 27/11/16. - */ -public class FormServletTest extends Mockito { +public class FormServletTest { + + @Mock + HttpServletRequest request; + + @Mock + HttpServletResponse response; + + @Mock + RequestDispatcher requestDispatcher; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } @Test public void testFormServlet() throws Exception { - HttpServletRequest request = mock(HttpServletRequest.class); - HttpServletResponse response = mock(HttpServletResponse.class); + when(request.getParameter("height")).thenReturn("2"); + when(request.getParameter("weight")).thenReturn("80"); - when(request.getParameter("userName")).thenReturn("baeldung"); - when(request.getParameter("userPlanet")).thenReturn("Mars"); - - PrintWriter writer = new PrintWriter(new StringWriter()); - when(response.getWriter()).thenReturn(writer); + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + when(response.getWriter()).thenReturn(pw); new FormServlet().doPost(request, response); - assertTrue(request.getParameter("userName").contains("baeldung")); + verify(request).setAttribute("bmi", 20.0); + + String result = sw.getBuffer().toString().trim(); + assertEquals("20.0", result); } }