Merge pull request #867 from sbmaggarwal/master

BAEL-425 Merge it
This commit is contained in:
Marek Lewandowski 2016-12-06 11:39:48 +01:00 committed by GitHub
commit 628011bbfa
7 changed files with 159 additions and 0 deletions

5
javax-servlets/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
# Created by .ignore support plugin (hsz.mobi)
.idea
classes
target
*.iml

39
javax-servlets/pom.xml Normal file
View File

@ -0,0 +1,39 @@
<?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>${javax.servlet.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${org.apache.httpcomponents.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<javax.servlet.version>3.1.0</javax.servlet.version>
<junit.version>4.12</junit.version>
<org.apache.httpcomponents.version>4.5</org.apache.httpcomponents.version>
</properties>
</project>

View File

@ -0,0 +1,47 @@
package com.root;
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(name = "FormServlet", urlPatterns = "/calculateServlet")
public class FormServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String height = request.getParameter("height");
String weight = request.getParameter("weight");
try {
double bmi = calculateBMI(Double.parseDouble(weight), Double.parseDouble(height));
request.setAttribute("bmi", bmi);
response.setHeader("Test", "Success");
response.setHeader("BMI", String.valueOf(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);
}
}

View File

@ -0,0 +1,34 @@
package com.root;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class FormServletTest {
@Test
public void whenPostRequestUsingHttpClient_thenCorrect() throws Exception {
HttpClient client = new DefaultHttpClient();
HttpPost method = new HttpPost("http://localhost:8080/calculateServlet");
List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair("height", String.valueOf(2)));
nvps.add(new BasicNameValuePair("weight", String.valueOf(80)));
method.setEntity(new UrlEncodedFormEntity(nvps));
HttpResponse httpResponse = client.execute(method);
assertEquals("Success", httpResponse.getHeaders("Test")[0].getValue());
assertEquals("20.0", httpResponse.getHeaders("BMI")[0].getValue());
}
}

View File

@ -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>

View File

@ -0,0 +1,26 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Calculate BMI</title>
</head>
<body>
<form name="bmiForm" action="calculateServlet" method="POST">
<table>
<tr>
<td>Your Weight (kg) :</td>
<td><input type="text" name="weight"/></td>
</tr>
<tr>
<td>Your Height (m) :</td>
<td><input type="text" name="height"/></td>
</tr>
<th><input type="submit" value="Submit" name="find"/></th>
<th><input type="reset" value="Reset" name="reset" /></th>
</table>
<h2>${bmi}</h2>
</form>
</body>
</html>

View File

@ -48,6 +48,7 @@
<module>jackson</module>
<module>java-cassandra</module>
<module>javax-servlets</module>
<module>javaxval</module>
<module>jee7</module>
<module>jjwt</module>