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.";
 
     }
 }
复制代码

转载于:https://www.cnblogs.com/lmt-g/p/10723639.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值