博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to disable directory listing for Jetty's We...
阅读量:7060 次
发布时间:2019-06-28

本文共 3694 字,大约阅读时间需要 12 分钟。

hot3.png

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.

I found the following page on the net which describes the same problem:

I quote what is mentioned in one of the entries in that post as reason for the problem:

the problem is that for some reason Jetty does not merge the webdefault.xml with user web.xml properly when embedded mode is used

and following is the code that was used to overcome the problem:

HashMap hmap = new HashMap
();  hmap.put("dirAllowed", "false");  hmap.put("redirectWelcome", "false");  hmap.put("aliases", "false");  ServletHolder []svh = wc.getServletHandler().getServlets();   if(svh != null && svh.length > 0) {
 for(int j = 0; j < svh.length; j++) {
ServletHolder svh1 = svh[j]; if(svh1.getClassName() != null && svh1.getClassName().endsWith(DEFAULT_SERVLET))  {
svh1.setInitParameters(hmap);  } } }

Unfortunately it didn't help:ServletHolder[] svh = webAppContext.getServletHandler().getServlets();returns a zero length array. – 

If anyone happens across this looking for the equivalent in Jetty 6:

. .
 
 

转载于:https://my.oschina.net/u/1047983/blog/140899

你可能感兴趣的文章
面试学到的css布局,细节影响了我的面试成绩
查看>>
Ubuntu16.04 安装lamp环境
查看>>
apache伪静态规则及常见规则用法实例
查看>>
去哪儿网电话是多少
查看>>
13、 使用openpyxl存储周杰伦的歌曲信息
查看>>
[python] A*算法基于栅格地图的全局路径规划
查看>>
苹果新的编程语言 Swift 语言进阶(二)--基本类型
查看>>
git分支管理
查看>>
VS2017 ASP.NET C#编译ScriptManager bug
查看>>
模数混合电路(模拟、数字电源和地)
查看>>
自然语言处理入门
查看>>
Jquery中children、find区别
查看>>
java窗体
查看>>
zipkin-client:brave核心代码思路整理
查看>>
iOS学习31之UITableVIewCell自定义
查看>>
mysql 分区
查看>>
移动端pc端同步测试工具 Browsersync的安装和使用
查看>>
将获得datebox值的文本形式转为日期格式
查看>>
写给大数据开发初学者的话5[转]
查看>>
LDA数学八卦笔记(三)LDA文本建模
查看>>