---
title: "一个使用Jquery编写的ajax样例，供大家参考"
author: "lijing727"
date: "2009-09-30T15:46:06Z"
tags: ["Ajax", "JavaScript", "Servlet", "jQuery", "json"]
slug: "jquery-ajax"
draft: false
lastmod: "2009-09-30T15:46:06Z"
id: 480875
url: "https://lijing727.iteye.com/blog/480875"
---

1、java代码

```
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;



public class LoginController extends AbstractController {

	@SuppressWarnings("unchecked")
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {


		Map paramMap = request.getParameterMap();

		String loginName = ((String[]) paramMap.get("loginName"))[0];

		String password = ((String[]) paramMap.get("password"))[0];


		
		UserDTO user = new UserDTO();
		
		user.setId("@^$&*!%jack");
		user.setName(loginName);
		user.setDescription("!@#$%^&*(dodo)");
		
		

		JSONObject jsonObject = new JSONObject();
		

		jsonObject.put("user", user);
		

		response.getOutputStream().write(jsonObject.toString().getBytes());

		return null;
	}

}
```

 

2、html代码

```
<html>
	<head>
		<title>just for test</title>
		<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
		<script type="text/javascript" src="js/jquery.js"></script> 
	</head>
	<body>
		<script type="text/javascript">
			$(document).ready(function(){
				alert("hello 大家哈！");
			});
		</script>
		<p>
			大家好！
		</p>
		
		<p>
			<input id="name" type="text" value=""/>
			<input id="password" type="text" value=""/>	
		</p>
		<p>
			<input id="submit" type="button" value="提交"/>
		</p>
		
			<table>
				
				<tr>
					<td>用户ID：</td>
					<td><input type="text" id="userId" value=""></td>
				</tr>
				
				<tr>
					<td>姓名：</td>
					<td><input type="text" id="userName" value=""></td>
				</tr>
				
				<tr>
					<td>描述：</td>
					<td><input type="text" id="userDes" value=""></td>
				</tr>
	
			</table>
			
			<p>
				<input id="getInfo" type="button" value="得到用户信息"/>
			</p>
		
		<script type="text/javascript">
			$("#submit").click(function(){
				$.ajax({
					url: 'login.do',
					data: {loginName:$('#name').val(), password:$('#password').val()},
					success: function(result){
						alert(result);
					}
				})
			});
			
			$("#getInfo").click(function(){
				$.post("login.do",
						{loginName:$('#name').val(),password:$('#password').val()},
						function(data){
							var jsData = eval('('+data+')');
							$("#userId").attr("value",jsData.user.id);
							$("#userName").attr("value",jsData.user.name);
							$("#userDes").attr("value",jsData.user.description);
						});
			});
		</script>
	</body>
</html>
```