Class WicketSessionFilter

java.lang.Object
org.apache.wicket.protocol.http.servlet.WicketSessionFilter
All Implemented Interfaces:
jakarta.servlet.Filter

public class WicketSessionFilter extends Object implements jakarta.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
  • Constructor Details

  • Method Details

    • init

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

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

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