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. Python——使用高德API获取指定城指定类别POI并实现XLSX文件合并

    # 以下内容为原创,转载请注明出处 1 import xlrd # 读xlsx import xlsxwriter # 写xlsx import urllib.request # url请求,Pyth ...

  2. react native navigationOptions中不能获取this

    static navigationOptions = ({ navigation, navigationOptions,screenProps }) => { const { params } ...

  3. Swift 4 经典数据结构 Data Struct大全

    快速看看吧,看看大神是如何写出最swifty的算法.我先fork一下,以表敬意. https://github.com/Imputes/swift-algorithm-club

  4. C#多线程处理

    创建多线程,并带参数! using System; using System.Collections; using System.Collections.Generic; using System.I ...

  5. awk和sed截取nginx和tomcat时间段日志

    1 nginx日志截取示例 日志路径:/usr/local/nginx/logs, 截取access.log中2019年3月24日17点00~02之间的日志: 写法1: cat access.log ...

  6. css-reset 代码

    最常用 * { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } *:bef ...

  7. zip4j压缩

    使用的jar包:zip4j_1.3.2.jar 基本功能: 针对ZIP压缩文件创建.添加.分卷.更新和移除文件 (读写有密码保护的Zip文件) (支持AES 128/256算法加密) (支持标准Zip ...

  8. 也写dateUtil.js

    yl.dateUtil = { /** * y 年 * M 月 * d 日 * H 时 h 时(am/pm) * m 分 * s 秒 * S 毫秒 * a 上午/下午(am/pm) * setInte ...

  9. requests使用retry策略

    在urllib3中使用retry 在requests中使用retry 网络请求往往会有很多不受控制的意外情况发生,有时候我们要让它let it crash,有时候我们想多尝试几次. 以前,使用retr ...

  10. zabbix3.2利用自动发现功能对fastcgi模式的php状态进行集中监控

    zabbix3.2利用自动发现功能对fastcgi模式的php状态进行集中监控 前端nginx虚拟主机引用后端多个php接口,为了方便监控,将后端服务器集中配置在nginx中,具体配置如下: [roo ...