title: how to redirect to success page through servlet using ajax?
body: <p>I have a jsp page which takes user name and password and then it redirect to a servlet. Below is jsp code,</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;head&gt;
  
      &lt;meta charset="utf-8"&gt;
      &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
      &lt;meta name="description" content=""&gt;
      &lt;meta name="author" content=""&gt;
  
      &lt;title&gt;DeliverMe&lt;/title&gt;
  
      &lt;!-- Bootstrap Core CSS --&gt;
      &lt;link href="css/bootstrap.min.css" rel="stylesheet"&gt;
  
      &lt;!-- Custom CSS --&gt;
      &lt;link href="css/sb-admin.css" rel="stylesheet"&gt;
  
      &lt;!-- Custom Fonts --&gt;
      &lt;link href="font-awesome-4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css"&gt;
  
      &lt;!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --&gt;
      &lt;!-- WARNING: Respond.js doesn't work if you view the page via file:// --&gt;
      &lt;!--[if lt IE 9]&gt;
          &lt;script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"&gt;&lt;/script&gt;
          &lt;script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"&gt;&lt;/script&gt;
      &lt;![endif]--&gt;
  
          &lt;!-- jQuery Version 1.11.0 --&gt;
      &lt;script src="js/jquery-1.11.0.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  $(document).ready(function() {
      $('#btn-login').click(function() {
          var userName = $('#userName').val();
          var password = $('#password').val();
          var datastr='userName='+userName+'&amp;password='+ password;
          $.ajax({
              url : 'LoginCheck',
              data :datastr,
              success : function(responseText) {
                  $('#ajaxGetUserServletResponse').text(responseText);
              },
          error:function(url){
              window.location = url;
            }  
  
          });
      });
  });
  &lt;/script&gt;
  &lt;/head&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div class="container"&gt;    
    &lt;span style="color: #000099"&gt;&lt;center&gt;&lt;h2&gt;DeliverMe Admin Panel&lt;/h2&gt;&lt;/center&gt;&lt;/span&gt;
          &lt;div id="loginbox" style="margin-top:50px;" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2"&gt;                    
              &lt;div class="panel panel-info" &gt;
                      &lt;div class="panel-heading"&gt;
                          &lt;div class="panel-title"&gt;Sign In&lt;/div&gt;
  
                      &lt;/div&gt;     
  
                      &lt;div style="padding-top:30px" class="panel-body" &gt;
  
                          &lt;div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"&gt;&lt;/div&gt;
  
                          &lt;form id="loginform" class="form-horizontal" role="form"&gt;
  
                              &lt;div style="margin-bottom: 25px" class="input-group"&gt;
                                          &lt;span class="input-group-addon"&gt;&lt;i class="glyphicon glyphicon-user"&gt;&lt;/i&gt;&lt;/span&gt;
                                          &lt;input id="userName" type="text" class="form-control" placeholder="username or email"&gt;                                        
                                      &lt;/div&gt;
  
                              &lt;div style="margin-bottom: 25px" class="input-group"&gt;
                                          &lt;span class="input-group-addon"&gt;&lt;i class="glyphicon glyphicon-lock"&gt;&lt;/i&gt;&lt;/span&gt;
                                          &lt;input id="password" type="password" class="form-control" name="password" placeholder="password"&gt;
                                      &lt;/div&gt;
  
  
                                  &lt;div style="margin-top:10px" class="form-group"&gt;
                                      &lt;!-- Button --&gt;
  
                                      &lt;div class="col-sm- 12 controls"&gt;
                                        &lt;a id="btn-login"  class="btn btn-success"&gt;Login&lt;/a&gt;
  
  
                                      &lt;/div&gt;
                                  &lt;/div&gt;
  &lt;span id="ajaxGetUserServletResponse" style="color: red;"&gt;&lt;/span&gt;
                              &lt;/form&gt;     
  
  
                          &lt;/div&gt;                     
                      &lt;/div&gt;  
          &lt;/div&gt;
  
      &lt;/div&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Then my servlet checks user name and password and if it is wrong then it gives error text but if it is true then it should be redirect to success page. But it doesn't effect. Below is servlet code,</p>
  
  <pre><code>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);
  
              }
  </code></pre>