tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marcel Stör <mar...@frightanic.com>
Subject Static resources not found in embedded Tomcat
Date Thu, 30 Apr 2009 19:56:53 GMT
All I need for an embedded Tomcat (I'm working with 6.x) are a few  
Tomcat JARs in my classpath, correct?

I'm asking because I'm seeing a strange behavior here. Access to  
Servlets deployed in the embedded Tomcat succeed, but requests to  
other resources such as JSPs or static CSS files result in 404 errors.
I have a feeling that I'm missing something in my Tomcat setup. Here's  
my Tomcat launcher class:

public class EmbeddedTomcat {

private String context = null;
private String docBase;
private int port;
private Embedded container = null;
private Log logger = LogFactory.getLog(getClass());

/**
  * Creates a single-webapp configuration to be run in Tomcat.
  *   * @param contextRoot without leading slash, e.g. "mywebapp"
  * @param docBase the web resources directory for the web application  
i.e. the
  * absolute (or relative) path to the content to be served under  
"mywebapp"
  * @param port the port
  */
public EmbeddedTomcat(String contextRoot, String docBase, int port) {
   assert !contextRoot.startsWith("/");
   this.context = "/" + contextRoot;
   this.docBase = docBase;
   this.port = port;

   setupContainer();
}

private void setupContainer() {
   // create server
   container = new Embedded();
   container.setCatalinaHome(findTomcatHome());

   // create webapp loader
   WebappLoader loader = new  
WebappLoader(this.getClass().getClassLoader());

   // create context
   Context rootContext = container.createContext(context, docBase);
   rootContext.setLoader(loader);
   rootContext.setReloadable(true);

   // create host
   Host localHost = container.createHost("localHost", new  
File("").getAbsolutePath());
   // one could call addChild for each web app...
   localHost.addChild(rootContext);

   // create engine
   Engine engine = container.createEngine();
   engine.setName("localEngine");
   engine.addChild(localHost);
   engine.setDefaultHost(localHost.getName());
   container.addEngine(engine);

   // create http connector
   Connector httpConnector = container.createConnector((InetAddress)  
null, port, false);
   container.addConnector(httpConnector);

   container.setAwait(true);
}

/**
  * Tomcat will create its "work" directory in this folder! Hence, the  
process
  * must have write access to it. To make this portable the system  
property
  * java.io.tmpdir is suffixed with "embedded-tomcat".
  */
private String findTomcatHome() {
   String path;
   try {
     path = new File(System.getProperty("java.io.tmpdir"), "embedded- 
tomcat").getCanonicalPath();
   } catch (IOException e) {
     path = "embedded-tomcat";
   }
   return path;
}

/**
  * Starts the embedded Tomcat server.
  *   * @throws LifecycleException if the server could not be started
  */
public void start() throws LifecycleException {
   // starts server
   container.start();

   // add shutdown hook to stop server
   Runtime.getRuntime().addShutdownHook(new Thread() {

     public void run() {
       EmbeddedTomcat.this.stop();
     }
   });
}

/**
  * Stops the embedded Tomcat server.
  */
public void stop() {
   try {
     if (container != null) {
       container.stop();
     }
   } catch (LifecycleException exception) {
     logger.warn("Cannot Stop Tomcat" + exception.getMessage());
   }
}

public String getContext() {
   return context;
}

public int getPort() {
   return port;
}
}


That's how I start it:

EmbeddedTomcat tomcat = new EmbeddedTomcat("myapp", "C:/ 
my_unpacked_war", 8888);
tomcat.start();


C:/my_unpacked_war contains

/WEB-INF/web.xml
/WEB-INF/classes/...
/css/style.css
/index.jsp

Requests to localhost:8888/myapp/index.jsp result in 404 and so does  
localhost:8888/myapp/css/style.css. However if I access the Servlets  
directly with the URLs they're mapped under everything works fine.

Kind regards,
Marcel

-- 
Marcel Stör, http://www.frightanic.com
Blog: http://frightanic.wordpress.com
Couchsurfing: http://www.couchsurfing.com/people/marcelstoer
Skype: marcelstoer




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message