加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 19:57:52 所属栏目:Java 来源:网络整理
导读:JavaScript提交至servlet 5种方式: /**第一种提交方式 * */function submitForm1(){ window.location.href="TestServletparam=hrefMethod" rel="external nofollow" ;}/**第二种提交方式 * */function submitForm2(){ var form=document.forms[0]; form.act

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>

您可能感兴趣的文章:

  • jsp页面中获取servlet请求中的参数的办法详解
  • JavaWeb实现用户登录注册功能实例代码(基于Servlet+JSP+JavaBean模式)
  • Servlet+Jsp实现图片或文件的上传功能具体思路及代码
  • JSP+Servlet制作Java Web登录功能的全流程解析
  • JSP与Servlet的介绍说明
  • Servlet与JSP间的两种传值情况
  • jsp和servlet中实现页面跳转的方式实例总结
  • JSP+Servlet+JavaBean实现登录网页实例详解
  • 基于JSP HttpServlet的详细介绍
  • JSP、Servlet中get请求和post请求的区别总结
  • Servlet+JavaBean+JSP打造Java Web注册与登录功能
  • 基于jsp+servlet实现的简单博客系统实例(附源码)
  • jsp+servlet+javabean实现数据分页方法完整实例
  • jsp+servlet+jdbc实现对数据库的增删改查
  • 在jsp中用bean和servlet联合实现用户注册、登录
  • jsp和servlet操作mysql中文乱码问题的解决办法
  • JSP使用Servlet作为控制器实现MVC模式实例详解
  • 访问JSP文件或者Servlet文件时提示下载的解决方法
  • jsp引用servlet生成的验证码代码演示

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读