Hello2 source analysis
在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的更多相关文章
- 【FLYabroad 】微软内部代码检查工具 (Microsoft Source Analysis for C#)[转]
SourceAnalysis (StyleCop)的终极目标是让所有人都能写出优雅和一致的代码,因此这些代码具有很高的可读性. 早就听说了微软内部的静态代码检查和代码强制格式美化工具 StyleCop ...
- Analysis of Hello2 source code
Hello2 应用程序是一个 Web 模块,它使用 Java Servlet 技术来显示问候语和响应,使用的是 Java Servlet 技术. 该应用程序源代码在 tutorial-examples ...
- hello2 Source Analisis
hello2应用程序是一个web模块,它使用Java Servlet技术来显示问候和响应.此应用程序的源代码位于 _tut-install_/examples/web/servlet/hello2/目 ...
- hello2 source analisis(notes)
该hello2应用程序是一个Web模块,它使用Java Servlet技术来显示问候语和响应.使用文本编辑器查看应用程序文件,也可以使用NetBeans IDE. 此应用程序的源代码位于 _tut-i ...
- Analisis of Hello2 source
GreetingServlet.java @WebServlet("/greeting") //为servelt指定URL pattern为:/greeting public cl ...
- hello2 source anaylis
首先,我们先来看一看这一段的整体代码, 代码如下: @WebServlet("/greeting") public class GreetingServlet extends Ht ...
- 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 ...
- 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 ...
- 把vim当做golang的IDE
开始决定丢弃鼠标,所以准备用vim了. 那么在vim里面如何搭建golang环境呢? git盛行之下,搭建vim环境是如此简单. 而且vim搭建好了之后,基本上跟IDE没有差别. 高亮.自动补全.自动 ...
随机推荐
- 14 - How to check replication status
The people using PostgreSQL and the Streaming Replication feature seem to ask many of the same quest ...
- PySe-008-开启浏览器的手机模式
以 Chrome 为例,通过设置 chromeoption 的参数即可实现启动浏览器后,开启手机模式.相应设置的源代码如下所示: chromeOptions = webdriver.ChromeOpt ...
- eclipse安装使用fat打jar包
在线安装步骤: eclipse菜单栏 help >software updates >Search for new features to install>new update si ...
- 关于windows 7 安装Django和基本使用命令
一.安装 在安装前需注意Django 1.6以前的版本不支持python 3.×以上的版本. Django 2.×支持python 3.6 安装方法:打开cmd->输入pip install - ...
- vue 点击图片放大
文档:https://www.npmjs.com/package/vue-directive-image-previewer [只能弹框查看 不能关闭和播放下一张,其他功能使用别的插件] 安装: np ...
- webpack(4)-管理输出
设置 HtmlWebpackPlugin html-webpack-plugin:它会用新生成的 index.html文件,替换我们的原有文件 plugins: [ new HtmlWebpackPl ...
- 常用邮箱POP3 STMP服务器与端口号设置
一.常用邮箱POP3 STMP服务器与端口号设置: [网易 163.126免费邮箱目前不直接开放smtp.pop3服务.有需要的用户可通过购买随身邮或邮箱伴侣及加入会员中心获得.从2006年11月16 ...
- nginx中的超时设置,请求超时、响应等待超时等
nginx比较强大,可以针对单个域名请求做出单个连接超时的配置. 比如些动态解释和静态解释可以根据业务的需求配置 proxy_connect_timeout :后端服务器连接的超时时间_发起握手等候响 ...
- nginx解决跨域
location ~* \.(eot|ttf|woff|woff2|svg)$ { add_header Access-Control-Allow-Origin *; add_header Acces ...
- Unityd外发光Shader Lab
Shader "Faye/OutLightting" { Properties { _MainTex("Texture (RGB)", 2D) = " ...