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

使用JAVAEE在调试器中显示响应后HTML页面,但在浏览器中未显示,java,angularjs,google-app-engine,servlets,Java,Angularjs,Google App Engine,Servlets,这就是问题所在 我有一个向servlet发送Post请求的连接表单。 这个servlet在执行一些测试(密码、用户的电子邮件验证)后将请求转发到不同的页面。 唯一的问题是,当查看我的POST请求时,我得到了正确的页面作为响应,但该页面没有显示在我的web浏览器中。为什么 这是我的密码 登录servlet package AHSServlets; import java.io.IOException; import objMetier.signInForm; import javax.serv

这就是问题所在 我有一个向servlet发送Post请求的连接表单。 这个servlet在执行一些测试(密码、用户的电子邮件验证)后将请求转发到不同的页面。 唯一的问题是,当查看我的
POST
请求时,我得到了正确的页面作为响应,但该页面没有显示在我的web浏览器中。为什么

这是我的密码

登录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);
        }
    }
}
signIn.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>
正如您在
signIn.jsp
中看到的,我有
${connect.error}
应该向我显示错误消息。我重复我自己的话,但我可以通过调试器看到此消息,但不能在我的web浏览器上看到。所以发生的事情是在
POST
之后,它停留在网页路径上,我只在响应中得到HTML

编辑

这是一个截图


非常感谢您提供的任何帮助。

好的,我不熟悉angular,但只了解ajax的响应,您需要处理如下响应:

将id添加到span元素(从中删除
${connect.error}
):

编辑:

这里的答案对于学习ajax和servlet非常有用:


好的,我对angular不太熟悉,但只看ajax的响应,您需要处理如下响应:

将id添加到span元素(从中删除
${connect.error}
):

编辑:

这里的答案对于学习ajax和servlet非常有用:



connect.error不会显示任何内容。除非您专门设置属性并将其转发到jsp。在你的角度部分,你没有做任何反应。换句话说,你误解了这个过程。您发出了一个ajax请求,但没有向servlet中的响应写入任何内容。(您也不会在js中处理响应)-如果您使用的是ajax,您不能只设置属性并期望它显示为这样。@JonathanLaliberte我可以在我的调试器中看到所需的结果,其中
connect.error
填充得很好。和
this.getServletContext().getRequestDispatcher(“/signIn.jsp”).forward(请求,响应)用于更新我的浏览器上的视图。但是它没有显示出来。它与来自表单的一个简单的
post
请求一起工作。我做错了什么?你能解释一下吗。非常感谢是的,就像我说的,你想提出一个ajax请求,对吗?你不是这样做的。我将在下面发布一个示例。如果我在没有角度的情况下向您展示它,可以吗?connect.error不会向您展示任何东西。除非您专门设置属性并将其转发到jsp。在你的角度部分,你没有做任何反应。换句话说,你误解了这个过程。您发出了一个ajax请求,但没有向servlet中的响应写入任何内容。(您也不会在js中处理响应)-如果您使用的是ajax,您不能只设置属性并期望它显示为这样。@JonathanLaliberte我可以在我的调试器中看到所需的结果,其中
connect.error
填充得很好。和
this.getServletContext().getRequestDispatcher(“/signIn.jsp”).forward(请求,响应)用于更新我的浏览器上的视图。但是它没有显示出来。它与来自表单的一个简单的
post
请求一起工作。我做错了什么?你能解释一下吗。非常感谢是的,就像我说的,你想提出一个ajax请求,对吗?你不是这样做的。我将在下面发布一个示例。如果我不带angular向您展示它,可以吗?我不想将您的connect变量替换为“hello world”,因为我不确定它是字符串。@Jonahtan Laliberte谢谢,我知道了,但是关于
this.getServletContext().getRequestDispatcher(“/signIn.jsp”).forward(请求,响应)为什么不能使用我的post方法?只有在不使用ajax的情况下才能使用requestDispatcher进行转发。如果使用ajax,只需写入响应即可。(无转发)介意在聊天室(在我帖子的评论中)上聊一会儿吗?会很快的,提前谢谢你?我不想把你的连接变量改为“hello world”,因为我不确定它是字符串。@Jonahtan Laliberte谢谢,我明白了,但是
this.getServletContext().getRequestDispatcher(“/signIn.jsp”)。转发(请求,响应);
为什么不能使用我的post方法?只有在不使用ajax的情况下才能使用requestDispatcher转发。如果使用ajax,只需写入响应即可。(无需转发)介意在聊天中聊一会儿(在我的帖子上发表评论)会很快的,提前谢谢?
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);
    }
}
 <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>
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.

}