加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

hello2部分代码解析

发布时间:2020-12-12 13:17:01 所属栏目:百科 来源:网络整理
导读:/** * 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-exam

/**

* 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");
if (username != null && username.length() > 0) {
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/response");

if (dispatcher != null) {
dispatcher.include(request,response);
}
}
out.println("</body></html>");
}
}

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

}
}

?

String username = request.getParameter("username");? //获取通过URL或者form传递过来的数据并赋值给usernameif (username != null && username.length() > 0) {? ? //对数据进行验证,满足不为空和长度大于0的条件RequestDispatcher dispatcher =getServletContext().getRequestDispatcher("/response");? ???//获取jsp上下文里边存储了各变量的信息(值),把一个命令发送到浏览器,让浏览器对指定的URL提出请求(此处的URL只能使用绝对路径)if (dispatcher != null) {dispatcher.include(request,response);? ? //如果接收到的客户端的请求不为空时,记录保留request和response,以后不能再修改response里表示状态的信息。?}}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读