javascript与jsp发送请求到servlet的几种方式实例

作者:wdc 时间:2023-06-15 15:59:30 

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
0
投稿

猜你喜欢

  • 多按钮共存——don’t make me think

    2010-01-11 20:20:00
  • sql server对字段的添加修改删除、以及字段的说明

    2012-01-05 18:50:52
  • ASP真正随机不重复查询代码

    2010-01-02 20:40:00
  • CSS执行顺序与优先权的问题

    2010-08-23 16:21:00
  • asp实现在线人数统计代码

    2008-08-10 18:35:00
  • ASP Recordset 分页显示数据的方法(修正版)

    2011-04-10 10:42:00
  • 解决MySQL启动时1067错误

    2010-09-30 14:09:00
  • EXEC(EXECUTE)函数访问INSERTED或DELETED的内部临时触发表

    2012-01-29 18:07:30
  • ASP验证码的实现原理及源码

    2007-10-02 12:14:00
  • 兼容IE和FF的收藏本站、设为首页代码

    2009-01-07 14:14:00
  • class和id命名探讨

    2007-10-16 17:55:00
  • 交互设计的方法

    2010-08-18 12:32:00
  • HTML5 声明兼容IE的写法

    2011-06-06 10:34:00
  • 一个不错网速测试代码

    2008-07-20 13:41:00
  • MySQL错误中文参照列表

    2010-09-30 14:41:00
  • asp 数据库连接函数代码

    2011-04-04 11:08:00
  • ASP脚本变量、函数、过程和条件语句

    2008-10-14 14:43:00
  • php 一维数组的循环遍历实现代码

    2023-06-12 00:49:04
  • 去除DW MX 2004表格宽度辅助

    2010-09-02 12:37:00
  • 归纳万恶IE6的HACK方法

    2010-02-04 17:17:00
  • asp之家 网络编程 m.aspxhome.com