Ajax的get和post方法

AjaxAction.java

package action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class AjaxAction extends ActionSupport {

	private String name;
	private String pass;
	// 接受值在返回
	public void ajax1() throws Exception {
		System.out.println("=+++++++++++++++++++++");

		System.out.println("名字" + name + "密碼" + pass);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");

		response.setCharacterEncoding("UTF-8");

		PrintWriter out = response.getWriter();
		out.write("nija,你好" + name + pass);
		out.flush();
		out.close();
	}
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}
}

Struts.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">


<struts>
  <action name="ajax1" class="action.AjaxAction" method="ajax1">
        
        </action>
    </package> 
 

</struts>

Text1.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<script type="text/javascript">
	//1.創建對象
	var xmlhttp = null;

	function createXMLHttp() {
		if (window.ActiveXObject) {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} else if (window.XMLHttpRequest) {
			xmlhttp = new XMLHttpRequest();
		}
		
	}
	//得到請求參數
	function queryString(){
		var name=document.getElementById("name").value;
		var pass=document.getElementById("pass").value;
		alert(name+pass);
		var queryStr="name="+name+"&pass="+pass;
		//編碼問題的解決***************
		return encodeURI(queryStr);
	}
	function doRequestUsingGET() {
		createXMLHttp();
		//2.建立請求
       alert("---------------");
		xmlhttp.open("get", "http://localhost:8080/helloAjax/ajax1?"+queryString(), true, "",
				"");
		//3.接受相應
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.status == 200 && xmlhttp.readyState == 4) {
				var test = xmlhttp.responseText;
				 var div=document.getElementById("serverResponse");
				var node=document.createTextNode(test);
				div.appendChild(node); 
			    alert(test);
			}
		};

		//4.發送請求
		xmlhttp.send("");
	}
	
	
	function doRequestUsingPOST() {

		createXMLHttp();
		//2.建立請求
		xmlhttp.open("post", "http://localhost:8080/helloAjax/ajax1", true, "",
				"");
		//3.接受相應
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.status == 200 && xmlhttp.readyState == 4) {
				var test = xmlhttp.responseText;
				var div=document.getElementById("serverResponse");
				var node=document.createTextNode(test);
				div.appendChild(node);
			}
		};

		//4.發送請求
		xmlhttp.send(queryString());
	}	
</script>
</head>

<body>
	<h2>輸入姓名和密碼</h2>
	<form>
		<input type="text" id="name" /><br> 
		<input type="password" 	id="pass" />
	</form>
	<form>
		<input type="button" value="GET" οnclick="doRequestUsingGET();" /><br>
		<input type="button" value="POST" οnclick="doRequestUsingPOST();" />
	</form>
	<div id="serverResponse"></div>
</body>
</html>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章