How to get ServletContext in Wicket
Published: February 23, 2009 , Updated: May 24, 2011 , Author: mkyong
Question
The ServletContext is a powerful serlvet class to can provide a lot of information about your web application. Is there any way to get the ServletContext class in Wicket?
Answer
Yes, you can get the ServletContext class via Wicket’s WebApplication class like this :
import javax.servlet.ServletContext; import org.apache.wicket.Page; import org.apache.wicket.protocol.http.WebApplication; import com.mkyong.hello.Hello; public class CustomApplication extends WebApplication { @Override public Class<? extends Page> getHomePage() { ServletContext servletContext = WebApplication.get().getServletContext(); return Hello.class; //return default page } }
servlet-api
If Wicket application can not locate the ServletContext class, please import the servlet-api library into your project class path. For Maven, add this into your pom.xml file.
If Wicket application can not locate the ServletContext class, please import the servlet-api library into your project class path. For Maven, add this into your pom.xml file.
<dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.4</version> </dependency>
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~
[ Read More ] You can find more similar articles at Apache Wicket Tutorials
Hi mkyong,
Just a humble suggestion. The ads seem to distract the content a lot.
may i know which ads is distracting?
yeah, actually it should be:
((WebApplication)WebApplication.get()).getServletContext()
thanks , but There is no getServletContext(); in WebApplication.get()