Added BAEL-425 Intro to Servets module named javax-servlets
This commit is contained in:
parent
6cca052251
commit
65c5535c7d
|
@ -0,0 +1,34 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.root</groupId>
|
||||
<artifactId>ServletmavenExample</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-all</artifactId>
|
||||
<version>1.10.19</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
|
||||
version="3.1">
|
||||
|
||||
</web-app>
|
|
@ -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" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>Form</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<form action="informationServlet" method="POST">
|
||||
|
||||
Name:<input type="text" name="userName"/><br/><br/>
|
||||
Planet:
|
||||
<select name="userPlanet">
|
||||
<option>Mercury</option>
|
||||
<option>Venus</option>
|
||||
<option>Mars</option>
|
||||
</select>
|
||||
<br/><br/>
|
||||
|
||||
<input type="submit" value="Submit"/>
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue