溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

javascript與jsp發(fā)送請(qǐng)求到servlet的方法有哪些

發(fā)布時(shí)間:2021-07-26 14:46:37 來(lái)源:億速云 閱讀:380 作者:小新 欄目:編程語(yǔ)言

這篇文章將為大家詳細(xì)講解有關(guān)javascript與jsp發(fā)送請(qǐng)求到servlet的方法有哪些,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

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+"&param=" + 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>

關(guān)于“javascript與jsp發(fā)送請(qǐng)求到servlet的方法有哪些”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI