0

I am creating a web portal using Angular on the front end and Java Servlets in the back end. The page starts with a login page, It takes the input and sends it to the servlet. The server validates and responds with a JSON object which has the username and his permissions. I set the values to a Service which is injected into the Controller. Then I use $windows.location to change the web page to the home page which is the dashboard. Now I want to use this Service in the controller of the homepage. But I am not able to maintain the $scope due to the page change.

So I thought of redirecting to the home page in the backend using response.redirect() . But I don't know how to get the user details in the homepage after redirection. Like how do I pass the User object to the Home.java servlet

This is my LoginCtrl.js

if (isValid) {
            $http({
                method : 'POST',
                url : 'login',
                data : JSON.stringify($scope.user),
                headers : {
                    'Content-Type' : 'application/json'
                }
            }).success(function(data) {
                if (!("failure" == data)) {
                    console.log(data);
                    var user = {};
                    user.name = data.name;
                    user.permissions = data.permissions;
                    MyService.setUser(user); // set the values for user

                    $window.location.href = 'main.jsp';
                } else {
                    $scope.information = "Invalid username/password!"
                }
            }).error(function(data) {
                console.log(data);
            });

This is my Login.java servlet

protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        Gson gson = new Gson();
        JsonParser parser = new JsonParser();
        JsonObject obj = (JsonObject) parser.parse(request.getReader());

        String username = "";
        String password = "";

        if (obj.get("name") != null & obj.get("password") != null) {
            username = obj.get("name").getAsString();
            password = obj.get("password").getAsString();
        }

        System.out.println("Username :" + username);
        System.out.println("Password :" + password);

        // Passing username and password to Context and validate.
        // If authentication successful, set user object with details and return
        // it
        // User user = (User) Context.Authorized(username,password)

        // for testing
        User user = new User();
        user.setName(username);
        user.setPermission("crwd");
        user.setAuthorized(true);

        response.setContentType("text/html");
        if (user.getAthorized()) {
            String responseJSON = gson.toJson(user);
            response.getWriter().write(responseJSON);
        } else {
            response.getWriter().write("failure");
        }
    }

Please tell me if my requirement can be achieved in Angular or if it can be done using Java, then how ?

v1shnu
  • 2,211
  • 8
  • 39
  • 68
  • You can use local storage to save data. Once user log in, save the response in the browser's local storage, then this data will be available throughout your site regardless of redirection. – Saurabh Palatkar Jul 20 '15 at 11:15
  • Is local storage in Angular or using Java? Can you give me a keyword to search for in google? – v1shnu Jul 20 '15 at 11:16
  • you can maintain the information in $rootScope rather than $scope.It will be available throughout the application – Mohit Jul 20 '15 at 11:16
  • but after redirecting the page, will the service state still be present in the $rootScope – v1shnu Jul 20 '15 at 11:18
  • Its plain js, look at this: to set: localStorage.setItem("FName", User.FName); And to get value: var FName= localStorage.getItem("FName"); – Saurabh Palatkar Jul 20 '15 at 11:19
  • Use an angular router and don't use `$window.location` it will load a new page and app will bootstrap again. Instead change url within the angular app. – charlietfl Jul 20 '15 at 11:30
  • @SaurabhLprocks yes it works. But is this a good practise ? Is there any other way ? – v1shnu Jul 20 '15 at 11:36
  • @charlietfl I have used angular routing in the home page. The login page contains only a form for username n password. The homepage contains a navbar below which I have used ngView to load the different pages. So it is the first page that needed help. – v1shnu Jul 20 '15 at 11:37
  • There are numerous other ways. Read some tutorials on this. There are lots of them available. – charlietfl Jul 20 '15 at 11:37
  • Can you please tell me what 'this' means ? I am ready to learn anything to get my requirement working :) – v1shnu Jul 20 '15 at 11:38
  • Angular authentication ... or Single page app authentication – charlietfl Jul 20 '15 at 11:39
  • I've gone thru few tutorials but all of them suggest to use routing. But my problem is that I will be using routing only in the homepage. Anyways will take a look at them as well. Thanks. – v1shnu Jul 20 '15 at 11:42
  • @SaurabhLprocks The problem with this method is it maintains the data in the localStorage even after I close the tab and open it again . But I don't want this to happen. I want it to erase the data even when the tab is closed . – v1shnu Jul 20 '15 at 11:44
  • @ViChU-If you try to clear the local storage on tab close and if your site is opened in two or more tabs then doing so will create problem. Follow this link for more info: http://stackoverflow.com/questions/9943220/how-to-delete-a-localstorage-item-when-the-browser-window-tab-is-closed – Saurabh Palatkar Jul 20 '15 at 12:18

1 Answers1

0

If you redirect to another page, there will be completely different Angular application, and you will lose service state.

  • You can make single page application and use $routeProvider and ng-view for both login page and homepage navbar.

  • Or your app can consist of different pages, but then in every page you must call server to get user info

Yurii K
  • 162
  • 1
  • 5