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

代码如下:

@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里表示状态的信息。 
}
}

hello2 source anaylis的更多相关文章

  1. Hello2 source analysis

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

  2. hello2 Source Analisis

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

  3. hello2 source analisis(notes)

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

  4. Analysis of Hello2 source code

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

  5. Analisis of Hello2 source

    GreetingServlet.java @WebServlet("/greeting") //为servelt指定URL pattern为:/greeting public cl ...

  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机器学习包 Windows下 pip安装 scikit-learn numpy scipy

    1.到PIP的目录中C:\Python34\Scripts;2. 2.1  pip安装numpy pip install numpy 2.2  pip安装sklearn pip install -U ...

  2. Android查看文件大小

    查看当前路径下的各个挂载模块的大小及剩余量(例如在根目录执行) df #输出 Filesystem Size Used Free Blksize /sys/fs/cgroup .0K /mnt/ase ...

  3. 怎么下载geventwebsocket

    pip install gevent-websocket sudo pip install gevent-websocket

  4. java第一次上机实验--验证码

    package javashiyan; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event. ...

  5. Java中的 内部类(吐血总结)

    1. 内部类的作用 内部类是一个独立的实体,可以用来实现闭包:能与外部类通信:内部类与接口使得多继承更完整 2. 内部类的分类 1)普通内部类 类的实例相关,可以看成是一个实例变量.内部类的类名由 “ ...

  6. git最佳实践之feature和hotfix分支

    先来复习一波,git的最佳分支管理流程: 再简单复习各个分支: master: 主分支,主要用来版本发布. develop:日常开发分支,该分支正常保存了开发的最新代码. feature:具体的功能开 ...

  7. eclipse解决js提示

    自学js,发现eclipse中不管js文件.html文件.jsp文件没有都没js代码的提示,对于js代码也不报错,有时候就因为单词敲错却查了很久没查出来,很烦很难受. 在网上找了很多方法,都没有解决, ...

  8. 作业2:git使用

    作业要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 远端库地址:https://github.com/yellow ...

  9. Python网络编程-Socket简单通信(及python实现远程文件发送)

    学习python中使用python进行网络编程,编写简单的客户端和服务器端进行通信,大部分内容来源于网络教程,这里进行总结供以后查阅. 先介绍下TCP的三次握手: 1,简单的发送消息: 服务器端: i ...

  10. python 中的__new__与__init__

    在Python中的class中有两个方法__new__与__init__,有什么区别呢? class TestCls(): """docstring for TestCl ...