Hello2 应用程序是一个 Web 模块,它使用 Java Servlet 技术来显示问候语和响应,使用的是 Java Servlet 技术。

该应用程序源代码在 tutorial-examples\web\servlet\hello2\src\main\java\javaeetutorial\hello2 下,分别为:GreetingServlet.javaResponseServlet.java

此 servlet 重写该 doGet 方法,实现 GET HTTP 方法。servlet 显示一个简单的HTML问候表单

GreetingServlet.java

@WebServlet("/greeting")      //将 URL 指定为/greeting
public class GreetingServlet extends HttpServlet { @Override
public void doGet(HttpServletRequest request, //重写 httpservlet 的 doget 方法,实现覆盖并用来处理 get 请求
HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html"); // 设置响应类型为 HTML
response.setBufferSize(8192);
try (PrintWriter out = response.getWriter()) {
out.println("<html lang=\"en\">" //设置 HTML 语言
+ "<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>"); // HTML 源代码,以显示页面 String username = request.getParameter("username");
//获取传来的参数值。
//通过容器传递给当前 httpservlet if (username != null && username.length() > 0) { //判断用户输入的值是否有效
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/response"); //获取url为/response的servlet作为一个dispatcher资源 if (dispatcher != null) {
dispatcher.include(request, response);
}
}
out.println("</body></html>");
}
}
//dispatcher 中通过 request 获得 username,在response中添加了hello这几句话,dispatcher 就是开始获得的 /response 对应的httpservlet。

ResponseServlet.java

@WebServlet("/response")   	// 将 URL 配置为 /response
public class ResponseServlet extends HttpServlet { @Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
try (PrintWriter out = response.getWriter()) { //获取输出对象,用于向页面写数据。返回一个 PrintWriter 对象
String username = request.getParameter("username"); //获取用户的字符串或字符
if (username != null && username.length() > 0) { //判断输入值是否有效,有效则执行
out.println("<h2>Hello, " + username + "!</h2>"); //在页面输出一句话。
}
}
} @Override
public String getServletInfo() {
return "The Response servlet says hello."; }
}

在本地部署servlet时,可以使用以下URL访问它:

http://localhost:8080/hello2/greeting

参考链接:

https://blog.csdn.net/X_Teddy/article/details/88785624

Analysis of Hello2 source code的更多相关文章

  1. LIRE教程之源码分析 | LIRE Tutorial of Analysis of the Source Code

    LIRE教程之源码分析 |LIRE Tutorial of Analysis of the Source Code 最近在做地理图像识别和检索的研究,发现了一个很好用的框架LIRE,遂研究了一通.网上 ...

  2. Memcached source code analysis (threading model)--reference

    Look under the start memcahced threading process memcached multi-threaded mainly by instantiating mu ...

  3. Android Bluetooth Stack: Bluedroid(五岁以下儿童):The analysis of A2DP Source

    1. A2DP Introduction The Advanced Audio Distribution Profile (A2DP) defines the protocols and proced ...

  4. [转]Native Java Bytecode Debugging without Source Code

    link from:http://www.crowdstrike.com/blog/native-java-bytecode-debugging-without-source-code/index.h ...

  5. How to compile and install Snort from source code on Ubuntu

    http://www.tuicool.com/articles/v6j2Ab Snort is by far the most popular open-source network intrusio ...

  6. Source Code Reading for Vue 3: How does `hasChanged` work?

    Hey, guys! The next generation of Vue has released already. There are not only the brand new composi ...

  7. Tips for newbie to read source code

    This post is first posted on my WeChat public account: GeekArtT Reading source code is always one bi ...

  8. 编程等宽字体Source Code Pro(转)

    Source Code Pro - 最佳的免费编程字体之一!来自 Adobe 公司的开源等宽字体下载     每一位程序员都有一套自己喜爱的代码编辑器与编程字体,譬如我们之前就推荐过一款"神 ...

  9. How to build the Robotics Library from source code on Windows

    The Robotics Library is an open source C++ library for robot kinematics, motion planning and control ...

随机推荐

  1. 用Matplotlib画三维图片的一个实例

    from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np from matp ...

  2. 估计量|估计值|矩估计|最大似然估计|无偏性|无偏化|有效性|置信区间|枢轴量|似然函数|伯努利大数定理|t分布|单侧置信区间|抽样函数|

    第二章 置信区间估计 估计量和估计值的写法? 估计值希腊字母上边有一个hat 点估计中矩估计的原理? 用样本矩来估计总体矩,用样本矩的连续函数来估计总体矩的连续函数,这种估计法称为矩估计法.Eg:如果 ...

  3. 爬虫—GEETEST滑动验证码识别

    一.准备工作 本次使用Selenium,浏览器为Chrome,并配置好ChromDriver 二.分析 1.模拟点击验证按钮:可以直接使用Selenium完成.    2.识别滑块的缺口位置:先观察图 ...

  4. Shell语法 【if while for】

    [if语法 测试条件 判断语句] 转自:http://lovelace.blog.51cto.com/1028430/1211353 [while for循环] 转自:https://blog.csd ...

  5. TPO1-2 The Origin of Theater

    Stories (myths) may then grow up around a ritual. Frequently the myths include representatives of th ...

  6. 代码审计中的SQL注入

    0x00 背景 SQL注入是一种常见Web漏洞,所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令.本文以代码审计的形式研 ...

  7. CHI 2013:人机交互领域那些令人兴奋的新技术

    2013:人机交互领域那些令人兴奋的新技术" title="CHI 2013:人机交互领域那些令人兴奋的新技术"> 编者按:CHI是人机交互领域首屈一指的国际盛会, ...

  8. 常见的Java不规范代码

    1.格式化源代码 Ctrl + Shift + F – 格式化源代码. Ctrl + Shift + O – 管理import语句并移除未使用的语句 除了手动执行这两个功能外,你还可以让Eclipse ...

  9. [LC] 443. String Compression

    Given an array of characters, compress it in-place. The length after compression must always be smal ...

  10. 学习python-20191107

    一.导入模块的两种方式 方式1:import 包名.模块名 [ as 别名]方式2:from 包名 import 模块名            from 包名.模块名 import 变量|函数|类 # ...