3

I am developing an application using jquery along with servlets.I have been using jquery theme roller for interface In my Login.jsp

<html>
<head>
<script>
$(document).ready(function() {
$("#dialog").dialog();
}); 
 </script>

  <script>
    $("#submit").click(function(){
    $("#LoginForm").submit(function()
    {
     var username=$("#username").val();
     var password=$("#password").val();
     var datastring='username='+username+ '&password= '+password;

   $.ajax({
   type: "POST",
   url:'Login',
   data: datastring
   error: function(){
            alert("Data Error"); 
        },
        success: function (data){
            window.location.replace("Items.jsp");
        }
     });
    });
});
  </script>

</head>
 <body style="font-size:62.5%;">
<div id="dialog" title="Login">
<form id="LoginForm" method="post">
<fieldset>
<label>Username:</label>
<input type="text" id="username"></input><br></br>
<label>Password:</label>
<input type="password" id="pwd"></input><br></br>
<input type="submit" id="submit" value="Log In" align="middle"></input>
</fieldset>
</form>
</div>

The data is being passed to the servlet and once the login is successful i want the user to be redirect to a new page say Items.jsp. In my callback I have used window.location.replace("Items.jsp").I am not able to redirect.

I tried with response.sendRedirect("Items.jsp") and return *"Items.jsp" *

But am Not able to Redirect it to a new page.Where I am goin wrong..

Thanks:)

4 Answers4

2

Have you tried window.location = "items.jsp" ?

EDIT

I see you are missing a "url:" parameter in your ajax call.

Look at my example here which works.

http://jsfiddle.net/zuSZg/

netbrain
  • 9,194
  • 6
  • 42
  • 68
  • @netbrain-yes I tried window.location..But I stay in the same page inspite of loggin in –  May 04 '11 at 10:01
  • Then there must be something wrong with the way you retrieve the redirect value and set it. – netbrain May 04 '11 at 10:03
  • 1
    @netbrain...In my servlet I just check for authentication and then I want to redirect to new page on success...can it be done without callback by using response.sendRedirect in my servlet itself?? –  May 04 '11 at 10:34
  • @netbrain-I deleted url while editing..Its included in my code –  May 04 '11 at 10:43
  • 1
    No it cant' you have to make the browser redirect with the main page. – netbrain May 04 '11 at 11:13
  • @bumblebee hi, I m trying to do the same ...making a jquery Ajax call from login.jsp and calling the servlet. From the servlet however I wanted to redirect to welcome.sp but the welcome.jsp doesn't render. No errors either. Any help would be great! – raikumardipak Jun 15 '17 at 06:17
  • Saw your comment now. So you can't redirect an Ajax call from the servlet to a new jsp. Am I right? – raikumardipak Jun 15 '17 at 06:18
1

Setting window.location to new URL will change the current webpage to the specified location.

window.location = "http://www.stackoverflow.com/"
suren
  • 969
  • 4
  • 22
0

Try window.location.href ="Items.jsp"

Source: window.location MDC

naveen
  • 53,448
  • 46
  • 161
  • 251
  • :) i saw your answer only after I edited my answer It was after reading MDC. `window.location.href` should also work. I will revert my answer – naveen May 04 '11 at 10:02
  • 1
    @naveen...In my servlet I just check for authentication and then I want to redirect to new page on success...can it be done without callback by using response.sendRedirect in my servlet itself?? I pass the credentials in my servlet to the login method where checking is done and the user is directed to new page.Is call back necessary in this case? –  May 04 '11 at 10:38
  • @Sadhwika Chandramohan: its better to do the redirect on server. client side code can be easily manipulated. In your case we dont need an ajax call just post the form and redirect if credentials are good – naveen May 04 '11 at 11:12
  • @naveen.. Oh you mean to say that I can use the normal post method to pass the data to servlet and redirect to new page from servlket only...Thanks...Am a beginner to both java and jquery..thanks:) –  May 04 '11 at 11:42
0
Use this :
 $(window.location).attr('href', 'http://www.google.com');
Suhail
  • 24
  • 3