How to get access to the raw HttpServletRequest and HttpServletResponse objects

In an ideal world, this wouldn't be necessary. But we all know how ideal our world really is! In my case it was a need to connect to a portal which required access to the HttpServletRequest and HttpServletResponse. Note that for most uses, Component's getRequest() and getResponse() methods will give you objects you can use. So really, this is not the Wicket Way.

But if you find you need access to these objects you can get them in this manner:


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import wicket.markup.html.WebMarkupContainer;
import wicket.protocol.http.servlet.ServletWebRequest;

[ ... ]

    ServletWebRequest servletWebRequest = (ServletWebRequest) getRequest();
    HttpServletRequest request = servletWebRequest.getHttpServletRequest();

    WebResponse webResponse = (WebResponse) getResponse();
    HttpServletResponse response = webResponse.getHttpServletResponse();

From within a subclass of WebPage you can do something similar by getting hold of the web request cycle:

HttpServletRequest request = getWebRequestCycle().getWebRequest().getHttpServletRequest();
HttpServletResponse response = getWebRequestCycle().getWebResponse().getHttpServletResponse();
  • No labels