javascript与jsp发送请求到servlet的方法有哪些

发布时间:2021-07-26 14:46:37 作者:小新
来源:亿速云 阅读:368

这篇文章将为大家详细讲解有关javascript与jsp发送请求到servlet的方法有哪些,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

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>

关于“javascript与jsp发送请求到servlet的方法有哪些”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. Jsp和Servlet有什么区别?
  2. servlet和jsp请求转发和重定向

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

javascript jsp servlet

上一篇:如何使用PHPMailer发送邮件

下一篇:Python中SMTP发送邮件遇到问题怎么办

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》