5 façons de soumettre JavaScript à Servlet:
/ ** la première méthode de soumission * * / fonction soumembal1 () {window.location.href = "testServlet? Param = hrefMethod" rel = "external nofollow";} / ** La deuxième méthode de soumission * * / fonction soumestorm2 () {var form = document.forms [0]; form.action = "testServlet? param = formMethod"; form.submit ();} / ** * La troisième méthode de soumission * / var xmlhttp; // Créer une fonction xmlhttp createExmlHttpRequest () {if (window.xmlhttpRequest) {// code pour ie7 +, firefox, chrome, opéra, safari xmlhttp = new xmlhttprequest (); } else {// code pour ie6, ie5 xmlhttp = new activeXObject ("Microsoft.xmlhttp"); }} // ajax utilise GET pour envoyer une fonction soumiseForm3 () {createExmlHttpRequest (); var querystring = "testServlet2?"; QueryString = QueryString + "¶m =" + new Date (). GetTime (); xmlhttp.onreadystateChange = handlestateChange; xmlhttp.open ("get", QueryString, true); xmlhttp.send (null); } // ajax utilise la publication pour envoyer une fonction soumiseForm4 () {createExmlHttpRequest (); var url = "testServlet2? Param =" + new Date (). getTime (); xmlhttp.open ("Post", URL, true); xmlhttp.onreadystateChange = handlestateChange; xmlhttp.setRequestHeader ("contenu-type", "application / x-www-form-urlencoded"); xmlhttp.send ("nihao");} fonction handleStateChange () {if (xmlhttp.readystate == 4) {// Résolve la valeur de retour if (xmlhttp.status == 200) {var responsableXT = document.creatExtNode (xmlhttp.ResponSeXT); alert ("la valeur de retour renvoyée en arrière-plan:" + xmlhttp.ResponSeText); }}} / ** La cinquième méthode Post soumission * @param à * @param p * / fonction soumeForm5 () {var myform = document.createelement ("form") var params = {"param": "zs", "param2": "li"}; myform.method = "post"; myform.action = "testServlet"; myform.style.display = "aucun"; pour (var k dans params) {var myInput = document.CreateElement ("Input"); myInput.name = k; myInput.Value = params [k]; Myform.ApendChild (MyInput); } document.body.appendChild (myform); MyForm.Submit (); //Document.body.removechild(Myform); renvoie ma forme;}6 façons de soumettre JSP au 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><!-- Méthode 4 -> <! - <meta http-equiv =" Refresh "content =" 0; url = testServlet? Param = méthode 4 "> -> <méta http-equiv =" Contenu-Type "Content =" Texl; charset = utf-8 "> <ititle> insérer le titre ici </ title> </ad> <body> <! - Méthode 1 -> <% - <% requestDispatcher rd = getServletContext (). getRequestDispatcher (" / testServlet? Param = méthode one "); Rd.Forward (demande, réponse);%> -%> <! - Méthode 2 -> <% - <% Response.SenDredirect ("TestServlet? Param = Method Two");%> -%> <! - Method 3 -> <% - <JSP: Forward Page = "TestServlet? Param = Method 3" /> -%> <! - Méthode = <% - <% intte 5 "; String Content = StayTime +"; url = "+ url; réponse.sethEader (" Refresh ", contenu);%> -%> <! - Méthode 6 -> <% Response.SetStatus (httpSservletResponse.sc_moved_permanly); String newLocation = "TestServlet? Param = Method 6"; Response.Setheader ("Location", NewLocation); %> </ body> </html>