【问题标题】:Jersey: Implementing a welcome page泽西岛:实施欢迎页面
【发布时间】:2013-05-14 04:24:17
【问题描述】:

我想在访问我的 Jersey 应用程序的上下文根时创建一个欢迎页面。不幸的是,servlet 映射在我的 web.xml 上设置为/*,并且根据this 链接,将 servlet 和主页放在同一个位置是很糟糕的。目前,如果我更改我的 servlet 的 URL 模式,这将需要我们想要阻止的大量代码更改,因此我们将通过使用后端代码生成欢迎 html 页面来采取不好的做法。

看到这已经是肮脏的方式了,我们怎样才能让它更干净一点呢?有没有更好的方法来导入 jsp 和 css 文件?我不想将它们全部硬编码为一个字符串。 :(

【问题讨论】:

  • 嗨,我的回答能解决你的问题吗:)

标签: http jersey web.xml


【解决方案1】:

你可以编写一个过滤器来拦截请求,在过滤器中检查请求的url是否为'/',如果是,则将请求转发到欢迎页面。

public class MyFilter implements Filter {
    private ServletContext servletContext;

    public void init(FilterConfig config) throws ServletException {
        servletContext = config.getServletContext();
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        String path = ((HttpServletRequest)request).getPathInfo();
        if(path.equals("/")){
            servletContext.getRequestDispatcher("/welcome.jsp").forward(request, response);
        } else {
            chain.doFilter(request,response);
        }
    }
}

在 web.xml 中应用过滤器:

<filter>  
    <filter-name>welcomeFilter</filter-name>  
    <filter-class>the filter class</filter-class>  
</filter>  
<filter-mapping>  
    <filter-name>welcomeFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 2013-12-21
    • 1970-01-01
    相关资源
    最近更新 更多