I am trying to get the contextPath but I get this exception
ServletContextHandler.contextInitialized()HERE MY PRINT
2011-02-22 02:45:38,614 ERROR main tomcat.localhost./photo.Context - Error listenerStart
2011-02-22 02:45:38,615 ERROR main tomcat.localhost./photo.Context - Context startup failed due to previous errors
this is my ServletContextListener
class
public class ServletContextHandler implements ServletContextListener {
private final static Logger logger = Logger.getLogger(ServletContextHandler.class);
public ServletContextHandler(){}
public void contextInitialized(ServletContextEvent contextEvent){
try{
//LOG DEBUG
logger.debug("Server.init()-> set context path");
System.out.println("ServletContextHandler.contextInitialized()HERE MY PRINT");
System.out.println("ServletContextHandler.contextInitialized() " + contextEvent.getServletContext().getContextPath());
}catch(Exception e){
e.printStackTrace();
}
}
public void contextDestroyed(ServletContextEvent contextEvent){
}
}
and this is my web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
<listener>
<listener-class>
utils.ServletContextHandler
</listener-class>
</listener>
</web-app>
can you help me please?
You will need to set store the context path somewhere. For example, you can do something like this:-
In this example, I created
MyServletContext
that basically contains 2 static methods that allow you to set and get the stored context path:-To get the context path, instead of doing
request.getContextPath()
, you invokeMyServletContext.getContextPath()
.