var xmlHttp;
function linkServer(){
聽聽聽 xmlHttp=GetXmlHttpObject();
聽聽聽 if (xmlHttp==null){
聽聽聽聽 alert ("Your browser does not support AJAX!");
聽聽聽聽 return;
聽聽聽聽 }
聽聽聽 var url="login"+"?"+"username="+document.getElementById("username").value+
聽聽聽 "&"+"password="+document.getElementById("password").value;
聽聽聽 xmlHttp.open("GET",url,true);
聽聽聽 xmlHttp.onreadystatechange=stateChanged;
聽聽聽 xmlHttp.send(null);
}
function stateChanged() {
聽 if (xmlHttp.readyState==4){
聽 alert("xmlState:"+xmlHttp.status);
聽聽 alert("xmlHttp.responseText:"+xmlHttp.responseText);
聽聽聽聽 if(xmlHttp.responseText=="DONE"){
聽聽聽聽 document.getElementById("user").innerHTML="(鐧誨綍鎴愬姛)";
聽 }
else
聽聽聽 document.getElementById("user").innerHTML="(鐢ㄦ埛鍚嶆垨瀵嗙爜閿欒)";
}
}
//鑾峰緱HTTP瀵硅薄
function GetXmlHttpObject(){
聽 var xmlHttp=null;
聽 try{
聽聽聽聽 xmlHttp=new XMLHttpRequest();
聽聽聽聽聽 }catch (e){
聽聽聽聽聽聽 try{
聽聽聽聽 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
聽聽聽聽聽聽聽 }catch (e)
聽聽聽聽聽聽 {
聽聽聽聽 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
聽聽聽 }
聽 }
return xmlHttp;
}
package com.service;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Login extends HttpServlet {
聽聽聽 private static final String CONTENT_TYPE = "text/html; charset=GBK";
聽聽聽 //Process the HTTP Get request
聽聽聽 public void doGet(HttpServletRequest request, HttpServletResponse response) throws
聽聽聽聽聽聽聽聽聽聽聽 ServletException, IOException {
聽聽聽聽聽聽聽 String u1 = request.getParameter("username");
聽聽聽聽聽聽聽 String p1 = request.getParameter("password");
聽聽聽聽聽聽聽 System.out.println("u1:" + u1);
聽聽聽聽聽聽聽 System.out.println("u2:" + p1);
聽聽聽聽聽聽聽 response.setContentType(CONTENT_TYPE);
聽聽聽聽聽聽聽 PrintWriter out = response.getWriter();
//涓嬮潰灝卞彲浠ユ暟鎹簱鏌ヨ錛岃繖閲岀畝鍗曠殑浣滀釜鍒ゆ柇
聽聽聽聽聽聽聽 if((u1!=null&&u1.equals("hello"))&&((p1!=null&&p1.equals("123456"))))
聽聽聽聽聽聽聽 out.print("DONE");
聽聽聽 else{
聽聽聽聽聽聽聽 out.print("FAIL");
聽聽聽 }
聽聽聽聽聽聽聽 out.close();
聽聽聽 }
聽聽聽 //Process the HTTP Post request
聽聽聽 public void doPost(HttpServletRequest request, HttpServletResponse response) throws
聽聽聽聽聽聽聽聽聽聽聽 ServletException, IOException {
聽聽聽聽聽聽聽 doGet(request, response);
聽聽聽 }
}