Analysis of Hello2 source code

Hello2 应用程序是一个 Web 模块,它使用 Java Servlet 技术来显示问候语和响应,使用的是 Java Servlet 技术。

该应用程序源代码在 tutorial-examples\web\servlet\hello2\src\main\java\javaeetutorial\hello2 下,分别为:GreetingServlet.javaResponseServlet.java

此 servlet 重写该 doGet 方法,实现 GET HTTP 方法。servlet 显示一个简单的HTML问候表单

GreetingServlet.java

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

    @Override
    public void doGet(HttpServletRequest request,  //重写 httpservlet 的 doget 方法,实现覆盖并用来处理 get 请求
            HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");  // 设置响应类型为 HTML
        response.setBufferSize(8192);  
        try (PrintWriter out = response.getWriter()) {
            out.println("<html lang=\"en\">"       //设置 HTML 语言
                    + "<head><title>Servlet Hello</title></head>");   // 网页标题
            
            // then write the data of the response
            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>");  // HTML 源代码,以显示页面
            
            String username = request.getParameter("username");
           //获取传来的参数值。
           //通过容器传递给当前 httpservlet
           
            if (username != null && username.length() > 0) {    //判断用户输入的值是否有效
                RequestDispatcher dispatcher =
                        getServletContext().getRequestDispatcher("/response");    //获取url为/response的servlet作为一个dispatcher资源
                
                if (dispatcher != null) {
                    dispatcher.include(request, response);
                }
            }
            out.println("</body></html>");
        }
    }
    //dispatcher 中通过 request 获得 username,在response中添加了hello这几句话,dispatcher 就是开始获得的 /response 对应的httpservlet。

ResponseServlet.java

@WebServlet("/response")    // 将 URL 配置为 /response
public class ResponseServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {
        try (PrintWriter out = response.getWriter()) {    //获取输出对象,用于向页面写数据。返回一个 PrintWriter 对象
            String username = request.getParameter("username"); //获取用户的字符串或字符
            if (username != null && username.length() > 0) {    //判断输入值是否有效,有效则执行
                out.println("<h2>Hello, " + username + "!</h2>");  //在页面输出一句话。
            }
        }
    }

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

    }
}

在本地部署servlet时,可以使用以下URL访问它:

http://localhost:8080/hello2/greeting

参考链接:

https://blog.csdn.net/X_Teddy/article/details/88785624

上一篇:Hello2 source analisis(代码分析)


下一篇:hello2 source Analysis