Angularjs发布servlet无法正常工作......!

时间:2013-11-07 05:35:45

标签: angularjs servlets

所以, 这是我的观点

<div>
    <div class="loginDiv">
        <form name="loginForm" ng-submit="validateDetails()" novalidate>
            <div class="userNameDiv">
                <label class="label" for="username">User Name:</label>
                <input type="text" id="username" name="userName" class="input-medium" ng-model="employee.userName" required ng-minlength=5 ng-maximumlength=20/>
            </div>
            <div class="passwordDiv">
                <label class="label" for="password">Password:</label>
                <input type="password" id="password" name="password" class="input-medium" ng-model="employee.password" required />
            </div>

            <button type="submit" class="btn-large">SUBMIT</button>
        </form>
    </div>
</div>

并且视图的控制器是

angular.module('employeeApp.controllers')
    .controller('LoginPageController',['$scope','authenticationDataModelService',function($scope,authenticationDataModelService)
    {

        $scope.validateDetails=function(){
            employee={};
            employee.userName=$scope.employee.userName;
            employee.password=$scope.employee.password;
            console.log(employee);
            authenticationDataModelService.validateData(employee);
        }
    }
    ]);

我将此员工对象发送到我在邮件请求中发送它的服务文件(不包括整个服务代码):

$http.post(this.employeeDataUrl,employee);

我将它发送到服务器,servlet代码是

public class LoginService extends HttpServlet{

    public void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        PrintWriter printWriter=response.getWriter();
        response.setContentType("text/html");
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        EmployeeDAO employee = new EmployeeDAO();
        System.out.println(userName+""+password);
        boolean validateLogin=employee.validateLogin(userName, password);
        if(validateLogin){
            printWriter.println("Login Success");
        }
        else{
            printWriter.println("Login Failed");
        }

    }
}

但我在这里得到空值。 我无法将angularjs中的服务文件中的employee对象中的值发送到servlet。我怎么能实现这个?请原谅我,如果问题很愚蠢......

1 个答案:

答案 0 :(得分:4)

AngularJS在JSON中发送您的“employee”对象,因此您无法使用您的代码在服务器端读取此对象。 您应该首先解析JSON对象。