index.jsp
[html]
view plaincopy<%@ 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" src="jquery-easyui-1.2.3/json.js"></script>
<script type="text/javascript">
//創(chuàng)建XMLHttpRequeset對象,這個對象是ajax的核心對象
function createXMLHttp(){
var xmlhttp;
try{
xmlhttp=new XMLHttpRequest();
}catch(e) {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
//使用Ajax
function ajaxSubmit(){
var xmlhttp=createXMLHttp();
xmlhttp.onreadystatechange=function(){
if(4==xmlhttp.readyState){
if(200==xmlhttp.status){
//獲取從服務器端傳過來的Text文本
alert(xmlhttp.responseText)
//獲取從服務器端傳過來的Text文本,并使用JSON的parseJSON函數(shù)將文本轉(zhuǎn)換為javascript對象
var myobj = xmlhttp.responseText.parseJSON();
//遍歷javascript對象
for (i = 0; i < myobj.programmers.length; i++) {
alert(myobj.programmers[i].lastName);
}
}else {
alert("no");
}
}
}
//使用標準json格式數(shù)據(jù),創(chuàng)建javascript對象
var people = { "programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "brett@newInstance.com" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "jason@servlets.com" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "elharo@macfaq.com" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
], "musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
]
}
//使用使用JSON的toJSONString函數(shù)將javascript對象轉(zhuǎn)換為文本
var value = people.toJSONString();
//象服務器端傳文本
xmlhttp.open("post","UserServlet?json="+value,true);
xmlhttp.send(null);
return null;
}
</script>
</head>
<body>
<form action="UserServlet" method="post">
<input type="text" name="username" />
<input type="button" onclick="ajaxSubmit();"value="提交"/>
</form>
</body>
</html>
UserActionServlet.java
[java]
view plaincopypackage action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UserActionServlet extends HttpServlet{
@Override
public void service(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
//System.out.println(arg0.getParameter("json"));
PrintWriter writer = arg1.getWriter();
//將從javascript傳過來的文本讀取到,然后在將文本傳回到客戶端
writer.print(arg0.getParameter("json"));
writer.flush();
writer.close();
}
}
接下來配置web.xml文件
[html]
view plaincopy<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>action.UserActionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/UserServlet</url-pattern>
</servlet-mapping>
</web-app>