This filter can be used to make the Wicket
org.apache.wicket.protocol.http.WebSessioninstances 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
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();
}
}