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

GreetingServlet.java

/**
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
*
* You may not modify, use, reproduce, or distribute this software except in
* compliance with the terms of the License at:
* https://github.com/javaee/tutorial-examples/LICENSE.txt
*/
package javaeetutorial.hello2; import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; @WebServlet("/greeting") //以@WebServlet注释开头,注释指定相对于上下文根的URL模式,即在根目录下使用/greeting来访问
public class GreetingServlet extends HttpServlet { @Override
public void doGet(HttpServletRequest request, //该servlet重写该doGet方法,实现GET HTTP方法
HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html"); //指定响应页面的类型为html
response.setBufferSize(8192); //指定缓冲区大小
try (PrintWriter out = response.getWriter()) { //实例化对象out,作为向html页面输出的对象 //使用println属性,向html页面输出html标签,这里输出的是: //<html lang=en>
// <head>
// <title>Servlet Hello</title>
// </head> out.println("<html lang=\"en\">"
+ "<head><title>Servlet Hello</title></head>"); //使用println属性,向html页面输出html标签,这里输出的是:
//<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>) 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>"); //使用一个字符串username来获取,第一个input标签内的username的值
String username = request.getParameter("username");
if (username != null && username.length() > 0) {
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/response");
//指定要跳转的页面相对于上下文根的URL模式为/response if (dispatcher != null) {
//整合request, response然后跳转
dispatcher.include(request, response);
}
}
//使用println属性,向html页面输出html标签,这里输出的是:
// </body>
//</html>
out.println("</body></html>");
}
} @Override
public String getServletInfo() {
return "The Hello servlet says hello."; }
}

ResponseServlet.java

/**
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
*
* You may not modify, use, reproduce, or distribute this software except in
* compliance with the terms of the License at:
* https://github.com/javaee/tutorial-examples/LICENSE.txt
*/
package javaeetutorial.hello2; 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; @WebServlet("/response") //以@WebServlet注释开头,注释指定相对于上下文根的URL模式,
//即在根目录下使用/response来访问,这里作为greting页面的响应页面来实现访问
public class ResponseServlet extends HttpServlet { @Override
public void doGet(HttpServletRequest request, ////该servlet重写该doGet方法,实现GET HTTP方法
HttpServletResponse response)
throws ServletException, IOException {
try (PrintWriter out = response.getWriter()) { //实例化对象out,作为向html页面输出的对象
//接收来自greting页面请求中的input标签中输入的username
String username = request.getParameter("username");
if (username != null && username.length() > 0) {
//使用println属性,向html页面输出html标签,这里输出的是:
//<h2>Hello,XXX(前面输入的username的值)!</h2>
out.println("<h2>Hello, " + username + "!</h2>");
}
}
} @Override
public String getServletInfo() {
return "The Response servlet says hello."; }
}

hello2 Source Analisis的更多相关文章

  1. hello2 source analisis(notes)

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

  2. Analisis of Hello2 source

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

  3. Hello2 source analysis

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

  4. Analysis of Hello2 source code

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

  5. hello2 source anaylis

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

  6. seL4之hello-2旅途(完成更新)

    seL4之hello-2旅途 2016/11/19 13:15:38 If you like my blog, please buy me a cup of coffee. 回顾上周 seL4运行环境 ...

  7. AutoMapper:Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type

    异常处理汇总-后端系列 http://www.cnblogs.com/dunitian/p/4523006.html 应用场景:ViewModel==>Mode映射的时候出错 AutoMappe ...

  8. mysql-5.6.34 Installation from Source code

    Took me a while to suffer from the first successful souce code installation of mysql-5.6.34. Just pu ...

  9. source /etc/profile报错-bash: id:command is not found

    由于误操作导致 source /etc/profile 报错 -bash: id:command is not found 此时,linux下很多命令到不能能用,包括vi ls 等... 可以使用 e ...

随机推荐

  1. react native 0.55.4 rctsrwebsocket会崩溃的问题解决 直接原文覆盖

    //// Copyright 2012 Square Inc.//// Licensed under the Apache License, Version 2.0 (the "Licens ...

  2. 区块链 编译android geth 填坑记录 ubuntu

    下载geth 源码  直接 make android 下载android ndk sdk 配置环境变量cd 安装golang  设置环境变量 发现没有gomobile命令 按照wiki方法 执行安装g ...

  3. Hadoop之HDFS概述

    一.HDFS产生背景及定义 1.HDFS产生背景 随着数据量越来越大,在一个操作系统存不下所有的数据,那么就分配到更多的操作系统管理的磁盘中,但是不方便管理和维护,迫切需要一种系统来管理多台机器上的文 ...

  4. Oracle 创建,查询,删除 job

    一 . 创建job 1. 通过创建存储过程的方式创建job 调用该存储过程使其开始执行  call PRO_DSJ_XJTJ_JOB(); create or replace procedure PR ...

  5. 给web请求加遮罩动画

    效果预览: css: /*#fountainG{ position:relative; margin:10% auto; width:240px; height:29px }*/ #fountainG ...

  6. Java8:Lambda表达式增强版Comparator和排序

    1.概述 在这篇教程里,我们将要去了解下即将到来的JDK 8(译注,现在JDK 8已经发布了)中的Lambda表达式——特别是怎样使用它来编写Comparator和对集合(Collection)进行排 ...

  7. NetSec2019 20165327 Exp4 恶意代码分析

    NetSec2019 20165327 Exp4 恶意代码分析 一.实践目标 1.监控你自己系统的运行状态,看有没有可疑的程序在运行. 2.分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分 ...

  8. 后缀html和htm文件的区别

    后缀html和htm文件的区别: (1)如果一个网站有index.html和index.htm,默认情况下,优先访问.html. (2).htm后缀是为了兼容以前的dos系统8.3的命名规范

  9. zookeeper应用与原理学习总结

    一.什么是zookeeper Zookeeper 分布式服务框架是Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务.状态同步服务.集群 ...

  10. mysql索引的选择

    一:索引的常见模型 1.哈希表(key-value)存储的数据结构 缺点:hash索引在做区间查询时,速度慢. 优点:hash索引很适用于等值查询的场景,比如memcached以及其他一些nosql引 ...