javascript與jsp發送請求到servlet的幾種方式執行個體,javascriptservlet

來源:互聯網
上載者:User

javascript與jsp發送請求到servlet的幾種方式執行個體,javascriptservlet

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; //建立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方式發送 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方式發送 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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.