zoukankan      html  css  js  c++  java
  • hello2代码分析

    Responseservlet.java:
    package javaeetutorial.hello2;  #软件包名为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;

      #导入io 和 serverlet相关的各类包
    @WebServlet("/response")

      #注解WebServerlet 为response
    public class ResponseServlet extends HttpServlet {

      #定义类ReponseServerlet从HttpServerlet拓展而来

    @Override

      #注解Override
    public void doGet(HttpServletRequest request,

      #定义对象doGet ,定义HttpServerletRequest 为request

    HttpServletResponse response)

      #定义HttpServerResponse 为response
    throws ServletException, IOException {

      #抛出异常
    try (PrintWriter out = response.getWriter()) {

      #将response的getWeite方法赋值给PrintWrite的out,response的getWrite方法来自于HttpServletResponse接口方法
    String username = request.getParameter("username");

      #将request的getParameter方法获得的username赋值给字符串username,requset来自于HttpServerletRequest接口方法
    if (username != null && username.length() > 0) {

      #当username字符串不为空且username的长度大于0时
    out.println("<h2>Hello, " + username + "!</h2>");

      #调用out的println打印Hello,和字符串
    }
    }
    }

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

      #定义字符串getServerletinfo,返回内容

    }
    }

    GreetingServlet.java:

    package javaeetutorial.hello2;  

      #包名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")  

      #注解名greeting
    public class GreetingServlet extends HttpServlet {

      #拓展类GreetingServlet从HttpServlet接口

    @Override

      #注解Override
    public void doGet(HttpServletRequest request,
    HttpServletResponse response)

      #同上定义对象 HttpServletRequest名为request,HttpServletResponse名为response
    throws ServletException, IOException {

      #抛出异常

    response.setContentType("text/html");

      #定义文本类型为text,html
    response.setBufferSize(8192);

      #定义缓冲池大小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");
    if (username != null && username.length() > 0) {
    RequestDispatcher dispatcher =
    getServletContext().getRequestDispatcher("/response");
      #将response响应赋给dispatcher分发调度
    if (dispatcher != null) {
    dispatcher.include(request, response);
    }

      #当匹配到处理请求响应的处理服务时,就返回请求和response.java的响应内容结果
    }
    out.println("</body></html>");
    }
    }

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

      #在页面返回响应的文字

    }
    }

  • 相关阅读:
    2012619 win7环境Jooma建站手记解决问题
    linux学习(4)ubuntu开机自动挂载nfs服务器上的home分区
    linux学习(3)nginx tomcat集群
    oracle to_date 函数
    inux学习(2)vim设置
    linux学习(1)常用命令入门
    tomcat域名绑定
    tomcat内存溢出问题解决思路
    TreeView控件无限级 一次性加载和异步加载剖析
    数据查询支持中文拼音首字母模糊检索
  • 原文地址:https://www.cnblogs.com/lijianxuan/p/10606299.html
Copyright © 2011-2022 走看看