给你个例子,用得servlet,你可以改改
index.html
第1个ajax例子 web.xml
ReturnName
ajax.servlet.ReturnName
ReturnName
/ajax
ReturnName.java
package ajax.servlet;
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 ReturnName extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name=request.getParameter("name");
int a=0;
if(name.equals("asdasd")){
a=1;
}else if(!name.matches("[a-zA-Z0-9]")){
a=2;
}else{
a=3;
}
response.getWriter().print(a);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}