【问题标题】:how to Open HTML page of subdirectory when URL is hit- using java web.xml当 URL 被命中时如何打开子目录的 HTML 页面 - 使用 java web.xml
【发布时间】:2011-10-17 10:03:05
【问题描述】:

我有一个场景,当用户打开 http://MYWEBSITE.com/abc/ 时,用户被定向到 abc 子目录中的 xyz.html 页面。我正在使用 Java 进行 Web 开发。如何在 web.xml 中执行此操作?

附:网址是 http://MYWEBSITE.com/abc/ 而不是 http://MYWEBSITE.com/abc

【问题讨论】:

    标签: java html web.xml subdirectory


    【解决方案1】:

    你可以像这样在 web.xml 中定义一个 servlet-filter

    <filter>
        <filter-name>incompleteUrlFilter</filter-name>
        <filter-class>com.mywebsite.IncompleteUrlFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>incompleteUrlFilter</filter-name>
        <url-pattern>/abc/*</url-pattern>
    </filter-mapping>
    

    过滤器类看起来像这样

    package com.mywebsite;
    
    public class IncompleteUrlFilter implements Filter {
    
        public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
            if( ((HttpServletRequest)req).getPathInfo() == null ){ // nothing after /abc/ ! 
                req.getRequestDispatcher("/abc/xyz.html").forward(req, resp); // forward to xyz.html
            } else {
                chain.doFilter(req, resp); // else continue as usual
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-12
      • 2013-08-26
      相关资源
      最近更新 更多