//ValidateAjax.java
package com.java.to.ajax;
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 ValidateAjax extends HttpServlet{
public ValidateAjax(){
super();
}
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
response.setContentType("text/html");
String LoginName= request.getParameter("loginName").toString();
if(LoginName.equals("java")){
PrintWriter out=response.getWriter();
out.write("true");
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
this.doGet(request, response);
}
}
//index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
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>
<script src="javascript/jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
function validateLogin(){
var loginName=$('#loginName').val();
$.ajax({
type:"post",
url:"ValidateAjax",
data:"loginName="+loginName,
success:function(data){
if(data=="true"){
alert("Java验证正确");
}else{
alert("填写的内容为空!!");
}
}
});
}
</script>
</head>
<body>
<form id="LoginForm" action="" method="post">
<input type="text" id="loginName" name="validateName" >
<input type="button" id="loginbutton" name="loginajax" value="点下看看" onclick="validateLogin();">
</form>
</body>
</html>
//web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- ValidateAjax 配置文件 -->
<servlet>
<description>this is ajax jsp</description>
<display-name>ajax java</display-name>
<servlet-name>ValidateAjax</servlet-name>
<servlet-class>com.java.to.ajax.ValidateAjax</servlet-class>
</servlet>
<!-- servlet映射文件 -->
<servlet-mapping>
<servlet-name>ValidateAjax</servlet-name>
<url-pattern>/ValidateAjax</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
package com.java.to.ajax;
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 ValidateAjax extends HttpServlet{
public ValidateAjax(){
super();
}
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
response.setContentType("text/html");
String LoginName= request.getParameter("loginName").toString();
if(LoginName.equals("java")){
PrintWriter out=response.getWriter();
out.write("true");
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
this.doGet(request, response);
}
}
//index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
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>
<script src="javascript/jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
function validateLogin(){
var loginName=$('#loginName').val();
$.ajax({
type:"post",
url:"ValidateAjax",
data:"loginName="+loginName,
success:function(data){
if(data=="true"){
alert("Java验证正确");
}else{
alert("填写的内容为空!!");
}
}
});
}
</script>
</head>
<body>
<form id="LoginForm" action="" method="post">
<input type="text" id="loginName" name="validateName" >
<input type="button" id="loginbutton" name="loginajax" value="点下看看" onclick="validateLogin();">
</form>
</body>
</html>
//web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- ValidateAjax 配置文件 -->
<servlet>
<description>this is ajax jsp</description>
<display-name>ajax java</display-name>
<servlet-name>ValidateAjax</servlet-name>
<servlet-class>com.java.to.ajax.ValidateAjax</servlet-class>
</servlet>
<!-- servlet映射文件 -->
<servlet-mapping>
<servlet-name>ValidateAjax</servlet-name>
<url-pattern>/ValidateAjax</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>