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)文章
圖解Eclipse j2ee開發(fā)環(huán)境的搭建過程
這篇文章以圖文結(jié)合的方式介紹了Eclipse j2ee開發(fā)環(huán)境的搭建過程,內(nèi)容很詳細,每一個步驟都有對應(yīng)的操作截圖,需要的朋友可以參考下2015-08-08
Mybatis Plus select 實現(xiàn)只查詢部分字段
這篇文章主要介紹了Mybatis Plus select 實現(xiàn)只查詢部分字段的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
Java基礎(chǔ)鞏固小項目點菜系統(tǒng)的實現(xiàn)
這篇文章主要介紹了一個Java小項目點菜系統(tǒng)的實現(xiàn),主要是用的集合,適合正在學(xué)習(xí)Java的朋友拿來實戰(zhàn)練手,感興趣的朋友快來看看吧2022-03-03
深入學(xué)習(xí)Java同步機制中的底層實現(xiàn)
在多線程編程中我們會遇到很多需要使用線程同步機制去解決的并發(fā)問題,這些同步機制是如何實現(xiàn)的呢?下面和小編來一起學(xué)習(xí)吧2019-05-05
淺談SpringBoot如何封裝統(tǒng)一響應(yīng)體
今天帶各位小伙伴學(xué)習(xí)SpringBoot如何封裝統(tǒng)一響應(yīng)體,文中有非常詳細的介紹及代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-05-05
java中線程的sleep()方法和yield()方法的區(qū)別
本文主要介紹了java中線程的sleep()方法和yield()方法的區(qū)別,Thread類的sleep()方法使線程休眠指定時間,不釋放鎖,而yield()提示調(diào)度器當(dāng)前線程愿意讓出CPU資源,不保證立即切換線程,感興趣的可以了解一下2024-10-10

