Forwarding to a URL from Wicket | MyThinkPond

Sometimes we need to forward to a servlet or resource that’s outside of Wicket such as a servlet. Here’s how:

  1. Modify your web.xml to define your servlet as well as your Wicket application “ignorePaths” init-param:

[sourcecode language=“xml”] <?xml version=“1.0” encoding=“UTF-8”?> <!DOCTYPE web-app PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN” “http://java.sun.com/dtd/web-app_2_3.dtd"> Wicket Example MyApplication org.apache.wicket.protocol.http.WicketFilter applicationClassName com.gu.MyApplication wicket.configuration deployment ignorePaths images/,s/ MyApplication /* SomeServlet com.gu.SomeServlet SomeServlet /s/koolkat.do [/sourcecode]

The first observation is that Wicket is running as a Filter and not a Servlet. Second, the init-param - “ignorePaths” says that anything in the “/images” and “/s/*” will be ignored by Wicket Filter and passed along downstream.

  1. Next in your Wicket Form, within your onSubmit method override you can do the following:

[sourcecode language=“Java”] class TestForm extends Form(id){ @override public TestForm(String id, SomeFormModel ghfm) { //Code removed } @override public onSubmit() { String url = “/s/koolkat.do”; //some url within the same web-context getRequestCycle().setRequestTarget(new RedirectRequestTarget(url)); } } [/sourcecode]

Notice that the url field does not contain the “ContextPath”. Wicket takes care of that in the background. On the other hand, suppose you wanted to redirect to a URL ourside of your web context or to an external site then you would set the variable like this.

[sourcecode language=“Java”] String url = “http://mythinkpond.wordpress.com/2009/11/13/forwarding-to-a-url-from-wicket/"; //or url = “http://myserver:port/othercontext/someurl";
[/sourcecode]

On some rare occasions, you might want to get the servlet Request Dispatcher to forward a request. For that it gets a little tricky.

– In your class you want to grab the “HttpServletResponse”. I’ve been unsuccessful in getting it in the class. [sourcecode language=“Java”] class xyz extends WebPage{ public xyz() { HttpServletResponse response = getWebRequestCycle().getWebResponse().getHttpServletResponse(); //additional code xyxForm thisForm = new xyzForm(thisModel, response); } }

class xyzForm extends Form{ HttpServletResponse response;

public xyzForm(xyzModel model, HttpServletResponse thisResponse) { response = thisResponse; }

public onSubmit() { HttpServletRequest request = getWebRequest().getHttpServletRequest(); //additional code here getWebRequest().getHttpServletRequest().getRequestDispatcher(url).forward(request, response); } } [/sourcecode]

Please note that the above code is pseudo-code and not actual code because I was writing it in Scala and not Java. But for the audience, I'm trying to express it in Java so that it can have the greatest impact.

That’s it! Happy Coding!

If you find this article useful, consider signing up for my RSS feed or Email Newsletter. See links on the right side.
Venkatt Guhesan avatar
About Venkatt Guhesan
Enterprise Software Architect currently working at DataDirect Networks developing a highly scalable monitoring solution called DDN Insight. My passions include all things Open Source including Linux (CentOS, Red Hat, Gentoo, Open Stack, Docker & Ubuntu), embedded ARM processors and microcontrollers (Arduino, UDOO, Cubieboard, BeagleBoard, OnionIO, TI's Zigbee). Coming from an Electrical and Computer Engineering background, working in developing and designing enterprise - distributed, highly scalable software requires a unique set of software skills with a good understanding of the hardware. This makes his work challenging and interesting. In his free time, he spends his time with his family and two children. He also enjoys researching on topics including - world ancient history and yoga. Visit the 'About' section for more details.
comments powered by Disqus