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