码迷,mamicode.com
首页 > Web开发 > 详细

XML&AJAX

时间:2016-04-27 20:42:38      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

AJAX: Asynchronous Javascript and XML

1. 客户端触发异步操作

2. 创建新的XMLHttpRequest, 是重要的js对象,通过它提起对服务器端的请求

3. 与server进行连接

4. 服务器端进行了连接处理

5. 返回包含处理结果的XML文档

6. XMLHttpRequest对象接收处理结果并分析

7. 更新页面

index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!doctype html>
<html>
<head>
	<title>www.mldnjava.cn,MLDN高端Java培训</title>
	<script language="javascript">
		var xmlHttp ;
		var flag = false ;
		function createXMLHttp(){
			if(window.XMLHttpRequest){
				xmlHttp = new XMLHttpRequest() ;
			} else {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") ;
			}
		}
		function checkUserid(userid){
			createXMLHttp() ;
			xmlHttp.open("POST","CheckServlet?userid="+userid) ;
			xmlHttp.onreadystatechange = checkUseridCallback ;
			xmlHttp.send(null) ;
			document.getElementById("msg").innerHTML = "正在验证..." ;
		}
		function checkUseridCallback(){
			if(xmlHttp.readyState == 4){				
				if(xmlHttp.status == 200){				
					var text = xmlHttp.responseText ;
					if(text == "true"){	// 用户id已经存在了
						document.getElementById("msg").innerHTML = "用户ID重复,无法使用!" ;
						flag = false ;
					} else {
						document.getElementById("msg").innerHTML = "此用户ID可以注册!" ;
						flag = true ;
					}
				}
			}
		}
		function checkForm(){
			return flag ;
		}
	</script>
</head>
<body>
<form action="regist.jsp" method="post" onsubmit="return checkForm()">
	用户ID:<input type="text" name="userid" onblur="checkUserid(this.value)"><span id="msg"></span><br>
	姓  名:<input type="text" name="name"><br>
	密  码:<input type="password" name="password"><br>
	<input type="submit" value="注册">
	<input type="reset" value="重置">
</form>
</body>
</html>

web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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"
   version="2.5">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
	<servlet>
		<servlet-name>CheckServlet</servlet-name>
		<servlet-class>org.lxh.ajaxdemo.CheckServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>CheckServlet</servlet-name>
		<url-pattern>/CheckServlet</url-pattern>
	</servlet-mapping>
	
	<welcome-file-list>
    	<welcome-file>index.jsp</welcome-file>
 	</welcome-file-list>
</web-app>

CheckServlet.java:

package org.lxh.ajaxdemo ;
import java.sql.* ;
import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
public class CheckServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
	public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
 	public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;
	public static final String DBUSER = "root" ;
	public static final String DBPASS = "linda0213" ;
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		this.doPost(request,response) ;
	}
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		request.setCharacterEncoding("GBK") ;
		response.setContentType("text/html") ;
		Connection conn = null ;
		PreparedStatement pstmt = null ;
		ResultSet rs = null ;
		PrintWriter out = response.getWriter() ;
		String userid = request.getParameter("userid") ;
		try{
			Class.forName(DBDRIVER) ;
			conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
			String sql = "SELECT COUNT(userid) FROM user WHERE userid=?" ;
			System.out.println(sql);
			pstmt = conn.prepareStatement(sql) ;
			pstmt.setString(1,userid) ;
			rs = pstmt.executeQuery() ;
			if(rs.next()){
				if(rs.getInt(1)>0){	// 用户ID已经存在了
					out.print("true") ;
				} else {
					out.print("false") ;
				}
			}
		}catch(Exception e){
			e.printStackTrace() ;
		}finally{
			try{
				conn.close() ;
			}catch(Exception e){}
		}
	}
}

regist.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>Regist OK</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    OK! <br>
  </body>
</html>

  

 

  

XML&AJAX

标签:

原文地址:http://www.cnblogs.com/wujixing/p/5440115.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!