hello2 Source Analisis
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的更多相关文章
- 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 analysis
在example目录下的web\servlet\hello2\src\main\java\javaeetutorial\hello2路径里可以找到hello2的GreetingServlet.java ...
- Analysis of Hello2 source code
Hello2 应用程序是一个 Web 模块,它使用 Java Servlet 技术来显示问候语和响应,使用的是 Java Servlet 技术. 该应用程序源代码在 tutorial-examples ...
- hello2 source anaylis
首先,我们先来看一看这一段的整体代码, 代码如下: @WebServlet("/greeting") public class GreetingServlet extends Ht ...
- 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运行环境 ...
- 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 ...
- 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 ...
- source /etc/profile报错-bash: id:command is not found
由于误操作导致 source /etc/profile 报错 -bash: id:command is not found 此时,linux下很多命令到不能能用,包括vi ls 等... 可以使用 e ...
随机推荐
- AI外包 人工智能外包 长年承接人工智能项目 北京动点软件
人工智能artificial intelligence,AI是科技研究中最热门的方向之一.像 IBM.谷歌.微软.Facebook 和亚马逊等公司都在研发上投入大量的资金.或者收购那些在机器学习.神 ...
- 解决[Errno 10048]报错
最近一直很纳闷,写的python程序在自己电脑上运行得非常顺利,放到远程测试机上经常报“[Errno 10048]通常每个套接字地址(协议/网络地址/端口)“这样的错误,百度到这种是端口号冲突引起的, ...
- Element-ui框架checkbox复选框回显
先看下效果是不是你需要的..... 然后废话不多说,上代码,希望能够帮助到你... <template> <div class=''> <el-form label-wi ...
- js判重
1.两个数组,取出不重复的部分 var arr=[1,2,3]; var arr1=[1,2]; vat tmp=[]; for(let i in arr1){ if(!(arr.includes(a ...
- vscode相关配置
一.插件 二.首先项设置: { "git.enableSmartCommit": true, "gitlens.advanced.messages": { &q ...
- 欢迎观临,这里藏着个秘密,神秘摩斯重着盛装依旧精彩,正如期待一个有趣的灵魂,轻启the key in my hands,也许是命中注定,我们的故事始于你生日的那天,每一句都动人心弦
U2FsdGVkX1/goI0+jYuePWrZo0ynTTAnPVoPEtJYC/1/9/DvtB2x7RkQYO8cEhp5Cb6YK0AqvXbwVmxFguDwZIevQhux++vYdrnB ...
- socket应用(vue、node.js、M站)
socket应用(vue.node.js.M站) 前言:我们在做一些项目的时候需要做到实时变化, 比如我们有时候有需求会要求我们做一个类似于聊天室的页面 比如有些时候我们对某些东西进行点赞和刷票,需要 ...
- MSSQL2005-由嵌套失误感受到的强大智能优化功能
在统计一组数据时用到了子查询(子查询用到了count,父查询用了sum) 后语句优化为无子查询 但是跟踪两次的执行计划和IO统计,发现 1.有子查询时扫描计数和逻辑读取远高于无子查询的语句 2.无子查 ...
- csla框架__使用Factory方式实现Csla.BusinessBase对象数据处理
环境:.net4.6+csla4.6 实现:对象的数据库访问及数据库执行使用Factory方式进行封闭. 正文: 以前在使用csla框架完成业务对象的定义时所有的数据处理都在对象内部实现,也不能说不好 ...
- win10重装win7
一般预装win8之上的电脑都是UEFI+gpt模式的,装win7很麻烦. 最简单省事的方法: BIOS,secure boot 关闭安全模式. 启动方式改为legacy. 启动方式中USB-HDD改到 ...