In this article I would like to share with you a program that I wrote using Java Servlet that will compute the area and the circumference of a circle by accepting the radius size from the user.The program is written in a simple manner that can be useful for anyone interested in web programming using servlet in Java.
If you have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360.
Sample Program Output
Program Listing
area.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Area of a Circle in Servlet</title>
</head>
<style>
label{
display: inline-block;
float: left;
clear: left;
width: 185px;
text-align: left;
}
input {
display: inline-block;
float: left;
}
h3 {
font-family:arial;
color:blue;
}
</style>
</head>
<body bgcolor="lightgreen">
<form name='area_circle' action='http://localhost:8080/circle/circle' method='get'>
<div>
<font face="arial" size="3">
<h3> Area of a Circle in Servlet</h3> <br>
<label>Enter the radius </label>
<input type='numberic' placeholder='radius' name='radius' size='7' autofocus required><br>
<br>
<input type='submit' name='submit' value='Compute Radius' title='Click here to see the result.'>
<input type='reset' name='reset' value='Clear' title='Click here to clear text box.'>
</font> </div>
</body>
</html>
area_circle.java
package area_circle;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class area_circle extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException , ServletException
{
PrintWriter pr=res.getWriter();
res.setContentType("text/html");
try
{
double radius=Integer.parseInt(req.getParameter("radius"));
//Area = PI*radius*radius
double area = Math.PI * (radius * radius);
//Circumference = 2*PI*radius
double circumference= Math.PI * 2*radius;
pr.println("<body bgcolor='lightgreen'>");
pr.println("<font face='arial' size='3'> ");
pr.println("<h3> <font color='Blue'> Display Results </font> </h3>");
pr.println("<br>");
pr.println("The area of the circle is " + String.format("%.4f", area )+ ".<br>");
pr.println("The circumference of the circle is " + String.format("%.4f", circumference) + ".<br>");
pr.println("<br>");
pr.println("<h3><font color='Blue'> End Program </font> </h3>");
pr.println("</font>");
}
catch(Exception e)
{
pr.println("Invalid Input value please try again");
}
} }
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>circle</display-name>
<servlet>
<servlet-name>circle</servlet-name>
<servlet-class>area_circle.area_circle</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>circle</servlet-name>
<url-pattern>/circle</url-pattern>
</servlet-mapping>
</web-app>
No comments:
Post a Comment