Servlet(练习2)

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <form action="JiSanQi">
11 <input type="text" name="shu1">
12     <select name = "operator">  
13     <option value=+>+</option>  
14     <option value=->-</option>  
15     <option value=*>*</option>  
16     <option value=/>/</option>        
17     </select>
18 <input type="text" name="shu2">
19 <input type="submit" value="等于">
20 </form>
21 </body>
22 </html>
 1 package hanqi;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 
 9 public class JiSanQi extends HttpServlet
10 {
11     private static final long serialVersionUID = 1L;
12     public JiSanQi()
13     {
14         super();
15     }
16     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
17     {
18         int a=Integer.parseInt(request.getParameter("shu1"));
19         int b=Integer.parseInt(request.getParameter("shu2"));
20         String c=request.getParameter("operator");
21         if(c.equals("+"))
22         {
23             response.getWriter().write(a+b+"");
24         }
25         else if(c.equals("-"))
26         {
27             response.getWriter().write(a-b+"");
28         }
29         else if(c.equals("*"))
30         {
31             response.getWriter().write(a*b+"");
32         }
33         else if(c.equals("/"))
34         {
35             response.getWriter().write(a/b+"");
36         }
37         
38     }
39     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
40     {
41         
42         doGet(request, response);
43     }
44 }

题目:简易Servlet计算器

posted @ 2016-07-01 22:27  明天会更好!!!!  阅读(177)  评论(0)    收藏  举报