用ajax实现判断用户名是否存在功能

servlet代码:

package svt;

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;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;

import dao.TeacherDao;
import entity.Teacher;

public class CheckUserNameServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");//输出的内容,主要确定编码格式
		PrintWriter out=response.getWriter();
		HttpSession session=request.getSession();
		PageContext pageContext=JspFactory.getDefaultFactory().getPageContext(this, request, response, null, true, 8192, true);
		String username=request.getParameter("name");
		TeacherDao tcd = new TeacherDao();
		System.out.println("-------------"+username);
		Teacher tch = tcd.checkname(username);
		System.out.println("-------------"+tch);
		if(tch==null){
			out.println("1");
		}else{
			out.println("0");
		}
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}

}

 jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


	
		
		
		

	
	
		姓名:
密码:

效果如下:

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(html,java,个人经验,练习)