3 Replies Latest reply on May 10, 2006 4:27 AM by js8523

    HttpServletRequest from Seam


      Is it possible to directly access the HttpServletRequest in a bean.

      For example I am now using a custom filter to add the follwing items to the request so that I can access them from Seam Components.

      This currently works fine, but I am wondering if I can do away with this filter.

      package pipetracker.web.servlet;
      import java.io.IOException;
      import javax.servlet.Filter;
      import javax.servlet.FilterChain;
      import javax.servlet.FilterConfig;
      import javax.servlet.ServletContext;
      import javax.servlet.ServletException;
      import javax.servlet.ServletRequest;
      import javax.servlet.ServletResponse;
      import javax.servlet.http.HttpServletRequest;
      import org.apache.commons.logging.Log;
      import org.apache.commons.logging.LogFactory;
      public class ExposeHttpServletRequestFilter implements Filter
       private static final Log logger = LogFactory.getLog(ExposeHttpServletRequestFilter.class);
       private ServletContext servletContext;
       public void destroy()
       public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
       HttpServletRequest httpServletRequest = (HttpServletRequest) request;
       chain.doFilter(request, response);
       } catch (Exception e)
       logger.error("ended request due to exception", e);
       throw new ServletException(e);
       } finally
       logger.debug("ended request");
       public void init(FilterConfig config) throws ServletException
       servletContext = config.getServletContext();
       private void addRemoteUser(HttpServletRequest httpServletRequest)
       httpServletRequest.setAttribute("remoteUsr", httpServletRequest.getRemoteUser());
       private void addBasePath(HttpServletRequest httpServletRequest)
       StringBuffer basePath = new StringBuffer();
       basePath.append(httpServletRequest.getScheme() + "://").append(httpServletRequest.getServerName() + ":" + httpServletRequest.getServerPort()).append(httpServletRequest.getContextPath() + "/");
       httpServletRequest.setAttribute("basePath", basePath.toString());

        • 1. Re: HttpServletRequest from Seam
          eekboom Newbie

          FacesContext facesContext = FacesContext.getCurrentInstance();
          ExternalContext externalContext = facesContext.getExternalContext();
          HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();

          • 2. Re: HttpServletRequest from Seam

            I did away with the filter. I am posting an example here in case anyone finds it useful. I mainly use this to set the href value is the base tag in xhtml.

            People have helped me on this so I am going to start posting general examples here.

            public class ExposeHttpServletRequestBean implements Serializable, ExposeHttpServletRequest
             private static final long serialVersionUID = 2675604092563757800L;
             @Out(scope = ScopeType.EVENT, required = false)
             private String userJAAS;
             @Out(scope = ScopeType.EVENT, required = false)
             private String basePath;
             private FacesContext facesContext;
             private HttpServletRequest request;
             public void setup() {
             request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
             public void loadUserJAAS()
             userJAAS = request.getRemoteUser();
             public void loadBasePath()
             basePath = request.getScheme() + "://" +
             request.getServerName() + ":" +
             request.getServerPort() + request.getContextPath() + "/";

            public interface ExposeHttpServletRequest {
             public void loadUserJAAS();
             public void loadBasePath();
             public void setup();

            • 3. Re: HttpServletRequest from Seam


              Probably the best place to put examples is the wiki.