javascript與jsp發(fā)送請求到servlet的幾種方式實例
更新時間:2018年03月18日 17:47:11 投稿:wdc
本文分別給出了javascript發(fā)送請求到servlet的5種方式實例與
jsp發(fā)送請求到servlet的6種方式實例
JavaScript提交至servlet 5種方式:
/**第一種提交方式
* */
function submitForm1(){
window.location.href="TestServlet?param=hrefMethod" rel="external nofollow" ;
}
/**第二種提交方式
* */
function submitForm2(){
var form=document.forms[0];
form.action="TestServlet?param=formMethod";
form.submit();
}
/**
*第三種提交方式
*/
var xmlHttp;
//創(chuàng)建xmlHttp
function createXMLHttpRequest(){
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xmlHttp=new XMLHttpRequest();
}else {// code for IE6, IE5
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
//Ajax使用get方式發(fā)送
function submitForm3(){
createXMLHttpRequest();
var queryString="TestServlet2?";
queryString=queryString+"¶m=" + new Date().getTime();
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.open("GET",queryString,true);
xmlHttp.send(null);
}
//Ajax使用post方式發(fā)送
function submitForm4(){
createXMLHttpRequest();
var url="TestServlet2?param=" + new Date().getTime();
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send("nihao");
}
function handleStateChange(){
if(xmlHttp.readyState==4){
//解析返回值
if(xmlHttp.status==200){
var responseText=document.createTextNode(xmlHttp.responseText);
alert("后臺返回的返回值: "+xmlHttp.responseText);
}
}
}
/**第五種方式 post提交
* @param to
* @param p
*/
function submitForm5() {
var myForm=document.createElement("form")
var params={"param":"zs","param2":"li"};
myForm.method = "post";
myForm.action = "TestServlet";
myForm.style.display = "none";
for ( var k in params) {
var myInput = document.createElement("input");
myInput.name= k;
myInput.value= params[k];
myForm.appendChild(myInput);
}
document.body.appendChild(myForm);
myForm.submit();
//document.body.removeChild(myForm);
return myForm;
}
jsp提交至servlet的6種方式:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- 方式四 -->
<!-- <meta http-equiv="refresh" content="0; url=TestServlet?param=方式四"> -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<!-- 方式一 -->
<%--
<%
RequestDispatcher rd = getServletContext().getRequestDispatcher("/TestServlet?param=方式一");
rd.forward(request, response);
%> --%>
<!-- 方式二 -->
<%-- <%
response.sendRedirect("TestServlet?param=方式二");
%> --%>
<!-- 方式三 -->
<%-- <jsp:forward page="TestServlet?param=方式3"/> --%>
<!-- 方式五 -->
<%-- <%
int stayTime=0;
String URL="TestServlet?param=Method 5";
String content=stayTime+";URL="+URL;
response.setHeader("REFRESH",content);
%> --%>
<!-- 方式六 -->
<%
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String newLocation = "TestServlet?param=Method 6";
response.setHeader("Location",newLocation);
%>
</body>
</html>
您可能感興趣的文章:
相關(guān)文章
Java實現(xiàn)多叉樹和二叉樹之間的互轉(zhuǎn)
本文主要介紹了Java實現(xiàn)多叉樹和二叉樹之間的互轉(zhuǎn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
Java Redis Template批量查詢指定鍵值對的實現(xiàn)
本文主要介紹了Java Redis Template批量查詢指定鍵值對的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Java使用Maven BOM統(tǒng)一管理版本號的實現(xiàn)
這篇文章主要介紹了Java使用Maven BOM統(tǒng)一管理版本號的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
JetBrains?產(chǎn)品輸入激活碼?Key?is?invalid?完美解決方案
JetBrains?系列產(chǎn)品(IDEA、Pycharm?等)使用本站破解教程?(opens?new?window),在輸入激活碼時,部分小伙伴反應(yīng)說提示?Key?is?invalid?無法激活,今天小編給大家分享完美解決方案,感興趣的朋友跟隨小編一起看看吧2022-11-11
Spring Boot中@ConditionalOnProperty的使用方法
這篇文章主要給大家介紹了關(guān)于Spring Boot中@ConditionalOnProperty的使用方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

