KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > servlets > DivideServlet


1 /*
2  * SumServlet.java
3  *
4  * Created on 07 January 2005, 15:28
5  */

6
7 package org.netbeans.test.servlets;
8
9 import java.io.*;
10 import java.net.*;
11
12 import javax.servlet.*;
13 import javax.servlet.http.*;
14
15 /**
16  *
17  * @author Administrator
18  * @version
19  */

20 public class DivideServlet extends HttpServlet {
21     
22     /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
23      * @param request servlet request
24      * @param response servlet response
25      */

26     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
27     throws ServletException, IOException {
28         response.setContentType("text/html");
29         PrintWriter out = response.getWriter();
30         
31         out.println("<html>");
32         out.println("<head>");
33         out.println("<title>Servlet DIVIDE</title>");
34         out.println("</head>");
35         out.println("<body>");
36         out.println("<h1>Servlet DivideServlet at " + request.getContextPath () + "</h1>");
37         
38         org.netbeans.test.freeformlib.Divider d = new org.netbeans.test.freeformlib.Divider();
39         try {
40             String JavaDoc attributeX = request.getParameter("x");
41             if (attributeX == null) {
42                 attributeX = "";
43             }
44             d.setX(Double.parseDouble(attributeX));
45         } catch(NumberFormatException JavaDoc e) {
46         }
47         try {
48             String JavaDoc attributeY = request.getParameter("y");
49             if (attributeY == null) {
50                 attributeY = "";
51             }
52             d.setY(Double.parseDouble(attributeY));
53         } catch(NumberFormatException JavaDoc e) {
54         }
55         
56         if (d.getY() == 0) {
57             out.println("<b>y</b> can't be 0!");
58         } else {
59             out.println("" + d.getX() + " / " + d.getY() + " = " + d.getDivision());
60         }
61         
62         out.println("<br/>");
63         out.println("<a HREF=\"index.jsp\">Go back to index.jsp</a>");
64         out.println("</body>");
65         out.println("</html>");
66         
67         out.close();
68     }
69     
70     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
71
/** Handles the HTTP <code>GET</code> method.
72      * @param request servlet request
73      * @param response servlet response
74      */

75     protected void doGet(HttpServletRequest request, HttpServletResponse response)
76     throws ServletException, IOException {
77         processRequest(request, response);
78     }
79     
80     /** Handles the HTTP <code>POST</code> method.
81      * @param request servlet request
82      * @param response servlet response
83      */

84     protected void doPost(HttpServletRequest request, HttpServletResponse response)
85     throws ServletException, IOException {
86         processRequest(request, response);
87     }
88     
89     /** Returns a short description of the servlet.
90      */

91     public String JavaDoc getServletInfo() {
92         return "Short description";
93     }
94     // </editor-fold>
95
}
96
Popular Tags