1 package edu.fjnu.controller;2
3 import java.io.IOException;4 import java.io.PrintWriter;5
6 import javax.servlet.ServletException;7 import javax.servlet.http.HttpServlet;8 import javax.servlet.http.HttpServletRequest;9 import javax.servlet.http.HttpServletResponse;10
11 public class MyCalcServlet extends HttpServlet {12
13 /**14 * Constructor of the object.15 */16 public MyCalcServlet() {17 super();18 }19
20 /**21 * The doGet method of the servlet.
22 *23 * This method is called when a form has its tag value method equals to get.24 *25 * @param request the request send by the client to the server26 * @param response the response send by the server to the client27 * @throws ServletException if an error occurred28 * @throws IOException if an error occurred29 */30 public void doGet(HttpServletRequest request, HttpServletResponse response)31 throws ServletException, IOException {32
33 int begin = Integer.parseInt(request.getParameter("begin"));34 int end = Integer.parseInt(request.getParameter("end"));35
36 //System.out.println("calc sope:"+begin+"-"+end);37
38 long sum = 0L;39 for(int i=begin;i<=end;i++){40 sum += i;41 }42
43 response.setContentType("text/html");44 PrintWriter out= response.getWriter();45 out.println(""-//W3C//DTD HTML 4.01 Transitional//EN\">");46 out.println("");47 out.println("
A Servlet");48 out.println(" ");49 out.print(begin+"+...+"+end+"="+sum);50 out.println(" ");51 out.println("");52 out.flush();53 out.close();54 }5556 /**57 * The doPost method of the servlet.
58 *59 * This method is called when a form has its tag value method equals to post.60 *61 * @param request the request send by the client to the server62 * @param response the response send by the server to the client63 * @throws ServletException if an error occurred64 * @throws IOException if an error occurred65 */66 public void doPost(HttpServletRequest request, HttpServletResponse response)67 throws ServletException, IOException {68
69 response.setContentType("text/html");70 PrintWriter out = response.getWriter();71 out.println(""-//W3C//DTD HTML 4.01 Transitional//EN\">");72 out.println("");73 out.println("
A Servlet");74 out.println(" ");75 out.print(" This is ");76 out.print(this.getClass());77 out.println(", using the POST method");78 out.println(" ");79 out.println("");80 out.flush();81 out.close();82 }8384 }