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

GreetingServlet.java(显示问候页面表单)

package javaeetutorial.hello2;

import java.io.IOException;      //IOException表示发生某种I/O异常的信号。此类是由失败或中断的I/O操作产生的一般异常类。
import java.io.PrintWriter;      //io常用类,包装流PrintWriter除了可以包装字节流OutputStream之外,还能包装字符流Writer。
import javax.servlet.RequestDispatcher;    //定义一个对象,该对象接收来自客户端的请求,并将它们发送到服务器上的任何资源(例如servlet,HTML文件或JSP文件)。
import javax.servlet.ServletException;    //定义servlet在遇到困难时可以抛出的一般异常。
import javax.servlet.annotation.WebServlet;    //web服务中的,在Glassfish下lib中的包。
import javax.servlet.http.HttpServlet;     //提供要进行子类化的抽象类,以创建适用于Web站点的HTTP Servlet。
import javax.servlet.http.HttpServletRequest;    //扩展ServletRequest接口以提供HTTP Servlet的请求信息。
import javax.servlet.http.HttpServletResponse;    //扩展ServletResponse接口以在发送响应时提供特定于HTTP的功能。 /**
* This is a simple example of an HTTP Servlet. It responds to the GET method of
* the HTTP protocol.
*/
@WebServlet("/greeting")    //设置标注@webserverlet,容器会自动读取里面的信息。此标注告诉容器,如果请求的UEL是“/greeting”,则由GreetingServelet的实例提供服务。
public class GreetingServlet extends HttpServlet {    //创建一个公有类GreetingServlet继承父类HttpServlet @Override          //覆盖标注,意思是下面覆盖HttpServlet中的doGet方法
public void doGet(HttpServletRequest request,    //参数:—req- HttpServletRequest包含客户端对servlet的请求的对象
HttpServletResponse response)     //参数:resp- HttpServletResponse包含servlet发送给客户端的响应的对象
throws ServletException, IOException {    //抛出:java.io.IOException - 如果在servlet处理GET请求时检测到输入或输出错误;ServletException - 如果无法处理GET请求 response.setContentType("text/html");    //发送给客户端的文章类型
response.setBufferSize(8192);    //发送给客户端的响应对象的缓冲大小是8192
try (PrintWriter out = response.getWriter()) {    //获取PrintWriter流,用来在客户端输出
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>"); String username = request.getParameter("username");    //定义一个字符串username并对它赋从request中拿出名字叫userName的值
if (username != null && username.length() > 0) {    //如果username不为空并且长度大于0
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/response");    //获取jsp上下文里边存储了各变量的信息(值),把一个命令发送到浏览器,让浏览器对指定的URL提出请求(此处的URL只能使用绝对路径) if (dispatcher != null) {
dispatcher.include(request, response);   //如果接收到的客户端的请求不为空时,记录保留request和response,以后不能再修改response里表示状态的信息
}
}
out.println("</body></html>");
}
} @Override    //覆盖
public String getServletInfo() {    //getServletInfo()方法是一个可选的方法,它提供有关servlet的信息,如作者、版本、版权
return "The Hello servlet says hello.";    //返回说明这个servelet的信息是says hello
}
}

ResponseServlet.java(响应页面)

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* This is a simple example of an HTTP Servlet. It responds to the GET
* method of the HTTP protocol.
*/
@WebServlet("/response")
public class ResponseServlet extends HttpServlet { @Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
try (PrintWriter out = response.getWriter()) {
String username = request.getParameter("username");    //同上
if (username != null && username.length() > 0) {    //如果username不为空且长度大于0
out.println("<h2>Hello, " + username + "!</h2>");    //打印Hello username
}
}
} @Override
public String getServletInfo() {
return "The Response servlet says hello."; }
}

Hello2 source analysis的更多相关文章

  1. 【FLYabroad 】微软内部代码检查工具 (Microsoft Source Analysis for C#)[转]

    SourceAnalysis (StyleCop)的终极目标是让所有人都能写出优雅和一致的代码,因此这些代码具有很高的可读性. 早就听说了微软内部的静态代码检查和代码强制格式美化工具 StyleCop ...

  2. Analysis of Hello2 source code

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

  3. hello2 Source Analisis

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

  4. hello2 source analisis(notes)

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

  5. Analisis of Hello2 source

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

  6. hello2 source anaylis

    首先,我们先来看一看这一段的整体代码, 代码如下: @WebServlet("/greeting") public class GreetingServlet extends Ht ...

  7. Why many EEG researchers choose only midline electrodes for data analysis EEG分析为何多用中轴线电极

    Source: Research gate Stafford Michahial EEG is a very low frequency.. and literature will give us t ...

  8. What technical details should a programmer of a web application consider before making the site public?

    What things should a programmer implementing the technical details of a web application consider bef ...

  9. 把vim当做golang的IDE

    开始决定丢弃鼠标,所以准备用vim了. 那么在vim里面如何搭建golang环境呢? git盛行之下,搭建vim环境是如此简单. 而且vim搭建好了之后,基本上跟IDE没有差别. 高亮.自动补全.自动 ...

随机推荐

  1. 必备Linux命令和C语言基础

    每一个学习嵌入式单片机的伙伴我相信对于这两个都不陌生,这毕竟是嵌入式单片机的生存之道    所有基础还是要打牢的   有句老话说的好基础不牢地动山摇    下面看下系统的资料吧   希望能对大家有所帮 ...

  2. k8s-No.2-pod学习

    本章目录 pod结构图 pod语法及参数说明 pod声明周期 一  pod结构图 大部分情况下,Openshift中的Pod只是容器的载体,通过Deployment.DaemonSet.RC.Job. ...

  3. kubernetes安装

    本文主要参考自: https://blog.csdn.net/real_myth/article/details/78719244 还有一份更适合在生产环境使用的超强高可用(多master,nginx ...

  4. 讨论mui 的 mui.init 与 mui.plusReady

    先来看一段代码 (function(m, doc) { mui.plusReady(function(){ var self = plus.webview.currentWebview(); olti ...

  5. anaconda安装tensorflow报错 No module named 'tensorflow'解决方法(windows)

    这个错误的原因可能是,anaconda安装的python版本为3.7,现在tensorflow仅支持python 3.6   改变python版本:首先在命令行创建一个名为python36的环境,指定 ...

  6. 775. Global and Local Inversions

    We have some permutation A of [0, 1, ..., N - 1], where N is the length of A. The number of (global) ...

  7. 读高性能MySql笔记

    1.1 MySQL逻辑架构 MySql服务器逻辑架构图 1.连接管理与安全性 每个客户端连接都会在服务器进程中拥有一个线程,这个连接的查询只会在这个单独的线程中执行,该线程只能轮流在某个CPU核心或者 ...

  8. POJ2762 Going from u to v or from v to u? 强连通分量缩点+拓扑排序

    题目链接:https://vjudge.net/contest/295959#problem/I 或者 http://poj.org/problem?id=2762 题意:输入多组样例,输入n个点和m ...

  9. weblogic的基础安装

    安装java环境 不能使用centos自带的openjdk  必须使用源码安装 把下载的jdk-8u181-linux-x64.tar 解压到 /usr/src目录下      tar zxvf jd ...

  10. extjs 跨域 ajax.request

    https://www.cnblogs.com/yuzhongwusan/p/3677955.html https://stackoverflow.com/questions/25727306/req ...