2014-12-05 83 views
1

我有一個jsp頁面,它接受用戶名和密碼,然後重定向到一個servlet。下面是JSP代碼,如何使用ajax通過servlet重定向到成功頁面?

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<head> 

    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <meta name="description" content=""> 
    <meta name="author" content=""> 

    <title>DeliverMe</title> 

    <!-- Bootstrap Core CSS --> 
    <link href="css/bootstrap.min.css" rel="stylesheet"> 

    <!-- Custom CSS --> 
    <link href="css/sb-admin.css" rel="stylesheet"> 

    <!-- Custom Fonts --> 
    <link href="font-awesome-4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css"> 

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --> 
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> 
    <!--[if lt IE 9]> 
     <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> 
     <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script> 
    <![endif]--> 

     <!-- jQuery Version 1.11.0 --> 
    <script src="js/jquery-1.11.0.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
    $('#btn-login').click(function() { 
     var userName = $('#userName').val(); 
     var password = $('#password').val(); 
     var datastr='userName='+userName+'&password='+ password; 
     $.ajax({ 
      url : 'LoginCheck', 
      data :datastr, 
      success : function(responseText) { 
       $('#ajaxGetUserServletResponse').text(responseText); 
      }, 
     error:function(url){ 
      window.location = url; 
      } 

     }); 
    }); 
}); 
</script> 
</head> 

</head> 
<body> 
    <div class="container">  
    <span style="color: #000099"><center><h2>DeliverMe Admin Panel</h2></center></span> 
     <div id="loginbox" style="margin-top:50px;" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">      
      <div class="panel panel-info" > 
        <div class="panel-heading"> 
         <div class="panel-title">Sign In</div> 

        </div>  

        <div style="padding-top:30px" class="panel-body" > 

         <div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div> 

         <form id="loginform" class="form-horizontal" role="form"> 

          <div style="margin-bottom: 25px" class="input-group"> 
             <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span> 
             <input id="userName" type="text" class="form-control" placeholder="username or email">           
            </div> 

          <div style="margin-bottom: 25px" class="input-group"> 
             <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span> 
             <input id="password" type="password" class="form-control" name="password" placeholder="password"> 
            </div> 


           <div style="margin-top:10px" class="form-group"> 
            <!-- Button --> 

            <div class="col-sm- 12 controls"> 
             <a id="btn-login" class="btn btn-success">Login</a> 


            </div> 
           </div> 
<span id="ajaxGetUserServletResponse" style="color: red;"></span> 
          </form>  


         </div>      
        </div> 
     </div> 

    </div> 

</body> 
</html> 

然後我的Servlet檢查用戶名和密碼,如果是錯誤的,那麼它給了錯誤的文字,但如果這是真的,那麼它應該被重定向到成功頁面。但它不起作用。下面是servlet代碼,

if(db.loginCheck(userName, password)) 
      { 
       request.getRequestDispatcher("/main.jsp").forward(request, response); 
      }else 
      { 
       String greetings = "Invalid Username/Password"; 
       response.setContentType("text/plain"); 
       response.getWriter().write(greetings); 

      } 
+0

嘗試'response.sendRedirect是(main.jsp中「);'或'刪除/ '。 – 2014-12-05 11:37:43

+0

是的,我已經使用了它,但它不工作,我已經嘗試過了,但它重定向到相同的頁面並顯示整個html代碼,像這樣<<!DOCTYPE html PUBLIC「 - // W3C // DTD HTML 4.01 Transitional // EN「」http://www.w3.org/TR/html4/loose.dtd「> 你好 '。 @ user3145373з – 2014-12-05 11:47:11

+0

您是否嘗試過'response.sendRedirect(main.jsp「);'? – 2014-12-05 11:48:16

回答

3

我得到了解決。我已經改變了我的servlet這樣的反應,

if(db.loginCheck(userName, password)) 
      { 
       response.getWriter().write("1"); 
      }else 
      { 
       greetings = "Invalid Username/Password"; 
       response.setContentType("text/plain"); 
       response.getWriter().write(greetings); 

      } 

和我的jsp頁面上我已經把這個,

<script type="text/javascript"> 
$(document).ready(function() { 
    $('#btn-login').click(function() { 
     var userName = $('#userName').val(); 
     var password = $('#password').val(); 
     var datastr='userName='+userName+'&password='+ password; 
     $.ajax({ 
      url : 'LoginCheck', 
      data :datastr, 
      success : function(responseText) { 
       if(responseText == "1"){ 
        window.location.assign("/main.jsp"); 
        }else{ 
         $('#ajaxGetUserServletResponse').text(responseText); 
        } 
      }, 

     }); 
    }); 
}); 
</script> 
0

試試這個:

的Servlet

if(db.loginCheck(userName, password)) 
{ 
    String greetings = "Success"; 
    response.setContentType("text/plain"); 
    response.getWriter().write(greetings); 
} 
else 
{ 
    String greetings = "Invalid Username/Password"; 
    response.setContentType("text/plain"); 
    response.getWriter().write(greetings); 

} 

的Jquery:

$.ajax({ 
     url : 'LoginCheck', 
     data :datastr, 
     success : function(responseText) { 
      if(responseText == "Success"){ 
      windows.location.href='/main.jsp'; 
      }else{ 
      windows.location.href='/error.jsp'; 
      }); 
+0

不,它重定向到相同的頁面,並給出了main.jsp的整個html代碼,<!DOCTYPE html PUBLIC」 - // W3C // DTD HTML 4.01 Transitional // EN「」http://www.w3.org/TR/html4/loose.dtd「>你好'@Sas – 2014-12-08 05:22:09

相關問題