001: /*
002: * SumServlet.java
003: *
004: * Created on 07 January 2005, 15:28
005: */
006:
007: package org.netbeans.test.servlets;
008:
009: import java.io.*;
010: import java.net.*;
011:
012: import javax.servlet.*;
013: import javax.servlet.http.*;
014:
015: /**
016: *
017: * @author Administrator
018: * @version
019: */
020: public class DivideServlet extends HttpServlet {
021:
022: /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
023: * @param request servlet request
024: * @param response servlet response
025: */
026: protected void processRequest(HttpServletRequest request,
027: HttpServletResponse response) throws ServletException,
028: IOException {
029: response.setContentType("text/html");
030: PrintWriter out = response.getWriter();
031:
032: out.println("<html>");
033: out.println("<head>");
034: out.println("<title>Servlet DIVIDE</title>");
035: out.println("</head>");
036: out.println("<body>");
037: out.println("<h1>Servlet DivideServlet at "
038: + request.getContextPath() + "</h1>");
039:
040: org.netbeans.test.freeformlib.Divider d = new org.netbeans.test.freeformlib.Divider();
041: try {
042: String attributeX = request.getParameter("x");
043: if (attributeX == null) {
044: attributeX = "";
045: }
046: d.setX(Double.parseDouble(attributeX));
047: } catch (NumberFormatException e) {
048: }
049: try {
050: String attributeY = request.getParameter("y");
051: if (attributeY == null) {
052: attributeY = "";
053: }
054: d.setY(Double.parseDouble(attributeY));
055: } catch (NumberFormatException e) {
056: }
057:
058: if (d.getY() == 0) {
059: out.println("<b>y</b> can't be 0!");
060: } else {
061: out.println("" + d.getX() + " / " + d.getY() + " = "
062: + d.getDivision());
063: }
064:
065: out.println("<br/>");
066: out.println("<a href=\"index.jsp\">Go back to index.jsp</a>");
067: out.println("</body>");
068: out.println("</html>");
069:
070: out.close();
071: }
072:
073: // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
074: /** Handles the HTTP <code>GET</code> method.
075: * @param request servlet request
076: * @param response servlet response
077: */
078: protected void doGet(HttpServletRequest request,
079: HttpServletResponse response) throws ServletException,
080: IOException {
081: processRequest(request, response);
082: }
083:
084: /** Handles the HTTP <code>POST</code> method.
085: * @param request servlet request
086: * @param response servlet response
087: */
088: protected void doPost(HttpServletRequest request,
089: HttpServletResponse response) throws ServletException,
090: IOException {
091: processRequest(request, response);
092: }
093:
094: /** Returns a short description of the servlet.
095: */
096: public String getServletInfo() {
097: return "Short description";
098: }
099: // </editor-fold>
100: }
|