天天看点

如何简单实用ajax

鍦↗SP椤甸潰涓細

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

聽 <head>

聽 <meta http-equiv="content-type" content="text/html; charset=gbk">

聽聽聽 <title>璇锋敞鍐?lt;/title>

聽<link rel="stylesheet" type="text/css" href="style/style.css" target="_blank" rel="external nofollow" >

聽 <script type="text/javascript">

聽聽 var xhr=null;

聽聽聽

聽聽聽

聽 function checkUname(){

聽 var username=document.myform.uname.value;

聽 //alert(username);

聽 if(username==""||username==null){

聽聽聽 document.getElementById("msgDiv").innerHTML="<font color='red'>璇疯緭鍏ョ敤鎴峰悕</font>";

聽 }

聽 //ajax鐨勬牳蹇冨璞? 聽 //var xhr=new XMLHttpRequest();//鍙闈瀒e娴忚鍣ㄧ敓鏁? 聽聽

聽 if(window.ActiveXObject){

聽聽

聽聽聽 xhr=new ActiveXObject("Microsoft.XMLHTTP");

聽 }else{

聽聽聽聽 xhr=new XMLHttpRequest();

聽 }

聽 var url="http://localhost:8080/12softsms0415/RegServlet?uname="+username;

聽聽 //褰撴牳蹇冨璞$殑鐘舵€佸彂鐢熸敼鍙橈紝瀹冪殑澶勭悊鍑芥暟

聽 xhr.onreadystatechange=haolejiaowo;

聽 xhr.open("get",url,true);

聽 xhr.send(null);

聽聽

聽聽

聽 }

聽聽

聽 //鎵嶆槸鏍稿績澶勭悊鍑芥暟

聽 function haolejiaowo(){

聽if(xhr.readyState==4&&xhr.status==200){

聽聽 var result=xhr.responseText;//寰楀埌杩滅▼鏈嶅姟鍣ㄨ繑鍥炵殑缁撴灉

聽聽 if(result.indexOf("true")!=-1){

聽聽聽聽 document.getElementById("msgDiv").innerHTML="<font color='red'>鐢ㄦ埛鍚嶅凡瀛樺湪</font>";;

聽聽 }else{

聽聽聽聽 document.getElementById("msgDiv").innerHTML="鍙互娉ㄥ唽";

聽聽 }

聽}

聽聽

聽聽

聽 }

聽 </script>

聽聽

聽 </head>

聽 <body>

聽 <div align="center">

聽 聽<%--聽 鐧诲綍聽 --%>

聽聽

聽聽聽 <div class="login" align="center">

聽聽聽 聽<br/>

聽聽聽聽聽聽聽 <h2>璐﹀彿娉ㄥ唽</h2>

聽聽聽 聽<form action="UserServlet" method="post" name="myform">

聽聽聽 聽聽聽<input type="hidden" name="cmd" value="reg">

聽聽聽 聽聽鐢ㄦ埛鍚?lt;input type="text" name="uname" class="input" 慰nblur="checkUname()"/>

聽聽聽 聽聽<div id="msgDiv" style="display:inline"></div>

聽聽聽 聽聽<br/>

聽聽聽 聽聽瀵?amp;nbsp;&nbsp;鐮?lt;input type="password" name="upass" class="input"/><br/>

聽聽聽 聽聽閲嶅瀵嗙爜<input type="password" name="upassrep" class="input"/><br/>

聽聽聽 聽聽<input type="submit" value=" 娉ㄥ唽 " class="btn"/>

聽聽聽 聽</form>

聽聽聽 </div>

聽聽聽

聽聽

聽 </div>

聽 </body>

</html>

鍦╯rc鐩綍涓嬫柊寤轰竴涓猂egServlet椤甸潰鏉ュ垽鏂敤鎴峰悕鏄惁鏈夋敞鍐岋細

package com.neusoft.sms.servlet;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class RegServlet extends HttpServlet {

聽public boolean isExist(String username){

聽聽聽 //妯℃嫙鍚庡彴鏁版嵁搴撴湁涓変釜鐢ㄦ埛

聽聽String[] names={"jack","marry","ljh"};

聽聽for (String s : names) {

聽聽聽if(username.equalsIgnoreCase(s)){

聽聽聽聽return true;

聽聽聽}

聽聽}

聽聽return false;

聽}

聽public void doGet(HttpServletRequest request, HttpServletResponse response)

聽聽聽throws ServletException, IOException {

聽聽聽聽 try {

聽聽聽Thread.sleep(3000);

聽聽} catch (InterruptedException e) {

聽聽聽// TODO Auto-generated catch block

聽聽聽e.printStackTrace();

聽聽}

聽聽

聽聽response.setContentType("text/html;charset=gbk");

聽聽PrintWriter out = response.getWriter();

聽聽

聽聽String username=request.getParameter("uname");

聽聽boolean flag=isExist(username);

聽聽out.println(flag);

聽聽

聽聽out.flush();

聽聽out.close();

聽}

聽public void doPost(HttpServletRequest request, HttpServletResponse response)

聽聽聽throws ServletException, IOException {

聽聽聽聽聽聽聽聽聽聽聽聽 doGet(request,response);

聽}}

继续阅读