简体   繁体   English

Java Servlet和jQuery AJAX-无法从会话中检索对象

[英]Java Servlet & jQuery AJAX - unable to retrieve object from session

Not sure how to solve this, need some help here. 不确定如何解决此问题,请在此处寻求帮助。

Ajax call brings user information to servlet, I save user object in HttpSession and control goes back to Ajax from where i redirect control to next JSP page via controller servlet. Ajax调用将用户信息带到servlet,我将用户对象保存在HttpSession中,控制权返回到Ajax,从那里我通过控制器servlet将控制权重定向到下一个JSP页面。 However, if i try to retrieve object from HttpSession it is null .. not sure how to solve this issue. 但是,如果我尝试从HttpSession检索对象,则为null ..不知道如何解决此问题。

here is my code for firstservlet: 这是我的firstservlet代码:

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

    // get values from http request
    // persist "user" object to database 

    HttpSession session = request.getSession();    //
    session.setAttribute("user", user);            //setting session variable

        Gson gson = new Gson();
        JsonElement jsonElement = null;
        jsonElement = gson.toJsonTree("/nextservlet");
        response.setContentType("text/plain");
        PrintWriter out=response.getWriter();


}

here is my Javascript / AJAX code to redirect request to nextservlet 这是我的Javascript / AJAX代码,将请求重定向到nextservlet

    $.ajax({
                type: 'POST',
                url: ‘firstservlet’,       
                dataType: 'json',
                contentType: 'application/json',
                data: JSON.stringify(quiz),


                success: function(result) {
                  //result = /nextservlet 
                  window.location.href = result;


                },
                error:function(data,status,er) {
                    console.log("Error:",er);
                }
              });

and finally control comes to nextservlet - where i would like to process data and then show new JSP page. 最后,控制权转到nextservlet-我要在其中处理数据,然后显示新的JSP页面。

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

    response.setContentType("text/plain");
    response.setCharacterEncoding("UTF-8");

    HttpSession session = request.getSession();
    User user = session.getAttribute(“user”);        //<--- this is NULL

    LOG.warning("User id is  : " + user.getId());    //<--- hence error here

    RequestDispatcher dispatcher = request.getRequestDispatcher 
    ("/anotherpage.jsp");
    dispatcher.forward(request, response);

} }

is issue because i am using -> window.location.href = result to send request to nextservlet .. and it goes to doGet?? 是有问题的,因为我正在使用-> window.location.href =结果将请求发送到nextservlet ..并转到doGet?

I am not sure it but I see in Ajax 我不确定,但我在Ajax中看到了

url: ‘firstservlet’, 
type: 'POST'

and control goes to doGet method of nextservlet. 然后控制权转到nextservlet的doGet方法。 It should be nextservlet of post method so use method doPost. 它应该是post方法的nextservlet,所以请使用doPost方法。

18.12.22 18.12.22

i'm not sure... but try it 我不确定...但是尝试一下

success: function(result) {
    // result = /nextservlet
    var form = document.createElement('form');
    form.action = result;
    form.method = 'GET'
    form.submit();
}

18.12.26 18.12.26

Javascript Java脚本

$.ajax({
    type: 'POST',
    url: '/firstservlet',
    dataType: 'json',
    contentType: 'application/json',
    data: JSON.stringfy(quiz),
    success: function(result) {
        console.info(result);
        var form = document.createElement('form');
        form.action = result;
        form.method = 'GET';
        document.body.appendChild(form);
        form.submit();
    },
    error: function(data, status, err) {
        console.log("Error: ", err);
    }
});

Servlet Servlet

HttpSession session = request.getSession();
session.setAttribute("test", "test");

Gson gson = new Gson();
JsonElement jsonElement = gson.toJsonTree("/nextservlet");
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.print(gson.toJson(jsonElement));

It can read session attribute in doGet Method try it. 可以在doGet方法中尝试读取会话属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM