ajax的简单实现Demo

首先先声说抱歉,因为在寝室,所以效率低一些,就不写一些关于ajax的知识了,如果想学可以去w3school搜索ajax

 

这里主要实现了通过ajax来判断用户名是否已经被注册,为了实现简单后台的匹配直接使用了admin

一、前端实现

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




Insert title here




 姓名:
密码 :

二、Servlet

package com.ui.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class chechUsername
 */
@WebServlet("/checkUsername.do")
public class chechUsername extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public chechUsername() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
	     PrintWriter out=response.getWriter();
	     String username=request.getParameter("username");
	     System.out.println(username);
	     if("admin".equals(username))
	     {
	    	 out.print("用户名已经注册");
	     }
	     else 
	     {
	    	 out.print("用户名可以");
	     }
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

三、实现结果

ajax的简单实现Demo_第1张图片

ajax的简单实现Demo_第2张图片 

你可能感兴趣的:(java)