Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Excerpt
hiddentrue

How to get access to the raw HttpServletRequest and HttpServletResponse objects

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:

Code Block

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:

Code Block
HttpServletRequest request = getWebRequestCycle().getWebRequest().getHttpServletRequest();
HttpServletResponse response = getWebRequestCycle().getWebResponse().getHttpServletResponse();