hello2 部分代码解析
ResponseServlet.java源码文件
1 @WebServlet("/response") //以@WebServlet注释开头,注释指定相对于上下文根的URL模式, 2 //即在根目录下使用/response来访问,这里作为greting页面的响应页面来实现访问 3 public class ResponseServlet extends HttpServlet { 4 5 @Override 6 public void doGet(HttpServletRequest request, //该servlet重写该doGet方法,实现GET HTTP方法 7 HttpServletResponse response) 8 throws ServletException, IOException { 9 try (PrintWriter out = response.getWriter()) { //实例化对象out,作为向html页面输出的对象 10 //接收来自greting页面请求中的input标签中输入的username 11 String username = request.getParameter("username"); 12 if (username != null && username.length() > 0) { 13 //使用println属性,向html页面输出html标签,这里输出的是: 14 //<h2>Hello,XXX(前面输入的username的值)!</h2> 15 out.println("<h2>Hello, " + username + "!</h2>"); 16 } 17 } 18 } 19 20 @Override 21 public String getServletInfo() { 22 return "The Response servlet says hello."; 23 24 } 25 }