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. cin cout

    编写一个程序,要求用户输入一串整数和任意数目的空格,这些整数必须位于同一行中,但允许出现在该行的任何位置.当用户按下enter键,数据输入停止.程序自动对所有的整数进行求和并打印出结果. 需要解决两个 ...

  2. 在CentOS7上从源码编译安装redis,并做成服务程序

    1.安装编译的依赖环境 # 安装pcre开发包: yum install -y pcre-devel # 安装ssl功能需要openssl库 yum -y install openssl-devel ...

  3. MySQL——事务(transaction)简单总结

    简介: MySQL事务操作主要用于处理操作量大,复杂度高的数据,比如说,在人员管理系统中要删除一个人员,你既要删除他的基本资料,也要删除该人员的相关信息,如文章.信箱等.这些数据库操作语句就构成了一个 ...

  4. Travelling Businessmen Problem

    Travelling Businessmen Problem 先求出图的两个部分,可能只有一个部分 然后用set模拟,得到不同部分差最小的 #include <bits/stdc++.h> ...

  5. 处理Ajax请求跨域问题

    ajax跨域的原理 ajax出现请求跨域错误问题,主要原因就是因为浏览器的“同源策略”. CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resourc ...

  6. doc文件转txt

    doc文件转txt # -*- coding:utf-8 -*- # 安装pywin32包 http://sourceforge.net/projects/pywin32/files/pywin32/ ...

  7. JavaScript可以做嵌入式开发了

    JS怎么可能搞嵌入式开发? TESSEL这个工具可直接在设备上运行JS,无需服务端支持. 百闻不如一见,请查看官网介绍吧:http://technical.io/ 网页内还有一个示例,点击会看到LED ...

  8. 统计学中比较重要的分布及python中使用方法

    比较重要的四种分布:正态分布.卡方分布.F分布.t分布 卡方分布概率密度曲线 t分布概率密度曲线 F分布概率密度曲线 参考资料: 统计学中四个概率分布 重要抽样分布

  9. Pooled genome sequence strategies |representative genome assembly approaches|Domestication|GERP|selective sweep|Hybridization|Introgression|iHS|SNP genotyping arrays|haplotype

    Design based on biology 通过比较基因组学的方法,将脊椎动物基因组的数据,解决生物学各方面问题.新的调控注释(在脊椎动物的进化过程中的出现的)可以丰富物种树(比如不同功能蛋白质进 ...

  10. iOS 之keychain详解(附有Demo)

    iOS keychain是苹果用来保存用户私密数据的一个专业的SQLite数据库.保存的数据主要是一些轻量级的私密数据,比如用户密码,token(令牌)等,保存在这个数据库中的密码不会因为你卸载了ap ...