2016-11-23 59 views
0

我正在尝试为我的网站设置一个登录页面,但是此刻我卡在sendRedirect方法中,因为它在需要时似乎不会加载新页面。response.sendRedirect在Java和AngularJS之间不起作用

我的登录页面是一个.jsp文件即会透过AngularJS $scope到servlet的用户名和密码信息,其中包含以下代码:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{ 
    System.out.println("Redirecting..."); 
    response.sendRedirect("https://www.youtube.com/"); 
    return; 
} 

System.out.println("Redirecting...");的作品,但它不重定向到提供的网址,不管它是什么。我已阅读其他建议,其中提到添加return;行,并且我尝试过其他URL专用于我的项目(例如index.jsp,\index.jsp等),但这些都没有改变,它仍然无法正常工作。

window.location会更合适吗?我应该在这里修改哪些代码?

+1

定义_doesn't WORK_。你为什么期望你的页面重定向,而不是通过AngularJS发送的请求?当您在浏览器的网络控制台中检查响应的状态代码时,它是什么? –

+0

我的意思是浏览器不会从登录页面重定向到我在'response.sendRedirect'方法中指定的内容(例如youtube.com,index.jsp等) – dat3450

+0

为什么要这样? –

回答

0

因此,我已经使网页最终重定向正确,我发布这只是为了防止别人在同一问题上绊倒。

正如在以前的评论中提到,响应应该从Java servlet来AngularJS发送,我已经做了重定向发生在我logincontroller.js文件如下:

var app = angular.module('myApp', []); 

function LoginController($scope, $http, $window) 
{ 
    $scope.login = function() 
    { 
     $http({ 
      url: 'login', 
      method: 'POST', 
      data: 
      { 
       'username': $scope.username, 
       'password': $scope.password 
      } 
      }).success(function(data, status, headers, config) { 
       alert("SUCCESS! Data: " + data); 
       $scope.details = data; 
       $window.location.href = "home.jsp"; //This line redirects. 
      }).error(function(data, status, headers, config) { 
       alert('ERROR!'); 
     }); 
    }; 
};