【发布时间】:2021-05-07 09:47:13
【问题描述】:
我已重建 vaadin webapp 并删除了所有 Spring 功能。 我的 webapp 现在是非基于 Spring 的应用程序的 GreetService starter 下载。
应用程序在 tomcat 9.0 应用程序服务器上运行,在 TOMCAT_HOME/conf/web.xml 中具有以下配置以获取目录列表。
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
应用程序中没有其他 web.xml。
我在我的 maven 项目的 src/main/webapp 文件夹中创建了一个图像、图标和测试文件夹。应用目录布局如下
按照 Alejandro 的建议,我在其中两个文件夹中加入了一个空的 index.html,然后在另一个文件夹中加入了一个简单的文本文件。
我使用 Production Profile 将应用程序构建到 WAR 文件中。
这一切看起来都很好,我的应用程序与以下 URL 完美配合。
但是,如果我浏览到图像文件夹或图标文件夹,我会得到一个内容列表。
我想停止该级别的任何文件夹的列表。我构建了默认的应用程序启动器并部署了它,我得到了上述行为。
然后我尝试使用@WebInitParam 引入以下@WebServlet 注释,以将此webappp 的列表设置为false。这也没有效果。
这是我的 AgentServlet 类的服务方法中的日志记录输出。
18-12:32:33.620 [https-jsse-nio-8443-exec-10] INFO o.v.e.AgentServlet.service - Request: Context Path = /myapp-nospring-1.0
, Request URI = /myapp-nospring-1.0/sw.js
, Request URL = https://wfd.sybernet.com:8443/myapp-nospring-1.0/sw.js
, Servlet Context Path = /myapp-nospring-1.0
, Servlet Context Name = null
, Servlet Mapping = /*
, Servlet Mapping Match Value = sw.js
, Servlet Mapping Match Name = PATH
18-12:32:33.623 [https-jsse-nio-8443-exec-10] INFO o.v.e.AgentServlet.service - In AgentServlet ... service().. Listings
Setting = false
18-12:32:33.625 [https-jsse-nio-8443-exec-10] INFO o.v.e.AgentServlet.lambda$service$0 -
org.vaadin.example.AgentServlet:org.vaadin.example.AgentServlet
18-12:32:33.626 [https-jsse-nio-8443-exec-10] INFO o.v.e.AgentServlet.lambda$service$0 - default:org.apache.catalina.servlets.DefaultServlet
18-12:32:33.629 [https-jsse-nio-8443-exec-10] INFO o.v.e.AgentServlet.lambda$service$0 jsp:org.apache.jasper.servlet.JspServlet
18-12:32:33.684 [https-jsse-nio-8443-exec-10] DEBUG c.v.f.s.c.UidlWriter.createUidl - * Creating response to client
18-12:32:33.688 [https-jsse-nio-8443-exec-10] DEBUG c.v.f.s.BootstrapHandler.getInitialUidl - Initial UIDL: [object
Object]
注册了三个 servlet - 默认一个,jsp 一个和我的一个,这是我的带有注释的 AgentServlet 类。
我的问题是:-
-
默认情况下,Vaadin servlet 是否允许列出这些文件夹,即默认情况下列表设置为“true”。使用 web init 参数将其设置为 false 似乎没有任何效果。
-
将文件夹放在 Vaadin 应用程序的 webapp 上下文文件夹中是否存在根本性错误,即在 http://appserver:port/vaadin-web-app/images 中。如果我使用 JSP 应用程序执行此操作,则应用程序的行为与我预期的一样,并且不会列出文件夹内容。你得到一个 404 或者如果 index.html 在那里,它将被呈现。如有任何 cmets 或建议,我将不胜感激。
@WebServlet(value = { "/*"}, initParams= {@WebInitParam(name="listings", value="false")}, asyncSupported =true) public class AgentServlet extends VaadinServlet { /** the logger. */ private static final Logger MLOGGER = LogManager.getLogger(AgentServlet.class); @Override protected void servletInitialized() throws ServletException { super.servletInitialized(); MLOGGER.info("In AgentServlet ... servletInitialized()"); } @Override protected void service(HttpServletRequest request, HttpServletResponse response) { MLOGGER.info("In AgentServlet ... service()"); MLOGGER.info("Request: Context Path = " + request.getContextPath() + "\n, Request URI = " + request.getRequestURI() + "\n, Request URL = " + request.getRequestURL() + "\n, Servlet Context Path = " + request.getServletContext().getContextPath() + "\n, Servlet Context Name = " + request.getServletContext().getServletContextName() + "\n, Servlet Mapping = " + request.getHttpServletMapping().getPattern() + "\n, Servlet Mapping Match Value = " + request.getHttpServletMapping().getMatchValue() + "\n, Servlet Mapping Match Name = " + request.getHttpServletMapping().getMappingMatch().name() ); String ListingsSetting = getServletConfig().getInitParameter("listings"); MLOGGER.info("In AgentServlet ... service().. Listings Setting = " + ListingsSetting); request.getServletContext().getServletRegistrations().forEach((key, value) -> MLOGGER.info(key + ":" + value.getClassName())); try { super.service(request, response); } catch (ServletException se) { MLOGGER.error("In AgentServlet ... service() caught Servlet Exception " + se.getMessage()); } catch (IOException ie) { MLOGGER.error("In AgentServlet ... service() caught IO Exception " + ie.getMessage()); }} }
【问题讨论】:
-
我猜您在问题中将
<param-value>更改为false。问题是否仍然存在?如果是,请通过编辑您的问题或在答案下发表评论来详细说明。如果没有,你可以接受@Alejandro Duarte 的回答,这有助于其他人知道这个问题的答案。 -
TOMCAT_HOME/conf/web.xml 的 web.xml 中的列表值已设置为“false”,但仍列出图像文件夹的内容。这是我的问题 - 这是在其他地方重置吗?
-
我已经重写了问题并删除了与问题无关的任何内容。我希望这更清楚。
-
为什么将 init 参数设置为 DefaultSerlvet,而不是 AgentServlet?
-
我的理解是我两样都做。 DefaultServlet 的 init-param 在 tomcat 主目录的 web.xml 中设置。这是 Tomcat 的“开箱即用”配置。因为我在我的 Vaadin 项目中使用了带有 '/*' 值的 @WebServlet 注释,所以我的理解是对我的 webapp 上下文的任何请求(无论是否针对静态资源)都将由 AgentServlet 处理,我我试图确保 AgentServlet 在目录列表方面的行为与 DefaultServlet 相同。