扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
package onlyfun.caterpillar;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetPostServlet extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {
public GetPostServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doResponse(request, response, "GET");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doResponse(request, response, "POST");
}
private void doResponse(HttpServletRequest request,
HttpServletResponse response, String method)
throws ServletException, IOException {
String name = request.getParameter("name");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(method + ": Hello!" + name + "!");
out.flush();
out.close();
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<title>GET、POST</title>
<script type="text/javascript" src="GETPOSTEx-1.js"></script>
</head>
<body>
<input id="namefield" type="text" name="name"/>
<input value="GET" type="button" onclick="doGetRequest();"/>
<input value="POST" type="button" onclick="doPostRequest();"/>
<br>
<div id="response"></div>
</body>
</html>
var xmlHttp;
function createXMLHttpRequest() {
if(window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
function createQueryString() { // 建立請求參數
return "name=" + document.getElementById("namefield").value; // 取得文字方塊值
}
function doGetRequest() {
var url = "GetPostServlet?" + createQueryString();
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", url);
xmlHttp.send(null);
}
function doPostRequest() {
var url = "GetPostServlet?timeStamp=" + new Date().getTime(); // 避免快取
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("POST", url);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(createQueryString());
}
function handleStateChange() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
document.getElementById("response").innerHTML =
xmlHttp.responseText;
}
}
}
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者