向jetty添加多个资源目录 [英] Add more than one resource directory to jetty
问题描述
http:// localhost:8282 / A
http:// localhost:8282 / B
http:// localhost:8282 / C
- A被放置在X / V / A中
- B放在Q / Z / B中
- C放在P / T / C
以下失败:
ResourceHandler resource_handler = new ResourceHandler();
resource_handler.setWelcomeFiles(new String [] {index.html});
resource_handler.setResourceBase(HTML_SITE);
ResourceHandler resource_handler1 = new ResourceHandler();
resource_handler1.setWelcomeFiles(new String [] {index.html});
resource_handler1.setResourceBase(HTML_CLIENTZONE_SITE);
//部署引擎
WebAppContext webapp = new WebAppContext();
String dir = System.getProperty(user.dir);
webapp.setResourceBase(getWebAppPath());
webapp.setContextPath(/);
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler [] {resource_handler,resource_handler1,webapp,new DefaultHandler()});
server.setHandler(处理程序);
如何添加多个静态资源目录?
自6.1.12以来,通过使用ResourceCollection到WebAppContext的基础资源来支持这一点:
服务器服务器=新服务器(8282);
WebAppContext context = new WebAppContext();
context.setContextPath(/);
ResourceCollection resources = new ResourceCollection(new String [] {
project / webapp / folder,
/ root / static / folder / A,
/ root /静态/文件夹/ B,
});
context.setBaseResource(resources);
server.setHandler(context);
server.start();
要随后打开一个文件,请使用ServletContext(例如WebAppContext),它可能是界面定义,如:
/ **
* servlet上下文。
* /
public default InputStream open(ServletContext context,String filename){
String f = System.getProperty(file.separator)+ filename;
return context.getResourceAsStream(f);
}
如:
< pre class =lang-java prettyprint-override>
InputStream in = open(context,filename.txt);
这将打开 filename.txt
存在于一个给定的目录中。请注意,getResourceAsStream将返回 null
,而不是抛出异常,所以最好检查它:
public default InputStream validate(InputStream in,String filename)
throws FileNotFoundException {
if(in == null){
抛出新的FileNotFoundException(filename);
}
返回;
}
然后你可以更新打开
方法如下:
return validate(context.getResourceAsStream(filename),filename) ;
Looking to use multiple static directories with Jetty. When the server runs:
http://localhost:8282/A
http://localhost:8282/B
http://localhost:8282/C
- A is placed in X/V/A
- B is placed in Q/Z/B
- C is placed in P/T/C
The following failed:
ResourceHandler resource_handler = new ResourceHandler();
resource_handler.setWelcomeFiles(new String[]{"index.html"});
resource_handler.setResourceBase(HTML_SITE);
ResourceHandler resource_handler1 = new ResourceHandler();
resource_handler1.setWelcomeFiles(new String[]{"index.html"});
resource_handler1.setResourceBase(HTML_CLIENTZONE_SITE);
// deploy engine
WebAppContext webapp = new WebAppContext();
String dir = System.getProperty("user.dir");
webapp.setResourceBase(getWebAppPath());
webapp.setContextPath("/");
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[]{resource_handler,resource_handler1 ,webapp, new DefaultHandler()});
server.setHandler(handlers);
How can I add more than one static resource directory?
Since 6.1.12, this is supported by using a ResourceCollection to the WebAppContext's base resource:
Server server = new Server(8282);
WebAppContext context = new WebAppContext();
context.setContextPath("/");
ResourceCollection resources = new ResourceCollection(new String[] {
"project/webapp/folder",
"/root/static/folder/A",
"/root/static/folder/B",
});
context.setBaseResource(resources);
server.setHandler(context);
server.start();
To subsequently open a file, use the ServletContext (e.g., WebAppContext), which could be part of an interface definition, such as:
/**
* Opens a file using the servlet context.
*/
public default InputStream open( ServletContext context, String filename ) {
String f = System.getProperty( "file.separator" ) + filename;
return context.getResourceAsStream( f );
}
Such as:
InputStream in = open( context, "filename.txt" );
This will open filename.txt
if it exists in one of the given directories. Note that getResourceAsStream will return null
, rather than throw an exception, so it's a good idea to check for it:
public default InputStream validate( InputStream in, String filename )
throws FileNotFoundException {
if( in == null ) {
throw new FileNotFoundException( filename );
}
return in;
}
Then you can update the open
method as follows:
return validate( context.getResourceAsStream( filename ), filename );
这篇关于向jetty添加多个资源目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!