其他分享
首页 > 其他分享> > hello2 source analysis

hello2 source analysis

作者:互联网

1.GreetingServlet.java源码文件:

 
 1 @WebServlet("/greeting")    //以@WebServlet注释开头,注释指定相对于上下文根的URL模式,即在根目录下使用/greeting来访问
 2 public class GreetingServlet extends HttpServlet {
 3 
 4     @Override
 5     public void doGet(HttpServletRequest request,    //该servlet重写该doGet方法,实现GET HTTP方法
 6             HttpServletResponse response)
 7             throws ServletException, IOException {
 8 
 9         response.setContentType("text/html");    //指定响应页面的类型为html
10         response.setBufferSize(8192);    //指定缓冲区大小
11         try (PrintWriter out = response.getWriter()) {    //实例化对象out,作为向html页面输出的对象
12             
13             //使用println属性,向html页面输出html标签,这里输出的是:
14             //<html lang=en>
15             //    <head>
16             //        <title>Servlet Hello</title>
17             //    </head>
18 
19             out.println("<html lang=\"en\">"
20                     + "<head><title>Servlet Hello</title></head>");
21             
22             //使用println属性,向html页面输出html标签,这里输出的是:
23             //<body  bgcolor=#ffffff>
24             //    <img src=resources/images/duke.waving.gif alt=Duke waving his hand>
25             //    <form method=get>
26             //        <h2>Hello, my name is Duke. What's yours?</h2>
27             //        <input title=My name is:  type=text name=username size=25/>
28             //        <p></p>
29             //        <input type=submit value=Submit/>
30             //        <input type=reset value=Reset/>
31             //    </form>)
32             
33             out.println("<body  bgcolor=\"#ffffff\">"
34                     + "<img src=\"resources/images/duke.waving.gif\" "
35                     + "alt=\"Duke waving his hand\">"
36                     + "<form method=\"get\">"
37                     + "<h2>Hello, my name is Duke. What's yours?</h2>"
38                     + "<input title=\"My name is: \" type=\"text\" "
39                     + "name=\"username\" size=\"25\"/>"
40                     + "<p></p>"
41                     + "<input type=\"submit\" value=\"Submit\"/>"
42                     + "<input type=\"reset\" value=\"Reset\"/>"
43                     + "</form>");
44             
45             //使用一个字符串username来获取,第一个input标签内的username的值
46             String username = request.getParameter("username");
47             if (username != null && username.length() > 0) {
48                 RequestDispatcher dispatcher =
49                         getServletContext().getRequestDispatcher("/response");
50                         //指定要跳转的页面相对于上下文根的URL模式为/response
51                 
52                 if (dispatcher != null) {
53                     //整合request, response然后跳转
54                     dispatcher.include(request, response);
55                 }
56             }
57             //使用println属性,向html页面输出html标签,这里输出的是:
58             //    </body>
59             //</html>
60             out.println("</body></html>");
61         }
62     }
63 
64     @Override
65     public String getServletInfo() {
66         return "The Hello servlet says hello.";
67 
68     }
69 }
 

2.ResponseServlet.java源码文件

 
 1 @WebServlet("/response")    //以@WebServlet注释开头,注释指定相对于上下文根的URL模式,
 2                             //即在根目录下使用/response来访问,这里作为greting页面的响应页面来实现访问
 3 public class ResponseServlet extends HttpServlet {
 4 
 5     @Override
 6     public void doGet(HttpServletRequest request,    ////该servlet重写该doGet方法,实现GET HTTP方法
 7             HttpServletResponse response)
 8             throws ServletException, IOException {
 9         try (PrintWriter out = response.getWriter()) {        //实例化对象out,作为向html页面输出的对象
10             //接收来自greting页面请求中的input标签中输入的username
11             String username = request.getParameter("username");        
12             if (username != null && username.length() > 0) {
13                 //使用println属性,向html页面输出html标签,这里输出的是:
14                 //<h2>Hello,XXX(前面输入的username的值)!</h2>
15                 out.println("<h2>Hello, " + username + "!</h2>");
16             }
17         }
18     }
19 
20     @Override
21     public String getServletInfo() {
22         return "The Response servlet says hello.";
23 
24     }
25 }

标签:username,analysis,source,html,println,out,hello2,response,页面
来源: https://www.cnblogs.com/djh222/p/10605153.html