这里用一个实例演示Ajax发送get请求,实例具体要求为一个注册页面,当用户填写完用户名称时,该输入框失去焦点后会通过Ajax向后台发送验证信息,如果用户名不是admin则通过验证,否则不通过验证。
servlet>
servlet-name>LoginServlet/servlet-name>
servlet-class>login.LoginServlet/servlet-class>
/servlet>
servlet-mapping>
servlet-name>LoginServlet/servlet-name>
url-pattern>/servlet/LoginServlet/url-pattern>
/servlet-mapping>
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charaet=UTF-8");
PrintWriter out = response.getWriter();
String name=request.getParameter("username");
System.out.println(name);
if(name.equals("admin"))
out.print(false);
else
out.print(true);
out.flush();
out.close();
}