我从Tomcat搬到Jetty,因此我对Jetty的知识非常有限,抱歉。我需要以嵌入模式运行Jetty(在我的应用程序中启动)。
我基本上使用JSP与EL,Servlets和WebSockets。 我已经在使用JSP和Servlet了,我知道如何运行WebSockets,但我找不到办法做所有toguether。
这是我目前正在使用的代码的简化版本。 非常感谢您的帮助。
private static Server server = null;
public static void startup()
{
WebAppContext ctx = new WebAppContext();
ctx.setContextPath( Config.getContextPath() );
initServer( ctx );
initWebSockets();
initServlets( ctx );
initJSPs( ctx );
server.start();
}
private static void initServer( WebAppContext ctx ) throws Exception
{
server = new Server( Config.getWebPort() );
server.setHandler( ctx );
server.setStopAtShutdown( true );
}
private static void initWebSockets() throws Exception
{
ServletContextHandler sch = new ServletContextHandler( ServletContextHandler.SESSIONS );
sch.setContextPath( Config.getContextPath() );
ServerContainer wsContainer = WebSocketServerContainerInitializer.configureContext( ctx );
wsContainer.addEndpoint( EventSocket.class );
}
private static void initServlets( WebAppContext ctx ) throws Exception
{
ctx.addServlet( GetFile.class, "/getfile" );
}
private static void initJSPs( WebAppContext ctx ) throws Exception
{
ctx.setResourceBase( "webui" );
ctx.setDescriptor( "web.xml" );
System.setProperty( "org.apache.jasper.compiler.disablejsr199", "false" );
File fTmp = new File( System.getProperty( "java.io.tmpdir" ), "jetty-jsp-tmp" );
if( ! UtilIO.createFolder( fTmp ) )
{
throw new IOException( "Can not create folder '"+ fTmp.getAbsolutePath() +"'" );
}
ctx.setAttribute("javax.servlet.context.tempdir", fTmp );
ClassLoader jspClassLoader = new URLClassLoader( new URL[0], WebServer.class.getClassLoader() );
ctx.setClassLoader( jspClassLoader );
ServletHolder holderJsp = new ServletHolder( "jsp", JspServlet.class );
holderJsp.setInitOrder( 0 );
ServletHolder holderDefault = new ServletHolder( "default", DefaultServlet.class );
holderDefault.setInitParameter( "resourceBase", ctx.getContextPath() );
holderDefault.setInitParameter( "dirAllowed", "false" );
ctx.addServlet( holderDefault, "/" );
ctx.setAttribute( "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/[^/]*jstl.*\\.jar$" );
Configuration.ClassList classlist = Configuration.ClassList.setServerDefault( server );
classlist.addBefore( "org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
"org.eclipse.jetty.annotations.AnnotationConfiguration" );
classlist.addAfter( "org.eclipse.jetty.webapp.FragmentConfiguration",
"org.eclipse.jetty.plus.webapp.EnvConfiguration",
"org.eclipse.jetty.plus.webapp.PlusConfiguration" );
}