You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Marcel Stör <ma...@frightanic.com> on 2009/04/30 21:56:53 UTC
Static resources not found in embedded Tomcat
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
Re: Static resources not found in embedded Tomcat
Posted by Marcel Stör <ma...@frightanic.com>.
On 01.05.2009, at 10:20, Mark Thomas wrote:
> Marcel Stör wrote:
>> All I need for an embedded Tomcat (I'm working with 6.x) are a few
>> Tomcat JARs in my classpath, correct?
>
> It depends what functionality you want - you'll probably need most
> of them.
>
>> 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:
>
> That sounds like you haven't configured the JSP servlet or the
> default servlet.
> Take a look at CATALINA_BASE/conf/web.xml - you probably want to
> include some of
> that stuff in your app's web.xml
Oh yes, indeed. Isn't it obvious...gosh.
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
Re: Static resources not found in embedded Tomcat
Posted by Mark Thomas <ma...@apache.org>.
Marcel Stör wrote:
> All I need for an embedded Tomcat (I'm working with 6.x) are a few
> Tomcat JARs in my classpath, correct?
It depends what functionality you want - you'll probably need most of them.
> 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:
That sounds like you haven't configured the JSP servlet or the default servlet.
Take a look at CATALINA_BASE/conf/web.xml - you probably want to include some of
that stuff in your app's web.xml
HTH,
Mark
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org