hello2 源码分析
Posted dpqq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hello2 源码分析相关的知识,希望对你有一定的参考价值。
1、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; //导入将请求转发送到另一个资源功能的包,它可能是html,servlet或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; //提供HTTP Servlet的请求信息 import javax.servlet.http.HttpServletResponse; //发送响应时提供特定于HTTP的功能 /** * This is a simple example of an HTTP Servlet. It responds to the GET method of * the HTTP protocol. */ @WebServlet("/greeting") //使用源文件中的注释@WebServlet servlet来设置servlet 的URL模式,此处将URL模式指定为/greeting public class GreetingServlet extends HttpServlet { //创建一个继承父类HttpServlet的共有类GreetingServlet @Override //当你想要覆写(重写)某个方法时,可以选择添加@Override这个注解,@Override 系统可以帮你检查方法的正确性 public void doGet(HttpServletRequest request, //重写该doGet方法,实现GETHTTP方法;HttpServletRequest包含客户端对servlet的请求的对象 HttpServletResponse response) //HttpServletResponse包含servlet发送给客户端的响应的对象 throws ServletException, IOException { //java.io.IOException如果在servlet处理GET请求时检测到输入或输出错误;ServletException如果无法处理GET请求 response.setContentType("text/html"); //发送给客户端浏览器的数据类型为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"); //通过绝对路径实现Servlet之间的请求传递(页面跳转) if (dispatcher != null) { dispatcher.include(request, response);//如果接收到的客户端的请求不为空时,记录保留request和response,以后不能再修改response里表示状态的信息 } } out.println("</body></html>"); } } @Override public String getServletInfo() { //定义一个共有类getServletInfo方法获取网页的说明信息 return "The Hello servlet says hello."; } }
2、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,则打印Hello username out.println("<h2>Hello, " + username + "!</h2>");
} } } @Override public String getServletInfo() { return "The Response servlet says hello."; } }
以上是关于hello2 源码分析的主要内容,如果未能解决你的问题,请参考以下文章