0

我正在尝试在显示 jsp 之前为登录的用户在一些 servlet 和 jsp 之间添加一个过滤器。问题是我正在尝试检索转发到登录页面的浏览器的 URL,但我找不到方法。我试图在过滤器处获取 URI 并将其作为请求的属性发送到Connexion servlet,但是它不起作用。

我的过滤器()

package com.forum.filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class RestrictionFilter implements Filter {
    public static final String CONNEXION = "/WEB-INF/connexion.jsp";
    public static final String ATT_USER = "utilisateur";
    public static final String CHEMIN = "chemin";

    public void destroy() {

    }

    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        String chemin = request.getRequestURI().substring(
                request.getContextPath().length());

        HttpSession session = request.getSession();

        if (session.getAttribute(ATT_USER) == null) {
            request.setAttribute(CHEMIN, chemin);
            request.getRequestDispatcher(CONNEXION).forward(request, response);
        } else {
            chain.doFilter(request, response);
        }
    }

    public void init(FilterConfig fConfig) throws ServletException {

    }

}

我的Connexion servlet

package com.forum.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.forum.beans.Utilisateur;
import com.forum.form.ConnexionForm;

public class Connexion extends HttpServlet {
    public static final String ATT_USER = "utilisateur";
    public static final String ATT_FORM = "form";
    public static final String VUE = "/WEB-INF/connexion.jsp";

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.getServletContext().getRequestDispatcher(VUE)
                .forward(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ConnexionForm form = new ConnexionForm();
        String chemin = (String) request.getAttribute("chemin");

        Utilisateur utilisateur = form.connecterUtilisateur(request);

        HttpSession session = request.getSession();

        if (form.getErreurs().isEmpty()) {
            session.setAttribute(ATT_USER, utilisateur);
            if (chemin == null || chemin.startsWith("/connexion"))
                response.sendRedirect("/projetForum/forum");
            else
                response.sendRedirect("/projetForum" + chemin);
        } else {
            request.setAttribute(ATT_USER, null);
            request.setAttribute(ATT_FORM, form);
            this.getServletContext().getRequestDispatcher(VUE)
                    .forward(request, response);
        }
    }
}

web.xml的一些标签

 <servlet>
 <servlet-name>Connexion</servlet-name>
 <servlet-class>com.forum.servlets.Connexion</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>Connexion</servlet-name>
 <url-pattern>/connexion</url-pattern>
</servlet-mapping>
<filter>
     <filter-name>RestrictionFilter</filter-name>
     <filter-class>com.forum.filters.RestrictionFilter</filter-class>
</filter>
<filter-mapping>
     <filter-name>RestrictionFilter</filter-name>
     <servlet-name>Nouveau</servlet-name>
     <servlet-name>Profil</servlet-name>
     <servlet-name>Editer</servlet-name>
     <servlet-name>Repondre</servlet-name>
</filter-mapping>
4

1 回答 1

0

我从您的过滤器中看到,request.setAttribute(CHEMIN, chemin);在您将其发送到登录页面之前,您已经存储了引用 URL。在这种情况下,在您的登录 jsp(我假设这是 /WEB-INF/connexion.jsp)上,您可以将其作为表单隐藏字段插入:

<form ...>
  <input type="hidden" name="referrer" value="<%= request.getAttribute("chemin") %>"/>
   ...
</form>

提交表单后,您可以通过 'referrer' 请求参数检索引荐来源网址

于 2013-08-21T00:57:22.463 回答