A Simple Servlet: Java Source Code
import javax.servlet.http.*;
public class PizzaSize extends HttpServlet {
// doGet method is called when HTML form uses GET method
public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// set the "content type" header of the response
res.setContentType("text/html");
// get the response's PrintWriter to return text to the client
PrintWriter out = res.getWriter();
// get the HTML form data
String squareSize = req.getParameter("SQUARE");
String circleSize = req.getParameter("CIRCLE");
// business logic (how big are the pizzas?)
Double circum = new Double(circleSize);
double circleArea=PI * (circum.doubleValue()/2) * (circum.doubleValue()/2);
Double length = new Double(squareSize);
double squareArea = length.doubleValue() * length.doubleValue();
// write the response HTML page
out.println("<html><head><title>Pizza Size Servlet</title></head><body>");
out.println("A " + circleSize +
" inch round pizza has an area of " + circleArea + " square inches<p>");
out.println("A " + squareSize +
" inch square pizza has an area of " + squareArea + " square inches");
out.println("</body></html>");
import javax.servlet.http.*;
public class PizzaSize extends HttpServlet {
public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
parameters = req.getParameterValues("SQUARE");
if (parameters != null) {
squareSize = parameters[0];
parameters = req.getParameterValues("CIRCLE");
if (parameters != null) {
circleSize = parameters[0];
Double circum = new Double(circleSize);
double circleArea = PI * (circum.doubleValue() / 2) *
(circum.doubleValue() / 2);
Double length = new Double(squareSize);
double squareArea = length.doubleValue() * length.doubleValue();
out.println("<head><title>Pizza Size Servlet</title></head>");
out.println("A " + circleSize +
" inch round pizza has an area of " + circleArea +
out.println("A " + squareSize +
" inch square pizza has an area of " + squareArea +
out.println("</body></html>");
Author | Title | Track | Home