Works with spring too.
$ jar -tvf webapps/webapp-3.0-bare-spring.war
0 Thu Jan 15 12:32:52 MST 2015 META-INF/
131 Thu Jan 15 12:32:52 MST 2015 META-INF/MANIFEST.MF
0 Thu Jan 15 12:32:52 MST 2015 WEB-INF/
0 Thu Jan 15 12:32:52 MST 2015 WEB-INF/lib/
0 Thu Jan 15 12:32:52 MST 2015 WEB-INF/classes/
0 Thu Jan 15 12:32:52 MST 2015 WEB-INF/classes/org/
0 Thu Jan 15 12:32:52 MST 2015 WEB-INF/classes/org/eclipse/
0 Thu Jan 15 12:32:52 MST 2015 WEB-INF/classes/org/eclipse/demo/
4467 Mon Oct 08 12:19:26 MST 2012 WEB-INF/lib/aopalliance-1.0.jar
442400 Thu Jan 15 12:27:02 MST 2015 WEB-INF/lib/spring-core-3.1.0.RELEASE.jar
53079 Thu Jan 15 12:27:02 MST 2015 WEB-INF/lib/spring-asm-3.1.0.RELEASE.jar
829601 Thu Jan 15 12:27:02 MST 2015 WEB-INF/lib/spring-context-3.1.0.RELEASE.jar
176283 Thu Jan 15 12:27:02 MST 2015 WEB-INF/lib/spring-_expression_-3.1.0.RELEASE.jar
540819 Thu Jan 15 12:27:02 MST 2015 WEB-INF/lib/spring-web-3.1.0.RELEASE.jar
60686 Thu Sep 27 08:14:48 MST 2012 WEB-INF/lib/commons-logging-1.1.1.jar
589253 Thu Jan 15 12:27:02 MST 2015 WEB-INF/lib/spring-beans-3.1.0.RELEASE.jar
331474 Thu Jan 15 12:27:02 MST 2015 WEB-INF/lib/spring-aop-3.1.0.RELEASE.jar
1699 Thu Jan 15 12:32:52 MST 2015 WEB-INF/classes/org/eclipse/demo/TimeServlet.class
1086 Thu Jan 15 12:32:52 MST 2015 WEB-INF/classes/org/eclipse/demo/MyAppInitializer.class
Code for MyAppInitializer.java
package org.eclipse.demo;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletContext;
import org.springframework.web.WebApplicationInitializer;
public class MyAppInitializer implements WebApplicationInitializer
{
@Override
public void onStartup(ServletContext context)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
context.setAttribute("my.app.init","Initialized at: " + sdf.format(new Date()));
}
}
and Code for TimeServlet.java
package org.eclipse.demo;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
@WebServlet(urlPatterns="/time")
public class TimeServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.setContentType("text/plain");
PrintWriter out = resp.getWriter();
out.println("(From " + MyAppInitializer.class.getName() + "): "
+ req.getServletContext().getAttribute("my.app.init"));
out.println("(From servlet): " + new Date());
}
}
The server console:
2015-01-15 12:33:35.626:INFO:oejs.Server:jetty-8.1.16.v20140903
2015-01-15 12:33:35.640:INFO:oejdp.ScanningAppProvider:Deployment monitor /home/joakim/jetty-distros/jetty-distribution-8.1.16.v20140903/webapps at interval 1
2015-01-15 12:33:35.646:INFO:oejd.DeploymentManager:Deployable added: /home/joakim/jetty-distros/jetty-distribution-8.1.16.v20140903/webapps/webapp-3.0-bare-spring.war
2015-01-15 12:33:35.676:INFO:oejw.WebInfConfiguration:Extract jar:file:/home/joakim/jetty-distros/jetty-distribution-8.1.16.v20140903/webapps/webapp-3.0-bare-spring.war!/ to /tmp/jetty-0.0.0.0-8080-webapp-3.0-bare-spring.war-_webapp-3.0-bare-spring-any-/webapp
2015-01-15 12:33:36.432:INFO:w.0-bare-spring:Spring WebApplicationInitializers detected on classpath: [org.eclipse.demo.MyAppInitializer@70941f0a]
2015-01-15 12:33:36.659:INFO:oejdp.ScanningAppProvider:Deployment monitor /home/joakim/jetty-distros/jetty-distribution-8.1.16.v20140903/contexts at interval 1
2015-01-15 12:33:36.907:INFO:oejus.SslContextFactory:Enabled Protocols [SSLv2Hello, TLSv1, TLSv1.1, TLSv1.2] of [SSLv2Hello, SSLv3, TLSv1, TLSv1.1, TLSv1.2]
2015-01-15 12:34:18.477:INFO:oejs.Server:Graceful shutdown o.e.j.w.WebAppContext{/webapp-3.0-bare-spring,file:/tmp/jetty-0.0.0.0-8080-webapp-3.0-bare-spring.war-_webapp-3.0-bare-spring-any-/webapp/},/home/joakim/jetty-distros/jetty-distribution-8.1.16.v20140903/webapps/webapp-3.0-bare-spring.war
2015-01-15 12:34:19.534:INFO:oejsl.ELContextCleaner:javax.el.BeanELResolver purged
2015-01-15 12:34:19.534:INFO:oejsh.ContextHandler:stopped o.e.j.w.WebAppContext{/webapp-3.0-bare-spring,file:/tmp/jetty-0.0.0.0-8080-webapp-3.0-bare-spring.war-_webapp-3.0-bare-spring-any-/webapp/},/home/joakim/jetty-distros/jetty-distribution-8.1.16.v20140903/webapps/webapp-3.0-bare-spring.war
The test case:
(From org.eclipse.demo.MyAppInitializer): Initialized at: 2015-01-15T12:33:36-0700
(From servlet): Thu Jan 15 12:34:12 MST 2015
$