Analisis of Hello2 source

时间:2023-02-15 10:00:14

GreetingServlet.java

@WebServlet("/greeting")   //为servelt指定URL pattern为:/greeting
public class GreetingServlet extends HttpServlet {

    @Override              //注解,对子类的方法进行重写
    public void doGet(HttpServletRequest request,   //扩展HttpServelt,重写doGet方法
            HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");         //指定响应页面的类型为html
        response.setBufferSize(8192);                   //指定缓冲区的大小
        try (PrintWriter out = response.getWriter()) {        //实例化对象out,作为向html页面输出的对象
            //使用println属性,向html页面输出html标签,这里输出的是:
            //<html lang=en>
            //<head>
            //    <title>Servelt Hello</title>
            //</head>
            out.println("<html lang=\"en\">"
                    + "<head><title>Servlet Hello</title></head>");

            // then write the data of the response
            //使用println属性,向html页面输出html标签,这里输出的是:
            //<body bgcolor=#ffffff>
            //<img scr=resources/images/duke.waving.gif alt=Duke waving his hand>
            //<form method=get>
            //    <h2>Hello,My name is Duke.What's yours?</h2>
            //    <input title=My name is: type=text name=username size=25>
            //    <p></p>
            //    <input type=submit value=Submit/>
            //    <input type=result value=Reset/>
            //</form>
            out.println("<body  bgcolor=\"#ffffff\">"
                    + "<img src=\"resources/images/duke.waving.gif\" "
                    + "alt=\"Duke waving his hand\">"
                    + "<form method=\"get\">"
                    + "<h2>Hello, my name is Duke. What's yours?</h2>"
                    + "<input title=\"My name is: \" type=\"text\" "
                    + "name=\"username\" size=\"25\"/>"
                    + "<p></p>"
                    + "<input type=\"submit\" value=\"Submit\"/>"
                    + "<input type=\"reset\" value=\"Reset\"/>"
                    + "</form>");

            //将抓取到的username的值赋给一个字符串username,获取input标签内的username值
            String username = request.getParameter("username");
            if (username != null && username.length() > 0) {
                RequestDispatcher dispatcher =
                        getServletContext().getRequestDispatcher("/response");
                        //指定要跳转的页面相对于上下文根的URL pattern为/response
                if (dispatcher != null) {
                    //整合request、response然后跳转
                    dispatcher.include(request, response);
                }
            }
            //使用println属性,向html页面输出HTML标签,这里输出的是:
            //    </body>
            //</html>
            out.println("</body></html>");
        }
    }

    @Override
    public String getServletInfo() {
        return "The Hello servlet says hello.";

    }
}

ResponseServelt.java

@WebServlet("/response")        //注释指定相对于上下文根的URL模式为:/response
public class ResponseServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request,    //该servelt重写改doGet方法,实现Get HTTP方法
            HttpServletResponse response)
            throws ServletException, IOException {
        try (PrintWriter out = response.getWriter()) {           //实例化对象out,作为html页面输出的对象
            //接受来自greeting页面请求中的input标签中的username输入
            String username = request.getParameter("username");
            if (username != null && username.length() > 0) {
                //使用println属性,向html页面输出html标签,这里输出的是:
                //<h2>Helelo," + username + "!</h2>
                out.println("<h2>Hello, " + username + "!</h2>");
            }
        }
    }

    @Override
    public String getServletInfo() {
        return "The Response servlet says hello.";

    }
}