1 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 20 public class DivideServlet extends HttpServlet { 21 22 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 attributeX = request.getParameter("x"); 41 if (attributeX == null) { 42 attributeX = ""; 43 } 44 d.setX(Double.parseDouble(attributeX)); 45 } catch(NumberFormatException e) { 46 } 47 try { 48 String attributeY = request.getParameter("y"); 49 if (attributeY == null) { 50 attributeY = ""; 51 } 52 d.setY(Double.parseDouble(attributeY)); 53 } catch(NumberFormatException 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 75 protected void doGet(HttpServletRequest request, HttpServletResponse response) 76 throws ServletException, IOException { 77 processRequest(request, response); 78 } 79 80 84 protected void doPost(HttpServletRequest request, HttpServletResponse response) 85 throws ServletException, IOException { 86 processRequest(request, response); 87 } 88 89 91 public String getServletInfo() { 92 return "Short description"; 93 } 94 } 96 | Popular Tags |