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

2025-05-29 0 20

JavaScript提交至servlet 5种方式:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84
/**第一种提交方式

* */

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种方式:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45
<%@ 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>

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 javascript与jsp发送请求到servlet的几种方式实例 https://www.kuaiidc.com/112490.html

相关文章

发表评论
暂无评论