我们使用 OSGI Equinox“org.eclipse.equinox.http.registry.resources”扩展来定义 OSGI Equinox 服务器中不同 JAR 中可访问的资源。其中大多数只是指向静态 HTML 内容,因此没有 Servlet 实现。我想知道定义子文件夹的默认页面的最简单方法是什么(定义通常在标准 Servlet 打包中的 web.xml 中定义的“Welcome”文件)。基本上,我在/mynewresource 定义了一个资源,并在用户输入时将其链接到 index.html,而不是收到服务器错误。
最佳答案
如果您只想在资源上实现访问 index.html 的默认行为,您可以创建这个简单的过滤器:
public class WelcomFilter implements javax.servlet.Filter {
/** {@inheritDoc} */
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
/** {@inheritDoc} */
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (request instanceof HttpServletRequest) {
HttpServletRequest r = (HttpServletRequest) request;
if ("/".equals(r.getPathInfo())) {
r.getRequestDispatcher("index.html").forward(request, response);
} else {
chain.doFilter(request, response);
}
} else {
chain.doFilter(request, response);
}
}
/** {@inheritDoc} */
@Override
public void destroy() {
}
}
您有两种选择:您可以在根 (/) 处注册此过滤器一次,但请记住,任何没有路径信息的请求都将被重定向到index.html,或者您可以将其注册到您所在的子域想要它。无论如何,您都需要使用 equinox http 过滤器扩展。
<extension
point="org.eclipse.equinox.http.registry.filters">
<filter
alias="/mydomain"
class="com.abc.filters.WelcomeFilter">
</filter>
</extension>
关于java - 如何在 Equinox OSGI 服务器中定义默认欢迎页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18018261/