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

  1. @WebServlet("/greeting") //将 URL 指定为/greeting
  2. public class GreetingServlet extends HttpServlet {
  3. @Override
  4. public void doGet(HttpServletRequest request, //重写 httpservlet 的 doget 方法,实现覆盖并用来处理 get 请求
  5. HttpServletResponse response)
  6. throws ServletException, IOException {
  7. response.setContentType("text/html"); // 设置响应类型为 HTML
  8. response.setBufferSize(8192);
  9. try (PrintWriter out = response.getWriter()) {
  10. out.println("<html lang=\"en\">" //设置 HTML 语言
  11. + "<head><title>Servlet Hello</title></head>"); // 网页标题
  12. // then write the data of the response
  13. out.println("<body bgcolor=\"#ffffff\">"
  14. + "<img src=\"resources/images/duke.waving.gif\" "
  15. + "alt=\"Duke waving his hand\">"
  16. + "<form method=\"get\">"
  17. + "<h2>Hello, my name is Duke. What's yours?</h2>"
  18. + "<input title=\"My name is: \" type=\"text\" "
  19. + "name=\"username\" size=\"25\"/>"
  20. + "<p></p>"
  21. + "<input type=\"submit\" value=\"Submit\"/>"
  22. + "<input type=\"reset\" value=\"Reset\"/>"
  23. + "</form>"); // HTML 源代码,以显示页面
  24. String username = request.getParameter("username");
  25. //获取传来的参数值。
  26. //通过容器传递给当前 httpservlet
  27. if (username != null && username.length() > 0) { //判断用户输入的值是否有效
  28. RequestDispatcher dispatcher =
  29. getServletContext().getRequestDispatcher("/response"); //获取url为/response的servlet作为一个dispatcher资源
  30. if (dispatcher != null) {
  31. dispatcher.include(request, response);
  32. }
  33. }
  34. out.println("</body></html>");
  35. }
  36. }
  37. //dispatcher 中通过 request 获得 username,在response中添加了hello这几句话,dispatcher 就是开始获得的 /response 对应的httpservlet。

ResponseServlet.java

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

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

  1. 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. Gradle project sync failed. Please fix your project and try again

    https://stackoverflow.com/questions/29808199/error-running-android-gradle-project-sync-failed-please ...

  2. 调用新浪短地址转换api的一个测试

    import base64 import requests url="http://www.~~~~.com" headers={ "User-Agent":& ...

  3. endnote插入|管理文件|成组

    信息检索 Endnote Filter(导入)---library(管理)---style(导出) 本地+网络数据库 点击research 在WOS上: 导入改文献: CNKI 导入PDF时选择PDF ...

  4. Linux-使用syslog记录调试信息

    1.有三个函数:openlog.syslog.closelog 2.一般的log信息都在操作系统的/var/log/messages这个文件中存储着,但是ubuntu中是在/var/log/syslo ...

  5. springboot学习笔记:8. springboot+druid+mysql+mybatis+通用mapper+pagehelper+mybatis-generator+freemarker+layui

    前言: 开发环境:IDEA+jdk1.8+windows10 目标:使用springboot整合druid数据源+mysql+mybatis+通用mapper插件+pagehelper插件+mybat ...

  6. goweb-表单

    表单 简单的处理一个登陆界面 package main import ( "fmt" "html/template" "log" " ...

  7. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:多线程队列操作

    import tensorflow as tf #1. 定义队列及其操作. queue = tf.FIFOQueue(100,"float") enqueue_op = queue ...

  8. 03.pipeline实现项目自动编译docker镜像自动打包

    https://jenkins.io/zh/doc/book/pipeline/ 官方教程,可以中文.Jenkinsfile就是把pipeline内容保存到文件,然后提交到svn等版本控制库中.安装b ...

  9. jQuery方法及使用

    jQuery内容: 选择器 筛选器 样式操作 文本操作 属性操作 文档处理 事件 动画效果 插件 each.data.Ajax 剩余未写的有: 1.表单筛选器: :text :password :fi ...

  10. mysql命令行操作大全

    Mysql安装目录 数据库目录/var/lib/mysql/配置文件/usr/share/mysql(mysql.server命令及配置文件)相关命令/usr/bin(mysqladmin mysql ...