使用ajax验证用户名是否重复

ajaxTest.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>


    
        
        JSP Page
    
    
    	输入用户名:光标移除检查是否重复
    	
显示结果

Servlet控制:

package 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 getServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("getservlet:doget");
		String username = request.getParameter("username");
		byte[] buf = username.getBytes("UTF-8");
		username = new String(buf, "UTF-8");
		String msg = null;
		if (username.equals("sa")) {
			msg = "该用户已经存在";

		} else {
			msg = "可以注册";
		}
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().write(msg);

	}

}

运行:

使用ajax验证用户名是否重复_第1张图片
使用ajax验证用户名是否重复_第2张图片

你可能感兴趣的:(Ajax)