zoukankan      html  css  js  c++  java
  • hello2源码分析

    完整的源代码

    /**
     * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
     *
     * You may not modify, use, reproduce, or distribute this software except in
     * compliance with  the terms of the License at:
     * https://github.com/javaee/tutorial-examples/LICENSE.txt
     */
    package javaeetutorial.hello2;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * This is a simple example of an HTTP Servlet. It responds to the GET method of
     * the HTTP protocol.
     */
    @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="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>");
                
                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>");
            }
        }
    
        @Override
        public String getServletInfo() {
            return "The Hello servlet says hello.";
    
        }
    }

    分析
    String userName=request.getParameter(“userName”):定义了一个叫userName的字符串变量,并给他赋值。request.getParameter(“userName”);这句的意思是从request中拿出名字叫userName的值,赋给了你刚才定义的变量。request是负责页面之间传递参数和数据的类,是java封装好的,我们只要把它当成一个工具就可以了。然后判断username是否为空或者长度为0,如果为空或者长度为0就实现Servlet之间的请求传递(页面跳转),最后输出。

  • 相关阅读:
    visio画UML用例图没有include关系的解决方法
    个人推荐-几款好用的App
    win10锁屏壁纸文件夹Assets中无文件问题的解决方法
    云服务器搭建Jupyter-主要部分为配置服务器安全组+添加python3
    开通博客的第一天
    express学习(二)—— Post()类型和中间件
    自定义模块
    命名参数名(含*args , * *kw的区别)
    Python学习之中文注释问题
    Python学习之再议row_input
  • 原文地址:https://www.cnblogs.com/jasonwan/p/10735832.html
Copyright © 2011-2022 走看看