On Java, Python, Groovy, Grails, Spring, Node.js, Linux, Arduino, ARM, Embedded Devices & Web

  • Recent Posts

    November 2009
    M T W T F S S
        Jan »
  • Subscribe Options

  • Awards

  • Most Valuable Blogger @ DZone
  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 175 other followers

  • Follow MyThinkPond on
  • Blog Stats

    • 366,804 hits
  • General Options

ContextPath in Apache Wicket

Posted by Venkatt Guhesan on November 5, 2009

One of the first things I found as I was getting familiar with Wicket is how do you point to resources that are part of a page. Well I did some digging around to find out so I’m documenting it here for others who might be facing the same challenges as they discover Apache Wicket.

You have pages in Wicket that are HTML with “Wicket” tags. But then you might want to point to resources such as CSS style sheets or JavaScript that’s part of a page. Here’s an example of a typical JSP page:

<style type="text/css" media="screen">@import \" + request.getContextPath() + "/styles/site.css\";</style>

Well, the above code works when you have a JSP page that gets compiled at run-time before being served. But in the case of Wicket, it does not. So how do you handle such situations?


Step-1: Add “<wicket:head>” and “</wicket:head>” between your HTML page like this:

<!-- Other existing HTML tags can continue to go here -->

Step-2: Get the ServletContext and in turn the Context-Path for the web application in your Java/Scala code.

For application servers implementing Servlet 2.5 and above: (Tomcat 6.x implements Servlet 2.5)

ServletContext servletContext = WebApplication.get().getServletContext();
String contextPath = servletContext.getContextPath();

For application servers implementing Servlet 2.4: (Tomcat 5.5x implements Servlet 2.4)

String contextPath = getWebRequestCycle().getWebRequest().getHttpServletRequest().getContextPath();

What’s the difference? Well, in Servlet 2.5, a new method was added
String javax.servlet.ServletContext.getContextPath()
But if you are deploying your code in an earlier Tomcat or other application servers that do not implement Servlet 2.5, you will be at a disadvantage. So in those cases, use the other approach.

Step-3: In your Java/Scala Wicket code, just as you add labels or other components to your page you can add the following:

add(new StringHeaderContributor("<style type=\"text/css\" media=\"screen\">@import \"" + contextPath + "/styles/site.css\";</style>"));

So here’s what happens. In step-2, we created a variable called “contextPath” with the Servlet Context Path as it’s value. And when you add a “StringHeaderContributor”, it adds an entry into the pages header. It’s that simple!

So now after your browser goes through the request you now have the correct “ContextPath” inserted into your application:

<style type="text/css" media="screen">@import "/sampleapp/styles/site.css";</style>

After I posted the article I noticed I had left out a few additional details on this topic. If you are
adding resources such as images on your page, Wicket provides you with a easy way to do this. There is a org.apache.wicket.markup.html.image.ContextImage class that you can use to create image html tags in your

In your Java or Scala class you can do the following:

add(new ContextImage("logo", contextPath + "/i/logo.jpg"))

and in your html (Wicket) page you can add a html-wicket element like this:

<img wicket:id="logo"/>

Wicket matches the wicket:id “logo” with the element added in the code and displays the image with the correct context-path.


If you find this article useful, consider signing up for my RSS feed or Email Newsletter. See links on the right side.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: