0

document.location() function given in the logn.js code works fine in Internet Explorer but does not work in Firefox. The given js code is for implementing AJAX in the login page .. AJAX directs the code to a servlet which if login OK gives user Login as response.

logn.js

function logn(emailId,password) {
    var parameters="emailId="+emailId+"&password="+password;
    var xmlhttp;

    if (window.XMLHttpRequest) {    // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {    // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {     
            if(xmlhttp.responseText.toString()=="User Login") {     
                document.location("userhome.jsp"); 
            } else if(xmlhttp.responseText.toString()=="Admin Login") { 
                document.location("adminhome.jsp"); 
            }else {
                //document.getElementById("message").innerHTML = xmlhttp.responseText;
                alert(xmlhttp.responseText);
            }       
        }
    };

    xmlhttp.open("POST", "LoginServlet", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send(parameters);   
}

the following is the servlet code LoginServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out=response.getWriter();
        String emailId=request.getParameter("emailId");
        String password=request.getParameter("password");



        if (emailId.isEmpty()||password.isEmpty()) { 
            out.write("Please enter EmailId/Password");
        } else {
            LoginModel lm=new LoginModel();
            lm.setEmailId(emailId);
            lm.setPassword(password);

            LoginService ls=new LoginService();
            lm=(LoginModel) ls.loginCheck(lm);

            if(lm!=null){
                System.out.println("login ok");
                HttpSession session =request.getSession();
                System.out.println(lm.getLoginId());
                session.setAttribute("userlogin", lm);

                if (lm.getIsAdmin()==0) {
                    System.out.println("aaaaaaaaaaa");
                    out.write("User Login");
                }
                else if (lm.getIsAdmin()==1) 
                    out.write("Admin Login");

                ls.setIsActive(lm.getLoginId(),1);
            } else 
                out.write("Wrong EmailId/Password");
        }
    }
Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
StrikerVillain
  • 3,719
  • 2
  • 24
  • 41

2 Answers2

2

You should be using:

window.location = "userhome.jsp";

and

window.location = "adminhome.jsp";

There were a couple issues with how you were doing it. It's preferred to use window.location instead of document.location. And, you assign to it, not call it like a function.

MDN reference: https://developer.mozilla.org/en/DOM/window.location

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • What is the difference between window.location and document.location ? – AlanFoster Mar 04 '12 at 19:17
  • @AlanFoster - As you have discovered, there are cross browser compatibility issues with document.location and it is read-only in some browsers. See here for more info: http://stackoverflow.com/questions/2430936/whats-the-difference-between-window-location-and-document-location-in-javascrip – jfriend00 Mar 04 '12 at 19:22
1

instead of document.location() try:

document.location.href = ...
Tom
  • 4,096
  • 2
  • 24
  • 38