5 ways to submit JavaScript to servlet:
/**The first submission method* */function submitForm1(){ window.location.href="TestServlet?param=hrefMethod" rel="external nofollow" ;}/**The second submission method* */function submitForm2(){ var form=document.forms[0]; form.action="TestServlet?param=formMethod"; form.submit();}/** *The third submission method*/var xmlHttp; //Create 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 uses get to send function submitForm3(){ createXMLHttpRequest(); var queryString="TestServlet2?"; queryString=queryString+"¶m=" + new Date().getTime(); xmlHttp.onreadystatechange=handleStateChange; xmlHttp.open("GET",queryString,true); xmlHttp.send(null); } //Ajax uses post to send 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){ //Resolve the return value if(xmlHttp.status==200){ var responseText=document.createTextNode(xmlHttp.responseText); alert("Return value returned in the background: "+xmlHttp.responseText); } } } /**The fifth method post submission* @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;}6 ways to submit jsp to servlet:
<%@ 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><!-- Method 4--><!-- <meta http-equiv="refresh" content="0; url=TestServlet?param=Method 4"> --><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body><!-- Method 1--><%-- <% RequestDispatcher rd = getServletContext().getRequestDispatcher("/TestServlet?param=Method One"); rd.forward(request, response);%> --%><!-- Method 2--><%- <% response.sendRedirect("TestServlet?param=Method Two");%> --%><!-- Method 3--><%- <jsp:forward page="TestServlet?param=Method 3"/> --%><!--Method 5--> <%-- <%int stayTime=0;String URL="TestServlet?param=Method 5";String content=stayTime+";URL="+URL;response.setHeader("REFRESH",content);%> --%><!--Method 6--><% response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); String newLocation = "TestServlet?param=Method 6"; response.setHeader("Location",newLocation); %> </body></html>