Java Servlets无法在另一个servlet中使用session属性

时间:2014-04-24 13:51:54

标签: java servlets

我正在努力让我的第一个servlet工作。我发现了一些类似的问题和解决方案,但我不想做什么。

这是我的登录servlet:

public class LoginServlet extends HttpServlet {  

    private static final long serialVersionUID = 1L;

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

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        String username=request.getParameter("username");
        String password=request.getParameter("password");
        if(LoginValidator.validate(username, password)){
            HttpSession session = request.getSession();
            session.setAttribute("user", username);
            session.setMaxInactiveInterval(30*60);
            Cookie sessionCookie = new Cookie("sessionKuki", username);
            sessionCookie.setMaxAge(30*60);
            response.addCookie(sessionCookie);
            RequestDispatcher rd=request.getRequestDispatcher("paste.jsp"); //INSTEAD of paste.jsp I would like to get session attribute called uri I set in filter. BUT I when I try to use get attribute, Eclipse says there is no attribute called URI. 
            rd.forward(request,response);  
        }
        else{  
            out.print("Sorry username or password error");
            RequestDispatcher rd=request.getRequestDispatcher("login.html");
            rd.include(request,response);
        }

        out.close(); 
    }
}

当用户未登录时,我会使用过滤器重定向到登录页面:

public class SessionFilter implements Filter{

//  private ServletContext context;

    public void init(FilterConfig filterConfig) throws ServletException {
        //this.context = filterConfig.getServletContext();
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;

        String uri = req.getRequestURI(); //THERE IS uri of the site from where the user    gets redirected to login page

        HttpSession session = req.getSession(false);
        session.setAttribute("uri", uri); // HERE I TRY to set uri to session attribute. My intention is to use that uri in my login servlet


        if(uri.endsWith(".css")) {
            chain.doFilter(request, response);
            return;
        }

        if(uri.endsWith(".js")) {
            chain.doFilter(request, response);
            return;
        }


        if(session == null && !(uri.endsWith("login.html") || uri.endsWith("login") || uri.endsWith("forgot.jsp") || uri.endsWith("signup.jsp"))){
            res.sendRedirect("login.html");
            System.out.print("redirecting to login");
        }else{
            chain.doFilter(request, response);
        }

    }


    public void destroy() {
    }
}

甚至可能,我想做什么?怎么做?有没有更好的方法呢?我不想混合HTML和脚本。我的意图是,当用户访问某个页面并尝试访问某个地方时,他会被重定向到登录页面。登录后,他应该被重定向到他想要开始的页面。

1 个答案:

答案 0 :(得分:0)

不确定这是否有效,但请尝试按照以下方式进行过滤:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;

        String uri = req.getRequestURI();

        HttpSession currentSession = req.getSession(false);


        if(uri.endsWith(".css")) {
            chain.doFilter(request, response);
            return;
        }

        if(uri.endsWith(".js")) {
            chain.doFilter(request, response);
            return;
        }


        if(currentSession == null && !(uri.endsWith("login.html") || uri.endsWith("login") || uri.endsWith("forgot.jsp") || uri.endsWith("signup.jsp"))){
            HttpSession newSession = req.getSession();
            newSession.setAttribute("uri", uri);
            res.sendRedirect("login.html");
            System.out.print("redirecting to login");
        }else{
            chain.doFilter(request, response);
        }

    }

我所做的是如果session为空,则在过滤器中创建一个新会话。