Ajax 入门例子

来源:互联网 发布:犀牛软件破解 编辑:程序博客网 时间:2024/03/29 21:30

1.ajaxIndex.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><html>  <head>  <script type="text/javascript">//声明全局变量var xmlHttpRequest;//创建一个xmlHttpRequest对象function createXmlHttpRequest(){if(window.ActiveXObject){try{xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");}catch(e){ xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");}//return xmlHttpRequest;}else if(window.XMLHttpRequest){xmlHttpRequest =  new XMLHttpRequest();}return xmlHttpRequest;}//点击按钮响应的事件function checkExist(){//1.创建xmlHttpRequest对象xmlHttpRequest = createXmlHttpRequest();//2.设置回调函数xmlHttpRequest.onreadystatechange=haolejiaowo;//指定处理请求的对象var url = "http://localhost:8080${pageContext.request.contextPath}/UserExistServlet?userName=" +document.getElementById("userName").value;//3.初始化xmlHttpRequest组件xmlHttpRequest.open("get",url,true);//4.发送请求xmlHttpRequest.send(null);}/*xmlHttpRequest.readystate的状态每改变一次,就调用一次回调函数0--未初始化1--初始化2--发送请求3--开始接受结果4--接受结果完毕200 -- ok404 --没有找到资源500 -- 服务器端出错,也就是你访问的页面有语法错误*///每当组件的状态改变的时候,都会调用一次下面的回调函数function haolejiaowo(){//readyState是组件状态的信息    statue:是服务器返回的状态信息if(xmlHttpRequest.readyState==4&&xmlHttpRequest.status==200){  var msg = xmlHttpRequest.responseText;//alert(msg);if(msg=="true"){alert("该用户名已经存在");}else{alert("恭喜你,该用户名不存在");}}else if(xmlHttpRequest.status==404){alert('请求的页面不存在');}else if(xmlHttpRequest.status==505){alert('你请求的页面有语法错误');}}  </script>  </head>   <body>  <body>  <form name="registerUserForm" id="registerUserForm" action="UserExistServlet" method="post">  <table align="center">  <caption>注册用户</caption>  <tr>  <td>用户名:</td>  <td>  <input name="userName" type="text" value="${name}"/>  <input type="button" value="exist" onclick="checkExist()">  </td>  </tr>  <tr>  <td>地址:</td>  <td><input name="add" type="text" value="${param.add}"/></td>  </tr>  <tr>  <td>电话:</td>  <td><input name="tel" type="text" value="${param.tel}"></td>  </tr>  <tr>  <td>年龄;</td>  <td><input name="age"  type="text" value="${param.age}"></td>  </tr>  <tr>  <td colspan="2">  <input type="submit" value="提交"><input type="reset" value="重置"/>  </td>  </tr>  </table>  </form>  </body></html>


2.web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  <servlet>    <servlet-name>UserExistServlet</servlet-name>    <servlet-class>UserExistServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>UserExistServlet</servlet-name>    <url-pattern>/UserExistServlet</url-pattern>  </servlet-mapping>    <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>


3.UserExistServlet

import java.io.IOException;import java.io.PrintWriter;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;public class UserExistServlet extends HttpServlet {public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//接收页面请求过来的参数//response.setContentType("text/html");//response.setCharacterEncoding("gb2312");String userName = request.getParameter("userName");//try {//Thread.sleep(2000);//} catch (InterruptedException e) {//// TODO Auto-generated catch block//e.printStackTrace();//}boolean flag = users.contains(userName);//request.setAttribute("exist", flag);//request.setAttribute("name", userName);System.out.println("ajax");//System.out.println(userName);System.out.println(flag);response.getWriter().print(flag);//request.getRequestDispatcher("ajaxIndex.jsp").forward(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doGet(request, response);}private List<String> users;public void init() throws ServletException {users = new ArrayList<String>();users.add("zhangsan");users.add("lishi");users.add("wangwu");}}





原创粉丝点击