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.";

    }
}

Analisis of Hello2 source的更多相关文章

  1. hello2 Source Analisis

    hello2应用程序是一个web模块,它使用Java Servlet技术来显示问候和响应.此应用程序的源代码位于 _tut-install_/examples/web/servlet/hello2/目 ...

  2. hello2 source analisis(notes)

    该hello2应用程序是一个Web模块,它使用Java Servlet技术来显示问候语和响应.使用文本编辑器查看应用程序文件,也可以使用NetBeans IDE. 此应用程序的源代码位于 _tut-i ...

  3. Hello2 source analysis

    在example目录下的web\servlet\hello2\src\main\java\javaeetutorial\hello2路径里可以找到hello2的GreetingServlet.java ...

  4. Analysis of Hello2 source code

    Hello2 应用程序是一个 Web 模块,它使用 Java Servlet 技术来显示问候语和响应,使用的是 Java Servlet 技术. 该应用程序源代码在 tutorial-examples ...

  5. hello2 source anaylis

    首先,我们先来看一看这一段的整体代码, 代码如下: @WebServlet("/greeting") public class GreetingServlet extends Ht ...

  6. seL4之hello-2旅途(完成更新)

    seL4之hello-2旅途 2016/11/19 13:15:38 If you like my blog, please buy me a cup of coffee. 回顾上周 seL4运行环境 ...

  7. AutoMapper:Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type

    异常处理汇总-后端系列 http://www.cnblogs.com/dunitian/p/4523006.html 应用场景:ViewModel==>Mode映射的时候出错 AutoMappe ...

  8. mysql-5.6.34 Installation from Source code

    Took me a while to suffer from the first successful souce code installation of mysql-5.6.34. Just pu ...

  9. source /etc/profile报错-bash: id:command is not found

    由于误操作导致 source /etc/profile 报错 -bash: id:command is not found 此时,linux下很多命令到不能能用,包括vi ls 等... 可以使用 e ...

随机推荐

  1. Angular记录(4)

    文档资料 箭头函数--MDN:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/Arrow_fun ...

  2. 静态网站创建工具Docusaurus

    地址:https://docusaurus.io/docs/zh-CN/installation 安装 Docusaurus

  3. docker 容器网络基础

    ======================== docker缺省自带的网络 ======================== host 网络, This enables a container to ...

  4. 1、jQuery的使用入门

    一.创建一个WEB项目,在WebRoot下新建一个Jscript文件夹,并将jQuery中的jquery-3.1.1.min.js文件复制过来. 二.用<script>标签引入jQuery ...

  5. MySQL保留字不能作为字段名使用

    在设计MySQL字段的时候,无意中使用InOut这个名称作为字段名称,结果前端提交后就是没有写入数据库!但后端没有任何提示,跟踪mySQL日志,也没有留下痕迹,反复查,不得其解. 后来实在没有办法情况 ...

  6. sqlmap注入入门

    sqlmap注入入门 sqlmap的用法: ​ linux中: sqlmap [选项] ​ Windows中: python sqlmap [选项] 常用的参数及含义: 目标 ​ -d DIRECT ...

  7. 为什么ArrayList、LinkedList线程不安全,Vector线程安全

    ArrayList源码 public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! ele ...

  8. word20161229

    1. launch 英[lɔ:ntʃ]美[lɔntʃ, lɑntʃ]vt. 发射; 发动; [计算机]开始(应用程序); 开展(活动.计划等);vi. 投入; 着手进行; 热衷于…;n. 投掷; 大船 ...

  9. 【easy】189. Rotate Array

    题目标签:Array 题目给了我们一个数组 和 k. 让我们 旋转数组 k 次. 方法一: 这里有一个很巧妙的方法: 利用数组的length - k 把数组 分为两半: reverse 左边和右边的数 ...

  10. html获取输入框的值

    https://zhinan.sogou.com/guide/detail/?id=316512383339