Class WicketSessionFilter

  • All Implemented Interfaces:
    javax.servlet.Filter

    public class WicketSessionFilter
    extends Object
    implements javax.servlet.Filter
    This filter can be used to make the Wicket WebSession instances available to non-wicket servlets.

    The following example shows how this filter is setup to for a servlet. You can find the example in the wicket-examples project.

      <!-- The WicketSesionFilter can be used to provide thread local access to servlets/ JSPs/ etc -->
      <filter>
        <filter-name>WicketSessionFilter</filter-name>
        <filter-class>org.apache.wicket.protocol.http.servlet.WicketSessionFilter</filter-class>
        <init-param>
          <param-name>filterName</param-name>
          <!-- expose the session of the input example app -->
          <param-value>FormInputApplication</param-value>
        </init-param>
      </filter>
     
      <!-- couple the session filter to the helloworld servlet -->
      <filter-mapping>
        <filter-name>WicketSessionFilter</filter-name>
        <url-pattern>/helloworldservlet/*</url-pattern>
      </filter-mapping>
      ...
     
      <servlet>
        <servlet-name>HelloWorldServlet</servlet-name>
        <servlet-class>org.apache.wicket.examples.HelloWorldServlet</servlet-class>
      </servlet>
     
      <servlet-mapping>
        <servlet-name>HelloWorldServlet</servlet-name>
        <url-pattern>/helloworldservlet/*</url-pattern>
      </servlet-mapping>
     
    Note: If both WicketFilter and WicketSessionFilter are mapped to the same url pattern, make sure to have the <filter-mapping> for WicketFilter first in your web.xml.

    After that, you can get to the Wicket session in the usual fashion:

     if (Session.exists())
     {
            Session wicketSession = Session.get();
     }
     
    Make sure to test for session existence first, like the HelloWorldServlet does:
     public class HelloWorldServlet extends HttpServlet
     {
            public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
                    IOException
            {
                    res.setContentType("text/html");
                    PrintWriter out = res.getWriter();
                    String message = "Hi. " +
                            (Session.exists() ? " I know Wicket session " + Session.get() + "."
                                    : " I can't find a Wicket session.");
                    out.println(message);
                    out.close();
            }
     }
     
    Author:
    Eelco Hillenius
    • Method Detail

      • init

        public void init​(javax.servlet.FilterConfig filterConfig)
                  throws javax.servlet.ServletException
        Specified by:
        init in interface javax.servlet.Filter
        Throws:
        javax.servlet.ServletException
        See Also:
        Filter.init(javax.servlet.FilterConfig)
      • doFilter

        public void doFilter​(javax.servlet.ServletRequest request,
                             javax.servlet.ServletResponse response,
                             javax.servlet.FilterChain chain)
                      throws IOException,
                             javax.servlet.ServletException
        Specified by:
        doFilter in interface javax.servlet.Filter
        Throws:
        IOException
        javax.servlet.ServletException
        See Also:
        Filter.doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
      • destroy

        public void destroy()
        Specified by:
        destroy in interface javax.servlet.Filter
        See Also:
        Filter.destroy()