jetty java 禁用目录列表,如何禁用Jetty的WebAppContext的目录列表?

I'm embedding Jetty (version 7.4.5.v20110725) into a java application. I'm serving JSP pages in ./webapps/jsp/ using Jetty's WebAppContext, but if I visit localhost:8080/jsp/ I get Jetty's directory listing for the entire contents of ./webapps/jsp/. I've tried setting the dirAllowed parameter to false on the WebAppContext and it does not change the directory listing behavior.

Disabling the directory listing on a ResourceHandler is simply done be passing false to setDirectoriesListed, works as expected. Can someone tell me how to do this for the WebAppContext?

import org.eclipse.jetty.server.Handler;

import org.eclipse.jetty.server.Server;

import org.eclipse.jetty.server.handler.ContextHandler;

import org.eclipse.jetty.server.handler.HandlerList;

import org.eclipse.jetty.server.handler.ResourceHandler;

import org.eclipse.jetty.server.nio.SelectChannelConnector;

import org.eclipse.jetty.servlet.ServletContextHandler;

import org.eclipse.jetty.servlet.ServletHolder;

import org.eclipse.jetty.webapp.WebAppContext;

public class Test {

public static void main(String[] args) throws Exception {

Server server = new Server();

SelectChannelConnector connector = new SelectChannelConnector();

connector.setHost("127.0.0.1");

connector.setPort(8080);

server.addConnector(connector);

// Create a resource handler for static content.

ResourceHandler staticResourceHandler = new ResourceHandler();

staticResourceHandler.setResourceBase("./webapps/static/");

staticResourceHandler.setDirectoriesListed(false);

// Create context handler for static resource handler.

ContextHandler staticContextHandler = new ContextHandler();

staticContextHandler.setContextPath("/static");

staticContextHandler.setHandler(staticResourceHandler);

// Create WebAppContext for JSP files.

WebAppContext webAppContext = new WebAppContext();

webAppContext.setContextPath("/jsp");

webAppContext.setResourceBase("./webapps/jsp/");

// ??? THIS DOES NOT STOP DIR LISTING OF ./webapps/jsp/ ???

webAppContext.setInitParameter("dirAllowed", "false");

// Create a handler list to store our static and servlet context handlers.

HandlerList handlers = new HandlerList();

handlers.setHandlers(new Handler[] { staticContextHandler, webAppContext });

// Add the handlers to the server and start jetty.

server.setHandler(handlers);

server.start();

server.join();

}

}

解决方案

You can set org.eclipse.jetty.servlet.Default.dirAllowed instead of dirAllowed:

webAppContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");

Tested for Jetty 7.4.5.v20110725, 8.1.4.v20120524, 9.0.2.v20130417 and 9.2.0.v20140526.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值