1 package com.fjnu.works.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 CookerServlet extends HttpServlet {
12 
13     /**
14      * Constructor of the object.
15      */
16     public CookerServlet() {
17         super();
18     }
19 
20     /**
21      * The doGet method of the servlet. <br>
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 server
26      * @param response the response send by the server to the client
27      * @throws ServletException if an error occurred
28      * @throws IOException if an error occurred
29      */
30     public void doGet(HttpServletRequest request, HttpServletResponse response)
31             throws ServletException, IOException {
32         String menu = (String)request.getAttribute("menu");
33         
34         System.out.println("cooker:刚休息一会,又要开始做菜了,这次做的是"+menu+",......好了!");
35         
36         request.removeAttribute("menu");
37         request.setAttribute("dinner","已经做好的"+menu);
38         request.getRequestDispatcher("sender").forward(request, response);
39         
40     }
41 
42     /**
43      * The doPost method of the servlet. <br>
44      *
45      * This method is called when a form has its tag value method equals to post.
46      * 
47      * @param request the request send by the client to the server
48      * @param response the response send by the server to the client
49      * @throws ServletException if an error occurred
50      * @throws IOException if an error occurred
51      */
52     public void doPost(HttpServletRequest request, HttpServletResponse response)
53             throws ServletException, IOException {
54         doGet(request, response);
55     }
56 
57 }
CookerServlet.java
 1 package com.fjnu.works.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 SenderServlet extends HttpServlet {
12 
13     /**
14      * Constructor of the object.
15      */
16     public SenderServlet() {
17         super();
18     }
19 
20     /**
21      * The doGet method of the servlet. <br>
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 server
26      * @param response the response send by the server to the client
27      * @throws ServletException if an error occurred
28      * @throws IOException if an error occurred
29      */
30     public void doGet(HttpServletRequest request, HttpServletResponse response)
31             throws ServletException, IOException {
32 
33         String dinner = (String)request.getAttribute("dinner");
34         request.removeAttribute("dinner");
35         
36         System.out.println("sender:刚打开QQ,又要送菜了,这次送的是"+dinner);
37         
38         response.setContentType("text/html");
39         response.setCharacterEncoding("utf-8");
40         PrintWriter out = response.getWriter();
41         out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
42         out.println("<HTML>");
43         out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
44         out.println("  <BODY>");
45         out.println("您好,您点的菜"+dinner+"已经好了,请慢用!");
46         out.println("  </BODY>");
47         out.println("</HTML>");
48         out.flush();
49         out.close();
50     }
51 
52     /**
53      * The doPost method of the servlet. <br>
54      *
55      * This method is called when a form has its tag value method equals to post.
56      * 
57      * @param request the request send by the client to the server
58      * @param response the response send by the server to the client
59      * @throws ServletException if an error occurred
60      * @throws IOException if an error occurred
61      */
62     public void doPost(HttpServletRequest request, HttpServletResponse response)
63             throws ServletException, IOException {
64 
65         doGet(request, response);
66     }
67 
68 }
SenderServlet.java
 1 package com.fjnu.works.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 SimpleCookerServlet extends HttpServlet {
12 
13     /**
14      * Constructor of the object.
15      */
16     public SimpleCookerServlet() {
17         super();
18     }
19 
20     /**
21      * The doGet method of the servlet. <br>
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 server
26      * @param response the response send by the server to the client
27      * @throws ServletException if an error occurred
28      * @throws IOException if an error occurred
29      */
30     public void doGet(HttpServletRequest request, HttpServletResponse response)
31             throws ServletException, IOException {
32         String menu = (String)request.getAttribute("menu");
33         
34         System.out.println("simple cooler:刚来2天实习,做点简单的活,现在要做的是"+menu+"......已经做好了!");
35         
36         request.removeAttribute("menu");
37         request.setAttribute("dinner", "已经做好的"+menu);
38         
39     }
40 
41     /**
42      * The doPost method of the servlet. <br>
43      *
44      * This method is called when a form has its tag value method equals to post.
45      * 
46      * @param request the request send by the client to the server
47      * @param response the response send by the server to the client
48      * @throws ServletException if an error occurred
49      * @throws IOException if an error occurred
50      */
51     public void doPost(HttpServletRequest request, HttpServletResponse response)
52             throws ServletException, IOException {
53         doGet(request, response);
54     }
55 
56 }
SimpleCookerServlet.java
 1 package com.fjnu.works.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 WaiterServlet extends HttpServlet {
12 
13     /**
14      * Constructor of the object.
15      */
16     public WaiterServlet() {
17         super();
18     }
19 
20     /**
21      * The doGet method of the servlet. <br>
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 server
26      * @param response the response send by the server to the client
27      * @throws ServletException if an error occurred
28      * @throws IOException if an error occurred
29      */
30     public void doGet(HttpServletRequest request, HttpServletResponse response)
31             throws ServletException, IOException {
32         
33         request.setCharacterEncoding("utf-8");
34         String dinner = request.getParameter("dinner");
35         
36         if(dinner.equals("油条")||dinner.equals("烧饼")){
37             
38             System.out.println("waiter:收到顾客点菜,点的是"+dinner+",我去小厨师那里拿!");
39             request.setAttribute("menu",dinner);
40             
41             request.getRequestDispatcher("simpleCooker").include(request, response);
42             
43             dinner = (String)request.getAttribute("dinner");
44             System.out.println("waiter:收到了小厨师做的"+dinner+",我去拿给客人!");
45 
46             response.setContentType("text/html");
47             response.setCharacterEncoding("utf-8");
48             PrintWriter out = response.getWriter();
49             out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
50             out.println("<HTML>");
51             out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
52             out.println("  <BODY>");
53             out.println("您好,您点的小菜"+dinner+",请慢用!");
54             out.println("  </BODY>");
55             out.println("</HTML>");
56             out.flush();
57             out.close();
58             
59         }
60         else{
61             
62         System.out.println("waiter:收到顾客点菜,点的是"+dinner+",我去通知厨师做菜!");
63 
64         request.setAttribute("menu", dinner);
65         request.getRequestDispatcher("cooker").forward(request, response); 
66     }
67         }
68     /**
69      * The doPost method of the servlet. <br>
70      *
71      * This method is called when a form has its tag value method equals to post.
72      * 
73      * @param request the request send by the client to the server
74      * @param response the response send by the server to the client
75      * @throws ServletException if an error occurred
76      * @throws IOException if an error occurred
77      */
78     public void doPost(HttpServletRequest request, HttpServletResponse response)
79             throws ServletException, IOException {
80         doGet(request, response);
81     }
82 
83 }
WaiterServlet.java
web.xml
 1 <!DOCTYPE html>
 2 <html>
 3   <head>
 4     <title>input_dinner.html</title>
 5     <meta charset="utf-8">
 6     <meta name="keywords" content="keyword1,keyword2,keyword3">
 7     <meta name="description" content="this is my page">
 8     <meta name="content-type" content="text/html; charset=UTF-8">
 9     
10     <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
11 <style>
12     div{
13         margin:5px;
14     }
15 </style>
16   </head>
17   
18   <body>
19     <form action="waiter" method="post">
20     <div>
21         <span>菜品名称:</span>
22         <input type="text" name="dinner">
23     </div>
24     <div>
25         <input type="submit" value="确认下单">
26     </div>
27     </form>
28   </body>
29 </html>
input_dinner.html

 

 

 

 

结果:

 

 

 

 

 

 

 

posted on 2017-04-11 23:57  随心随行  阅读(95)  评论(0)    收藏  举报