Skip to content

Commit 628011b

Browse files
authored
Merge pull request eugenp#867 from sbmaggarwal/master
BAEL-425 Merge it
2 parents ee95f0c + e903968 commit 628011b

7 files changed

Lines changed: 159 additions & 0 deletions

File tree

‎javax-servlets/.gitignore‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
.idea
3+
classes
4+
target
5+
*.iml

‎javax-servlets/pom.xml‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.root</groupId>
8+
<artifactId>ServletmavenExample</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
13+
<dependency>
14+
<groupId>javax.servlet</groupId>
15+
<artifactId>javax.servlet-api</artifactId>
16+
<version>${javax.servlet.version}</version>
17+
</dependency>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>${junit.version}</version>
22+
<scope>test</scope>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.apache.httpcomponents</groupId>
26+
<artifactId>httpclient</artifactId>
27+
<version>${org.apache.httpcomponents.version}</version>
28+
<scope>test</scope>
29+
</dependency>
30+
31+
</dependencies>
32+
33+
<properties>
34+
<javax.servlet.version>3.1.0</javax.servlet.version>
35+
<junit.version>4.12</junit.version>
36+
<org.apache.httpcomponents.version>4.5</org.apache.httpcomponents.version>
37+
</properties>
38+
39+
</project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.root;
2+
3+
import javax.servlet.RequestDispatcher;
4+
import javax.servlet.ServletException;
5+
import javax.servlet.annotation.WebServlet;
6+
import javax.servlet.http.HttpServlet;
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpServletResponse;
9+
import java.io.IOException;
10+
11+
@WebServlet(name = "FormServlet", urlPatterns = "/calculateServlet")
12+
public class FormServlet extends HttpServlet {
13+
14+
@Override
15+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
16+
throws ServletException, IOException {
17+
18+
String height = request.getParameter("height");
19+
String weight = request.getParameter("weight");
20+
21+
try {
22+
double bmi = calculateBMI(Double.parseDouble(weight), Double.parseDouble(height));
23+
24+
request.setAttribute("bmi", bmi);
25+
response.setHeader("Test", "Success");
26+
response.setHeader("BMI", String.valueOf(bmi));
27+
28+
RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
29+
dispatcher.forward(request, response);
30+
} catch (Exception e) {
31+
32+
response.sendRedirect("index.jsp");
33+
}
34+
}
35+
36+
@Override
37+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
38+
throws ServletException, IOException {
39+
40+
// do something else here
41+
}
42+
43+
private Double calculateBMI(Double weight, Double height) {
44+
45+
return weight / (height * height);
46+
}
47+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.root;
2+
3+
import org.apache.http.HttpResponse;
4+
import org.apache.http.client.HttpClient;
5+
import org.apache.http.client.entity.UrlEncodedFormEntity;
6+
import org.apache.http.client.methods.HttpPost;
7+
import org.apache.http.impl.client.DefaultHttpClient;
8+
import org.apache.http.message.BasicNameValuePair;
9+
import org.junit.Test;
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
import static org.junit.Assert.assertEquals;
15+
16+
public class FormServletTest {
17+
18+
@Test
19+
public void whenPostRequestUsingHttpClient_thenCorrect() throws Exception {
20+
21+
HttpClient client = new DefaultHttpClient();
22+
HttpPost method = new HttpPost("http://localhost:8080/calculateServlet");
23+
24+
List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
25+
nvps.add(new BasicNameValuePair("height", String.valueOf(2)));
26+
nvps.add(new BasicNameValuePair("weight", String.valueOf(80)));
27+
28+
method.setEntity(new UrlEncodedFormEntity(nvps));
29+
HttpResponse httpResponse = client.execute(method);
30+
31+
assertEquals("Success", httpResponse.getHeaders("Test")[0].getValue());
32+
assertEquals("20.0", httpResponse.getHeaders("BMI")[0].getValue());
33+
}
34+
}

‎javax-servlets/web/WEB-INF/web.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
5+
version="3.1">
6+
7+
</web-app>

‎javax-servlets/web/index.jsp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
2+
<html>
3+
<head>
4+
<title>Calculate BMI</title>
5+
</head>
6+
<body>
7+
8+
<form name="bmiForm" action="calculateServlet" method="POST">
9+
10+
<table>
11+
<tr>
12+
<td>Your Weight (kg) :</td>
13+
<td><input type="text" name="weight"/></td>
14+
</tr>
15+
<tr>
16+
<td>Your Height (m) :</td>
17+
<td><input type="text" name="height"/></td>
18+
</tr>
19+
<th><input type="submit" value="Submit" name="find"/></th>
20+
<th><input type="reset" value="Reset" name="reset" /></th>
21+
</table>
22+
<h2>${bmi}</h2>
23+
</form>
24+
25+
</body>
26+
</html>

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
<module>jackson</module>
5050
<module>java-cassandra</module>
51+
<module>javax-servlets</module>
5152
<module>javaxval</module>
5253
<module>jee7</module>
5354
<module>jjwt</module>

0 commit comments

Comments
 (0)