簡體   English   中英

Java Servlet無法在另一個Servlet中使用會話屬性

[英]Java Servlets cant use session attribute in another servlet

我正在嘗試使我的第一個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和腳本。 我的意圖是,當用戶進入頁面並嘗試訪問某處時,他將被重定向到登錄頁面。 在登錄后,應將其重定向到開始時要轉到的頁面。

不知道這是否行得通,但請嘗試按以下方式進行過濾:

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為空,我所做的就是在過濾器中創建一個新會話。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM