<%@ page contentType="text/html;charset=utf-8" %>
<html>
<SCRIPT LANGUAGE="JavaScript"> var  xmlHttp;
    function createRequest() {  
          try {  
           xmlHttp = new XMLHttpRequest();  
          } catch (trymicrosoft) {  
            try {  
              xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");  
            } catch (othermicrosoft) {  
              try {  
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  
              } catch (failed) {  
                xmlHttp = false;  
              }  
            }  
          }  
            if (!xmlHttp)  
             alert("Error initializing XMLHttpRequest!");  
        }  
function login(){
var uid = document.loginForm.uid.value;
var password = document.loginForm.password.value;
 createRequest(); 
var url =
"servlet/LoginServlet?uid="+uid+"&password="+password;
xmlHttp.open("post",url,true);
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState == 4){
resultDiv.innerHTML = xmlHttp.responseText;
}
else{
// document.writeln(xmlHttp.readyState);
resultDiv.innerHTML += "赈灾登陆中......"+xmlHttp.readyState;
// alert(xmlHttp.responseText);
}
}

xmlHttp.send();
}
</SCRIPT>  <head>
  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>用户登录</title> 
    <meta http-equiv="expires" content="0">
    <style type="text/css">
<!--
.style1 {color: #0000CC;font-size: 16px;font-weight: bold;}
-->
    </style>
  </head>  
  <body>
  <div id="resultDiv">
   <form name="loginForm" >
    <table width="274"  border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#CCCCCC" height="107">
      <caption>
      <span class="style1">      登 录      </span><br>
      </caption>
      <tr align="left">
        <th width="40%" height="35" align="center" scope="row">用户名:</th>
        <td width="60%"><input n ame="uid" type="text" id="uid" maxlength="20"></td>
      </tr>
      
      <tr align="left">
        <th height="35" align="center" scope="row">密&nbsp;&nbsp;码:</th>
        <td><input name="password" type="password" id="password" maxlength="30"></td>
      </tr>
  
      <tr align="center">
        <th height="35" colspan="2" scope="row"><input type="button" onclick="login()" value="登录">
      </tr>
    </table>
    </form>
      </div>
  </body>
</html></html>  import java.io.IOException;
import java.io.PrintWriter;import javax.faces.application.Application;
import javax.security.auth.login.AccountException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet { /**
 * Constructor of the object.
 */
public LoginServlet() {
super();
} /**
 * The doGet method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to get.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println("  <BODY>");
out.print("    This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println("  </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} /**
 * The doPost method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to post.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String uid = request.getParameter("uid");
String password = request.getParameter("password");
String loginStateString = "fail";
String targeturlString = "/loginFail.jsp";
if(uid.equals(password)){
loginStateString ="Success";
targeturlString = "/loginSuccess.jsp";
HttpSession session =request.getSession();
session.setAttribute("uid", uid);
}

ServletContext application = this.getServletContext(); RequestDispatcher rd =application.getRequestDispatcher(targeturlString);
rd.forward(request, response);
}}
readystate一直为1
求大神!!

解决方案 »

  1.   

    没看出什么大问题。。是不是你网速慢,一直在载入中??既然参数都放在url后面,用get发送就好了吧,没有参数send的时候加null,还有引用控件不用直接使用id,要不firefox出错。function login(){
        var uid = document.loginForm.uid.value;
        var password = document.loginForm.password.value;
         createRequest();    
        var url =
                "servlet/LoginServlet?uid="+uid+"&password="+password+'&_dc='+new Date().getTime();
        xmlHttp.open("get",url,true);
        xmlHttp.onreadystatechange=function(){
                if(xmlHttp.readyState == 4){
                    resultDiv.innerHTML = xmlHttp.responseText;
            }
            else{
            //  document.writeln(xmlHttp.readyState);////注意不要调用document.write,要不页面当前内容会被覆盖掉导致代码不继续执行
                //resultDiv.innerHTML += "赈灾登陆中......"+xmlHttp.readyState;
    document.getElementById('resultDiv').innerHTML += "赈灾登陆中......"+xmlHttp.readyState;
            //  alert(xmlHttp.responseText);
            }
        }
         
        xmlHttp.send(null);
    }
      

  2.   

    大神 为什么get就好了??????????????????????
      

  3.   

    我调了好几天。。为什么一个get就好了
      

  4.   

    你改为post,send的时候加null试试,一般post,get都没什么问题的,可能是send的时候少了null存在一些bug什么的,一般标准的写法就是没有参数时使用send(null),而不是send()不加参数
      

  5.   

    调试的时候post还是能传进来参数的....但是readystate还是1
      

  6.   


    post的时候将参数放到send中试试。
    function login(){
        var uid = document.loginForm.uid.value;
        var password = document.loginForm.password.value;
         createRequest();    
        var url ="servlet/LoginServlet";
        xmlHttp.open("post",url,true);
    //post要设置响应头
    xmlHttp.setRequestHeader("content-type","application/x-www-form-urlencoded")
        xmlHttp.onreadystatechange=function(){
                if(xmlHttp.readyState == 4){
                    resultDiv.innerHTML = xmlHttp.responseText;
            }
            else{
            //  document.writeln(xmlHttp.readyState);////注意不要调用document.write,要不页面当前内容会被覆盖掉导致代码不继续执行
                //resultDiv.innerHTML += "赈灾登陆中......"+xmlHttp.readyState;
    document.getElementById('resultDiv').innerHTML += "赈灾登陆中......"+xmlHttp.readyState;
            //  alert(xmlHttp.responseText);
            }
        }
          
        xmlHttp.send("uid="+uid+"&password="+password);
    }或者多加个时间戳防止缓存,照理post应该是没有缓存的。
    function login(){
        var uid = document.loginForm.uid.value;
        var password = document.loginForm.password.value;
         createRequest();    
        var url ="servlet/LoginServlet?uid="+uid+"&password="+password+'&_dc='+new Date().getTime();
        xmlHttp.open("post",url,true);
    xmlHttp.setRequestHeader("content-type","application/x-www-form-urlencoded")
        xmlHttp.onreadystatechange=function(){
                if(xmlHttp.readyState == 4){
                    resultDiv.innerHTML = xmlHttp.responseText;
            }
            else{
            //  document.writeln(xmlHttp.readyState);////注意不要调用document.write,要不页面当前内容会被覆盖掉导致代码不继续执行
                //resultDiv.innerHTML += "赈灾登陆中......"+xmlHttp.readyState;
    document.getElementById('resultDiv').innerHTML += "赈灾登陆中......"+xmlHttp.readyState;
            //  alert(xmlHttp.responseText);
            }
        }
          
        xmlHttp.send(null);
    }