最近研究 jquery和ajax时发现一个让我很疑惑的问题,也就是servlet取值的问题,一般在java代码里面通过String name = request.getParameter("name"); 其中括号里面的那个name应该和前台页面的值对应,也就是input type="text" name = "name" 应该和name里面的值name相对应,可是我用jquery的时候,写了一个测试demo
测试ajax.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>itcast.cn用户校验的ajax</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript" src="jslib/jquery.js"></script>
<script type="text/javascript" src="jslib/verify.js">
</script>
</head>
<body>
用户名校验的ajax实例,请输入用户名:<br/>
<input type="text" id="userName"/>
<input type="button" value="校验" onclick="verify()"/>
<div id="result">
</div>
</body>
</html>
测试verify.js
function verify()
{
// document.getElementsByName("userName");
// document.getElementById("userName");
// alert("userName)");
// jquery的查找节点的值的方式
var jqueryObj = $("#userName");
var userName = jqueryObj.val();
// alert(userName);
$.get("AJAXServer?name=" +userName,null,callback);
}
function callback(data)
{
// alert("服务器端的数据回来了");
// alert(data);
var resultObj = $("#result");
resultObj.html(data);
alert("");
}
AjaxServlet.java
package cn.itcast.project;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
public class AjaxServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
try
{
// request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();