• hello2 源代码分析


    /**
    * 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;     //导包

    /**
    * This is a simple example of an HTTP Servlet. It responds to the GET method of
    * the HTTP protocol.
    */
    @WebServlet("/greeting")
    public class GreetingServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {

    response.setContentType("text/html");
    response.setBufferSize(8192);
    try (PrintWriter out = response.getWriter()) {
    out.println("<html lang="en">"
    + "<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赋值
    if (username != null && username.length() > 0) {               //判断username 长度是大于零
    RequestDispatcher dispatcher =
    getServletContext().getRequestDispatcher("/response");

    if (dispatcher != null) {
    dispatcher.include(request, response);
    }                 //

    ||如果接收到的客户端的请求不为空时,记录保留request和response,以后不能再修改response里表示状态的信息。

       请求重定向转发到指定URL "/"代表相对与web应用路径。用include()方法将HTTP请求转送给其他Servlet后,被调用的Servlet虽然可以处理这个HTTP请求,但是最后的主导权仍然是在原来的Servlet。


    }
    out.println("</body></html>");
    }
    }

    @Override
    public String getServletInfo() {
    return "The Hello servlet says hello.";

    }
    }

  • 相关阅读:
    SQL复杂查询和视图(2)
    SQL复杂查询和视图
    SQL语言概述
    元组演算与关系代数关系
    关系的元组演算
    线索二叉树
    二叉树的遍历
    树的基本概念
    【支付专区】之微信支付构建请求参数xml
    【支付专区】之微信支付请求数据签名
  • 原文地址:https://www.cnblogs.com/tily123/p/10810426.html
Copyright © 2020-2023  润新知