• hello2 source anaylis


    首先,我们先来看一看这一段的整体代码,

    代码如下:

    @WebServlet("/greeting")
    public class GreetingServlet extends HttpServlet {
    
        @Override
        public void doGet(HttpServletRequest request,
                HttpServletResponse response)
                throws ServletException, IOException {
    
            response.setContentType("text/html");
            response.setBufferSize(8192);
            try (PrintWriter out = response.getWriter()) {
                out.println("<html lang="en">"
                        + "<head><title>Servlet Hello</title></head>");
    
                // then write the data of the response
                out.println("<body  bgcolor="#ffffff">"
                    + "<img src="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>");
    
                String username = request.getParameter("username");
                if (username != null && username.length()> 0) {
                    RequestDispatcher dispatcher =
                        getServletContext().getRequestDispatcher("/response");
    
                    if (dispatcher != null) {
                        dispatcher.include(request, response);
                    }
                }
                out.println("</body></html>");
            }
        }

    对里面的部分代码分析:

    String username = request.getParameter("username");  //获取通过URL或者form传递过来的数据并赋值给username
    if (username != null && username.length() > 0) {    //对数据进行验证,满足不为空和长度大于0的条件
    RequestDispatcher dispatcher =
    getServletContext().getRequestDispatcher("/response");    
     //获取jsp上下文里边存储了各变量的信息(值),把一个命令发送到浏览器,让浏览器对指定的URL提出请求(此处的URL只能使用绝对路径)
    if (dispatcher != null) {
    dispatcher.include(request, response);    //如果接收到的客户端的请求不为空时,记录保留request和response,以后不能再修改response里表示状态的信息。 
    }
    }

  • 相关阅读:
    GODIAG GD801 ODOMETER里程改正指南车清单
    XTOOL A80 Pro(H6 Pro)使用提示+常见问题解答
    Vident IAUTO702 Pro 2012 Peugeot 308喷油器编码:是否支持?
    es创建模板
    docker 帮助信息
    聊聊白盒测试、黑盒测试
    Go 通过结构体指定字段进行排序
    Go语言 之结构体数组 赋值
    PHP中json_encode与json_decode
    golang sync WaitGroup
  • 原文地址:https://www.cnblogs.com/madman-fly/p/10591491.html
Copyright © 2020-2023  润新知