javascript與jsp發(fā)送請(qǐng)求到servlet的幾種方式實(shí)例
更新時(shí)間:2018年03月18日 17:47:11 投稿:wdc
本文分別給出了javascript發(fā)送請(qǐng)求到servlet的5種方式實(shí)例與
jsp發(fā)送請(qǐng)求到servlet的6種方式實(shí)例
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("后臺(tái)返回的返回值: "+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>
您可能感興趣的文章:
- 使用easyui從servlet傳遞json數(shù)據(jù)到前端頁(yè)面的兩種方法
- jsp+servlet實(shí)現(xiàn)猜數(shù)字游戲
- servlet+jsp實(shí)現(xiàn)過(guò)濾器 防止用戶未登錄訪問(wèn)
- Jsp servlet驗(yàn)證碼工具類分享
- jsp頁(yè)面中獲取servlet請(qǐng)求中的參數(shù)的辦法詳解
- Jsp+Servlet實(shí)現(xiàn)文件上傳下載 刪除上傳文件(三)
- Jsp+Servlet實(shí)現(xiàn)文件上傳下載 文件上傳(一)
- Java,JSP,Servlet獲取當(dāng)前工程路徑(絕對(duì)路徑)問(wèn)題解析
- Servlet返回的數(shù)據(jù)js解析2種方法
相關(guān)文章
Java實(shí)現(xiàn)多叉樹和二叉樹之間的互轉(zhuǎn)
本文主要介紹了Java實(shí)現(xiàn)多叉樹和二叉樹之間的互轉(zhuǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05Bean的自動(dòng)注入及循環(huán)依賴問(wèn)題
本文詳細(xì)介紹了Bean的自動(dòng)注入及循環(huán)依賴,文中通過(guò)代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)有一定的研究?jī)r(jià)值,感興趣的小伙伴可以閱讀參考2023-03-03Java Redis Template批量查詢指定鍵值對(duì)的實(shí)現(xiàn)
本文主要介紹了Java Redis Template批量查詢指定鍵值對(duì)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Java使用Maven BOM統(tǒng)一管理版本號(hào)的實(shí)現(xiàn)
這篇文章主要介紹了Java使用Maven BOM統(tǒng)一管理版本號(hào)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04idea報(bào)錯(cuò)之找不到符號(hào):類的問(wèn)題及解決
這篇文章主要介紹了idea報(bào)錯(cuò)之找不到符號(hào):類的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12JetBrains?產(chǎn)品輸入激活碼?Key?is?invalid?完美解決方案
JetBrains?系列產(chǎn)品(IDEA、Pycharm?等)使用本站破解教程?(opens?new?window),在輸入激活碼時(shí),部分小伙伴反應(yīng)說(shuō)提示?Key?is?invalid?無(wú)法激活,今天小編給大家分享完美解決方案,感興趣的朋友跟隨小編一起看看吧2022-11-11Spring Boot中@ConditionalOnProperty的使用方法
這篇文章主要給大家介紹了關(guān)于Spring Boot中@ConditionalOnProperty的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12