zoukankan      html  css  js  c++  java
  • Analysis of Hello2 source code

    GreetingServlet.java

    package javaeetutorial.hello2;  //导入包
    //import 导入类和接口
    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")  //指定上下文根的相对URL模式
    public class GreetingServlet extends HttpServlet {  //定义一个GreetingServlet类继承HttpServlet

        @Override       //覆盖方法
        public void doGet(HttpServletRequest request, //定义一个doGet方法 ,有两个参数
                HttpServletResponse response)
                throws ServletException, IOException { //抛出异常

            response.setContentType("text/html"); //获取上下文类型的响应
            response.setBufferSize(8192);         //设置BufferSize为8192
            try (PrintWriter out = response.getWriter()) {     //定义一个PrintWriter类型的变量out 写入
                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");  //定义string类型变量name 将响应得到的username赋给它
                if (username != null && username.length() > 0) {      //判断username不存在并且长度大于0
                    RequestDispatcher dispatcher =                    
                            getServletContext().getRequestDispatcher("/response"); //将内容跳转到服务器的/response地址上
                   
                    if (dispatcher != null) {
                        dispatcher.include(request, response);  //包含响应中的资源
                    }
                }
                out.println("</body></html>");
            }
        }

        @Override  //覆盖,方法重写
        public String getServletInfo() {         //重写getServletInfo方法 并返回字符串
            return "The Hello servlet says hello.";

        }
    }
     

    ResponseServlet.java

    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")  //指定上下文的相对URL
    public class ResponseServlet extends HttpServlet {  //定义一个ResponseServlet类继承HttpServlet

        @Override//覆盖
        public void doGet(HttpServletRequest request,  //定义doGet方法,有两个参数
                HttpServletResponse response)
                throws ServletException, IOException { //抛出异常
            try (PrintWriter out = response.getWriter()) {  //定义PrintWrite类型变量out 将getWriter输出的数据赋给它
                String username = request.getParameter("username");  //将响应得到的username赋值给String类型的变量username
                if (username != null && username.length() > 0) { //如果username不为空并且长度大于0
                    out.println("<h2>Hello, " + username + "!</h2>");//输出hello+username
                }
            }
        }

        @Override//覆盖方法
        public String getServletInfo() { //返回字符串
            return "The Response servlet says hello.";

        }
    }
  • 相关阅读:
    SuperMap房产测绘成果管理平台
    SuperMap产权登记管理平台
    Android adb shell am 的用法(1)
    由浅入深谈Perl中的排序
    Android 内存监测和分析工具
    Android 网络通信
    adb server is out of date. killing...
    引导页使用ViewPager遇到OutofMemoryError的解决方案
    adb logcat 详解
    How to send mail by java mail in Android uiautomator testing?
  • 原文地址:https://www.cnblogs.com/wudidd/p/10768433.html
Copyright © 2011-2022 走看看