Skip to content

Commit ca43996

Browse files
author
devops
committed
Java Web Calculator
1 parent b69e006 commit ca43996

5 files changed

Lines changed: 154 additions & 0 deletions

File tree

pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.web.cal</groupId>
5+
<artifactId>WebAppCal</artifactId>
6+
<packaging>war</packaging>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<name>WebAppCal Maven Webapp</name>
9+
<url>http://maven.apache.org</url>
10+
<dependencies>
11+
<dependency>
12+
<groupId>junit</groupId>
13+
<artifactId>junit</artifactId>
14+
<version>4.8.1</version>
15+
<scope>test</scope>
16+
</dependency>
17+
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
18+
<dependency>
19+
<groupId>javax.servlet</groupId>
20+
<artifactId>servlet-api</artifactId>
21+
<version>2.5</version>
22+
</dependency>
23+
</dependencies>
24+
<build>
25+
<finalName>WebAppCal</finalName>
26+
</build>
27+
</project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package mypackage;
2+
3+
import java.io.*;
4+
import javax.servlet.*;
5+
import javax.servlet.http.*;
6+
7+
public class Calculator extends HttpServlet
8+
{
9+
public long addFucn(long first, long second){
10+
11+
return first+second;
12+
}
13+
14+
public long subFucn(long first, long second){
15+
16+
return second-first;
17+
}
18+
19+
public long mulFucn(long first, long second){
20+
21+
return first*second;
22+
}
23+
24+
25+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
26+
{
27+
try
28+
{
29+
response.setContentType("text/html");
30+
PrintWriter out= response.getWriter();
31+
int a1= Integer.parseInt(request.getParameter("n1"));
32+
int a2= Integer.parseInt(request.getParameter("n2"));
33+
34+
35+
36+
if(request.getParameter("r1")!=null)
37+
{
38+
out.println("<h1>Addition</h1>"+addFucn(a1, a2));
39+
}
40+
if(request.getParameter("r2")!=null)
41+
{
42+
out.println("<h1>Substraction</h1>"+subFucn(a1, a2));
43+
}
44+
if(request.getParameter("r3")!=null)
45+
{
46+
out.println("<h1>Multiplication</h1>"+mulFucn(a1, a2));
47+
}
48+
RequestDispatcher rd=request.getRequestDispatcher("/index.jsp");
49+
rd.include(request, response);
50+
}
51+
catch(Exception e)
52+
{
53+
54+
}
55+
}
56+
}

src/main/webapp/WEB-INF/web.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
3+
<display-name>Servlet</display-name>
4+
<servlet>
5+
<servlet-name>Servlet</servlet-name>
6+
<servlet-class>mypackage.Calculator</servlet-class>
7+
</servlet>
8+
<servlet-mapping>
9+
<servlet-name>Servlet</servlet-name>
10+
<url-pattern>/firstHomePage</url-pattern>
11+
</servlet-mapping>
12+
<welcome-file-list>
13+
<welcome-file>index.jsp</welcome-file>
14+
</welcome-file-list>
15+
</web-app>

src/main/webapp/index.jsp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2+
<html>
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
5+
<title>Calculator</title>
6+
</head>
7+
<body>
8+
<h1 style="text_align=center">Calculator</h1>
9+
<form action="firstHomePage" method="get">
10+
<label>first number:</label>
11+
<input type="text" name="n1" />
12+
<br />
13+
<label>Second number : </label>
14+
<input type="text" name="n2" />
15+
<br />
16+
<div>
17+
<label>
18+
<input type="radio" name="r1" value="add" />addition
19+
<br />
20+
</label>
21+
<input type="radio" name="r2" value="sub" />subtraction
22+
<br />
23+
<input type="radio" name="r3" value="mul" />multiplication
24+
<br />
25+
26+
</div>
27+
<input type="submit" value="submit" />
28+
</form>
29+
</body>
30+
</html>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package mypackage;
2+
3+
import org.junit.Test;
4+
import static org.hamcrest.CoreMatchers.is;
5+
import static org.junit.Assert.assertThat;
6+
7+
public class CalculatorTest {
8+
@Test
9+
public void twoAndThreeIsFive() throws Exception {
10+
final long result = new Calculator().addFucn(2, 3);
11+
assertThat(result, is(5L));
12+
}
13+
14+
@Test
15+
public void threeMinusTwoIsOne() throws Exception {
16+
final long result = new Calculator().subFucn(2, 3);
17+
assertThat(result, is(1L));
18+
}
19+
20+
@Test
21+
public void threeXThreeIsNine() throws Exception {
22+
final long result = new Calculator().mulFucn(3, 3);
23+
assertThat(result, is(9L));
24+
}
25+
26+
}

0 commit comments

Comments
 (0)