javascript与jsp发送请求到servlet的几种方式实例
程序员文章站
2023-01-15 08:24:52
javascript提交至servlet 5种方式:
/**第一种提交方式
* */
function submitform1(){
window...
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+"¶m=" + 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>
下一篇: Spring MVC全局异常实例详解