Analysis of Hello2 source code
Hello2 应用程序是一个 Web 模块,它使用 Java Servlet 技术来显示问候语和响应,使用的是 Java Servlet 技术。
该应用程序源代码在 tutorial-examples\web\servlet\hello2\src\main\java\javaeetutorial\hello2 下,分别为:GreetingServlet.java
和ResponseServlet.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的更多相关文章
- LIRE教程之源码分析 | LIRE Tutorial of Analysis of the Source Code
LIRE教程之源码分析 |LIRE Tutorial of Analysis of the Source Code 最近在做地理图像识别和检索的研究,发现了一个很好用的框架LIRE,遂研究了一通.网上 ...
- Memcached source code analysis (threading model)--reference
Look under the start memcahced threading process memcached multi-threaded mainly by instantiating mu ...
- Android Bluetooth Stack: Bluedroid(五岁以下儿童):The analysis of A2DP Source
1. A2DP Introduction The Advanced Audio Distribution Profile (A2DP) defines the protocols and proced ...
- [转]Native Java Bytecode Debugging without Source Code
link from:http://www.crowdstrike.com/blog/native-java-bytecode-debugging-without-source-code/index.h ...
- 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 ...
- 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 ...
- 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 ...
- 编程等宽字体Source Code Pro(转)
Source Code Pro - 最佳的免费编程字体之一!来自 Adobe 公司的开源等宽字体下载 每一位程序员都有一套自己喜爱的代码编辑器与编程字体,譬如我们之前就推荐过一款"神 ...
- 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 ...
随机推荐
- python库文件下载地址(持续更新)
numpy https://pypi.org/project/numpy/#files PIL https://pypi.org/simple/pillow/ cv2 https://pypi.tun ...
- centos 下使用 pytesseract 识别文字
偶发一个想法搭一个验证码识别工具,网上查了一下有Tesseract 这个工具可以识别,所以有了后面一小时的搭建过程 ps:Ubuntu 下似乎可以直接用包管理工具来安装,我使用的源码编译安装 前提 由 ...
- 显著水平|区间估计|假设检验|显著性|第一类错误|Ⅱ类错误|β错误|t检验|连续性矫正|二项分布的假设检验|样本百分率|
第三章 假设检验 区间估计与假设检验的基本区别? 上一章中讨论了置信区间的估计方法.它是利用样本数据,以抽样总体的分布为理论基础,用一定的概率保证来计算出原总体中未知参数的区间范围.特别值得注意的是: ...
- 886C. Petya and Catacombs#墓室探险(set集合)
题目出处:http://codeforces.com/problemset/problem/886/C 题目大意:很多墓穴之间有通道,探险家来回穿梭并记录日志 日志规则:第一次到该墓穴计时间t,0&l ...
- visual studio2019下静态链接库的制作
创建静态库项目 项目名称为20199324lib // pch.h #ifndef __PCH__ #define __PCH__ extern int add(int a, int b);//ext ...
- 关于tomcat报错记录
启动报错关键信息如下: Caused by: java.lang.IllegalStateException: Unable to complete the scan for annotations ...
- hdu2222 (AC自动机模板)
题:http://acm.hdu.edu.cn/showproblem.php?pid=2222 学习出:https://bestsort.cn/2019/04/28/402/ 主要是fail的建立. ...
- shell_常规小脚本
shell 1.检查Mysql的健康状态 #!/bin/bashpgrep -x mysqld &> /dev/nullif [ $? -ne 0 ]thenecho “At time: ...
- VMware 三种网络配置解释
https://blog.csdn.net/noob_f/article/details/51099040 ifconfig -a 网卡名称
- day06-迭代器
一.迭代器: 1.可迭代协议:含有__iter__方法. 2.迭代器协议:同时含有__iter__和__next__方法.迭代器是可迭代对象. iterator迭代器. 3.使用可迭代对象有什么好处? ...