简体   繁体   English

无法从另一个普通的Http Servlet访问过滤器Servlet的会话属性

[英]Cannot access filter Servlet's session attribute from another normal Http Servlet

I have a problem here: 我在这里有一个问题:

After i use a filter servlet to set session attribute, i try to retrieve the session attribute in another normal http servlet, but it looks getAttribute('system.userinfo') cannot retrieve anything. 在使用过滤器servlet设置会话属性之后,我尝试在另一个普通的http servlet中检索会话属性,但是它看起来getAttribute('system.userinfo')无法检索任何内容。 what's wrong with this? 这怎么了 Thanks! 谢谢!

public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {
    HttpServletRequest httpReq = (HttpServletRequest) request;
    HttpServletResponse httpResp = (HttpServletResponse) response;
    HttpSession session = httpReq.getSession();

    httpReq.setCharacterEncoding("UTF-8");

    UserDTO dto = new UserDTO();
    session.setAttribute("system.userinfo", dto);

    chain.doFilter(request, response);

}


public class FileUpload extends HttpServlet {
    @SuppressWarnings("unchecked")
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");

        // cannot get anything here
        UserDTO userinfo = (UserDTO)request.getSession(false).getAttribute("system.userinfo");


        }
}

Both servlets are in same web application. 两个servlet都在同一个Web应用程序中。

Seems like you are not getting the session in the servlet that you think got created in the Filter. 似乎您没有在您认为是在过滤器中创建的servlet中获得会话。 In the filter you are using req.getSession() which is always creating a new session. 在过滤器中,您使用的是req.getSession(),它始终在创建一个新会话。 In the servlet you are giving request.getSession(false), the container is supposed to return null if no session exists or return an existing session. 在servlet中,您要提供request.getSession(false),如果不存在会话,则容器应返回null或返回现有会话。 Which servlet container are you using? 您正在使用哪个servlet容器? If you are using an IDE, can you put a debug point and compare the session IDs to confirm they are the same? 如果使用的是IDE,是否可以放置调试点并比较会话ID以确认它们相同? Also, is your UserDTO serializable? 另外,您的UserDTO是否可序列化?

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

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