我们都知道,在注册页面时,常常会用到无页面刷新的数据交互,那么就要用到ajax技术,这里我们用jquery的ajax来进行一个简单的交互
首先建立一个jsp页面:
代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>Insert title here
还要导入一个包:jquery-1.4.2.min.js自己到百度上一搜一下就可以啦
Servlet端代码:
package hrm.ajax;import java.io.IOException;import java.util.ArrayList;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class ajaxJquery */public class ajaxJquery extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name=request.getParameter("username"); List list=new ArrayList(); list.add("admin"); list.add("user"); if(list.contains(name)){ response.getWriter().write("no"); }else{ response.getWriter().write("yes"); } }}
别忘记在web.xml中要注册servlet
这里:
ajaxJquery ajaxJquery ajax.ajaxJquery ajaxJquery /ajaxJquery
ok!