使用UrlRewriteFilter从页面中删除.html扩展名? - 使用GAE [英] Using UrlRewriteFilter to remove .html extension from page? – Using GAE

查看:135
本文介绍了使用UrlRewriteFilter从页面中删除.html扩展名? - 使用GAE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我的网址正确写入有点困难。我刚刚发现了 UrlRewriteFilter ,它似乎正是我需要的,因为它很难重写url在GAE中使用Java(使用python / go和app.yaml非常简单,但对于Java而言,由于某种原因这是一个很大的麻烦,我必须使用Java)。这是我的 -

 < rule> 
< from> ^ / test.html $< / from>
< to> / test< / to>
< / rule>

我只希望人们能够键入 mysite.com/test 而不是 mysite.com/test.html 。我是否以正确的方式去做这件事?当我使用上面的代码时,得到由 java.lang.StackOverflowError 引起的 HTTP ERROR 500



任何帮助将不胜感激!感谢。

解决方案

在我的研究中,我尝试使用Paul Tuckey的UrlRewriteFilter来处理其他问题。这对我来说并不好,原因有两个。




  • 我不喜欢使用UrlRewriteFilter过滤所有请求的想法,因为所有请求都必须通过应用程序引擎实例进行路由,因此切断了内容交付网络
  • 我可能对此有错,但如果我没有记错的话,在我快速进入UrlRewriteFilter期间,我发现过滤器在内部将内容转发给静态内容,不起作用,因为appengine实例本身没有设置为静态内容 - 只有内容交付网络才这样做。



什么我最近看起来像这样:

  public class RewriteFilter implements Filter {
@Override
public void init (FilterConfig filterConfig)throws ServletException {

}
$ b $ @覆盖
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException {
HttpServletRequest httpServletRequest =(HttpServletRequest)请求;
HttpServletResponse httpServletResponse =(HttpServletResponse)响应;

log.info(Proxy Servlet for:+ httpServletRequest.getRequestURI());

byte [] buffer = new byte [1024];
try(InputStream is = new FileInputStream(index.html); OutputStream os = httpServletResponse.getOutputStream()){
int read = is.read(buffer);
while(read> 0){
os.write(buffer,0,read);
read = is.read(buffer);
}

}

// chain.doFilter(request,response);


@Override
public void destroy(){



$ / code>

尽管非常丑陋,但它可以工作,即使只是用于index.html页面(也可以使用filter init params和定义多个过滤器来更改) 。

但为什么它在UrlRewriteFilter不工作的地方工作?



答案很简单。这个过滤器servlet不会将内部转发给静态内容,而是为内容本身提供服务。由于此过滤器只会在我通过< url-pattern> 指定的< filter-mapping> 在web.xml中,通过此过滤器服务时,我可以忍受轻微的性能下降。如果有人找到了更好的方法来做到这一点,请留下评论。


having a bit of difficulty getting my url to write properly. I just found out about UrlRewriteFilter and it seems to be exactly what I need, since it's kind of difficult to rewrite urls using Java with GAE (it's disgustingly simple using python/go and app.yaml, but with Java it's a major pain in the ass for some reason, and I have to use Java). Here's what I have —

<rule>
    <from>^/test.html$</from>
    <to>/test</to>
</rule>

I just want people to be able to type mysite.com/test instead of mysite.com/test.html. Am I going about this the right way? When I use this code above I get a HTTP ERROR 500 caused by java.lang.StackOverflowError.

Any help would be appreciated! Thanks.

解决方案

I tried to use the UrlRewriteFilter by Paul Tuckey during my research for your other question. That didn't work well for me, for two reasons.

  • I didn't like the idea of filtering all requests with the UrlRewriteFilter because all requests will have to be routed through app engine instances, and therefor cutting out the content delivery network
  • I could be wrong about this but if i remember correctly, during my quick excourse into the UrlRewriteFilter i found that the filter forwards internally to the static content, which doesn't work because the appengine instance itself is not set up to serve static content - only the content delivery network does that.

What i did recently looks like this:

public class RewriteFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;

        log.info("Proxy Servlet for: " + httpServletRequest.getRequestURI());

        byte[] buffer = new byte[1024];
        try (InputStream is = new FileInputStream("index.html"); OutputStream os = httpServletResponse.getOutputStream()) {
            int read = is.read(buffer);
            while (read > 0) {
                os.write(buffer, 0, read);
                read = is.read(buffer);
            }

        }

        // chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}

While being really ugly, this works, even if just for the index.html page (well i could change that with filter init params and defining multiple filters).

But why does it work where the UrlRewriteFilter doesn't?

The answer is quite simple. This filter servlet doesn't forward internally to static content but serves the content itself. Since this filter will only become active for urls i specify by <url-pattern> below <filter-mapping> in the web.xml, i can live with the slight performance dip when serving through this filter. If anyone found a better way to do this, please leave a comment.

这篇关于使用UrlRewriteFilter从页面中删除.html扩展名? - 使用GAE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆