获取浏览器的URL

时间:2013-08-20 23:46:38

标签: jsp java-ee servlets servlet-filters

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

我的 doFilter()

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>

1 个答案:

答案 0 :(得分:0)

我从您的过滤器中看到,您已经使用request.setAttribute(CHEMIN, chemin);存储了引荐来源网址,然后再将其发送到登录页面。在这种情况下,在您的登录jsp(我假设这是/WEB-INF/connexion.jsp),您只需将其作为表单隐藏字段插入:

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

提交表单后,您可以通过“referrer”请求参数

检索引荐来源网址