代码之家  ›  专栏  ›  技术社区  ›  JSmith

在调试器中显示响应后的HTML页面,但在使用JAVAEE的浏览器中不显示

  •  0
  • JSmith  · 技术社区  · 7 年前

    我有一个连接表单向servlet发送Post请求。 这个servlet在完成一些测试(密码、用户的电子邮件验证)后将请求转发到不同的页面。 唯一的问题是我得到正确的页面作为回应时,我看我的 POST

    登录servlet

    package AHSServlets;
    
    import java.io.IOException;
    
    import objMetier.signInForm;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    import AHSbeans.User;
    
    @WebServlet(
        name = "signInServ",
        urlPatterns = {"/"}
    )
    public class signInServ extends HttpServlet{
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            this.getServletContext().getRequestDispatcher("/signIn.jsp").forward(request, response);;
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            signInForm connect = new signInForm();
            User user = connect.validateSignIn(request);
            HttpSession session = request.getSession();
            if (user != null) {
                request.setAttribute("connect", connect);
                session.setAttribute("sessionUser", user);
                this.getServletContext().getRequestDispatcher("/restrict_client/client.jsp").forward(request, response);
            }
            else
            {
             request.setAttribute("connect", connect);
             this.getServletContext().getRequestDispatcher("/signIn.jsp").forward(request, response);
            }
        }
    }
    

    登录.jsp

    <%@ page pageEncoding="UTF-8" %>
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8" />
            <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js" integrity="sha256-QRJz3b0/ZZC4ilKmBRRjY0MgnVhQ+RR1tpWLYaRRjSo=" crossorigin="anonymous"></script>
        </head>
        <body ng-App="AHS" ng-controller="myCtrl">
            <form ng-submit="submitForm()">
                <div>
                    <input type="text" ng-model="userObj.mail" placeholder="email"/>
                </div>
                <div>
                    <input type="password" ng-model="userObj.password" placeholder="password">
                </div>
                <div>
                    <button type="submit">Connect</button>
                </div>
            </form>
            <div>
                <span>${connect.error}</span>
            </div>
            <script type="text/javascript">
                var app = angular.module("AHS", []);
                app.controller("myCtrl", function ($scope, $http, $httpParamSerializerJQLike){
                    $scope.userObj = {
                            mail: "",
                            password: "",
                    }
                    $scope.submitForm = function() {
                          $http({
                             method : 'POST',
                             url : '/', // use relative
                             data: $httpParamSerializerJQLike($scope.userObj),
                             headers: {
                               'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8;'
                             }
                          });
                        };
                });
            </script>
        </body>
    </html>
    

    package objMetier;
    import javax.servlet.http.HttpServletRequest;
    
    import AHSbeans.User;
    
    public class signInForm {
        private String EMAIL_FIELD = "email";
        private String PASSWORD_FIELD = "password";
        private String error = "Default";
    
        public String getError()
        {
            return (this.error);
        }
        public User validateSignIn(HttpServletRequest request){
            if(request.getParameter("password").isEmpty())
            {
                this.error = "wrong password";
                return (null);
            }
            User user = new User();
            user.setEmail("test@hotmail.com");
            this.error = "connected";
            return (user);
        }
    }
    

    signIn.jsp 我有 ${connect.error} 应该给我看错误信息。我重复一遍,但我可以看到这个消息显示使用调试器,但不是在我的网页浏览器。所以接下来发生的事 岗位 它停留在网页路径上,我只在响应中得到HTML。

    这是一个截图

    view on debugger

    1 回复  |  直到 7 年前
        1
  •  2
  •   Jonathan Laliberte    7 年前

    好吧,我不太熟悉角度,只是看看 w3 对于ajax,您需要像这样处理响应:

    向span元素添加id(删除 ${connect.error}

     <span id="somediv"></span>
    

    添加一个,然后单击“成功”:

       <script type="text/javascript">
            var app = angular.module("AHS", []);
            app.controller("myCtrl", function ($scope, $http, $httpParamSerializerJQLike){
                $scope.userObj = {
                        mail: "",
                        password: "",
                }
                $scope.submitForm = function() {
                      $http({
                         method : 'POST',
                         url : '/', // use relative
                         data: $httpParamSerializerJQLike($scope.userObj),
                         headers: {
                           'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8;'
                         }
                      }).then(function mySuccess(response) {
                          //handle response here
                          console.log(response.data);
                          document.getElementById("somediv").innerHTML = response.data;
                      });
                    };
            });
        </script>
    

    那么对于servlet:

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        signInForm connect = new signInForm();
        User user = connect.validateSignIn(request);
        HttpSession session = request.getSession();
    
        if (user != null) {
           // request.setAttribute("connect", connect); //what the hell is connect supposed to be here? 
            session.setAttribute("sessionUser", user);
           // this.getServletContext().getRequestDispatcher("/restrict_client/client.jsp").forward(request, response); //you do not do this in ajax requests.
        }else{
        //request.setAttribute("connect", connect);
         //this.getServletContext().getRequestDispatcher("/signIn.jsp").forward(request, response);
        }
        response.setContentType("text/plain");  // Set content type 
        response.setCharacterEncoding("UTF-8"); 
        response.getWriter().write("hello world");       // Write response body.
    
    }
    

    编辑:

    这里的答案非常适合学习ajax和servlet:

    How to use Servlets and Ajax?