SORU
26 HAZİRAN 2010, CUMARTESİ


≪url-pattern> içinde <filtre eşleme> bazı somut URL dışarıda bırakabilir miyim?

Bazı somut filtre bir beton hariç tüm adresler (/specialpath dışında /* için yani) için uygulanmasını istiyorum.

Bunu yapmak için bir olasılık var mı?


örnek kod:

<filter>
    <filter-name>SomeFilter</filter-name>
    <filter-class>org.somproject.AFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>SomeFilter</filter-name>
    <url-pattern>/*</url-pattern>   <!-- the question is: how to modify this line?  -->
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

CEVAP
26 HAZİRAN 2010, CUMARTESİ


Standart Sunucu API bu tesis desteklemiyor. İsteyebilirsiniz, ya da bir yeniden-URL filtresi için böyle Tuckey's one (ki, bu daha çok benzer Apache mod_rewrite) veya Ekle bir kontrol doFilter() yöntemin Filtre dinleme /*.

String path = ((HttpServletRequest) request).getRequestURI();
if (path.startsWith("/specialpath")) {
    chain.doFilter(request, response); // Just continue chain.
} else {
    // Do your business stuff here for all paths other than /specialpath.
}

web.xml yine de kontrol edebilirsiniz gerekirse filtre init-param olarak yolları-göz ardı belirtebilirsiniz. Aşağıdaki gibi filtre alabilirsiniz

private String pathToBeIgnored;

public void init(FilterConfig config) {
    pathToBeIgnored = config.getInitParameter("pathToBeIgnored");
}

Güncellemefiltre 3. parti API olması söz konusu olduğundan bahsetmiş olması) gibi görünüyor. Bu durumda, daha özel url-pattern örneğin /otherfilterpath/* bir haritaya ve ileri /* dinlediğini hangi filtre izin verin.

String path = ((HttpServletRequest) request).getRequestURI();
if (path.startsWith("/specialpath")) {
    chain.doFilter(request, response); // Just continue chain.
} else {
    request.getRequestDispatcher("/otherfilterpath"   path).forward(request, response);
}

Güncelleme 2:az daha unutuyordum eklemek için önlemek bu filtre Ara kendi içinde sonsuz bir döngüye ihtiyacınız için izin dinle (gönderme) REQUEST sadece 3 parti filtre FORWARD sadece.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • chickenby

    chickenby

    2 HAZİRAN 2008
  • Matthew Smith

    Matthew Smit

    24 Mayıs 2010
  • TheXiaxue

    TheXiaxue

    3 AĞUSTOS 2009