Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5935879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:19:05+00:00 2026-05-22T15:19:05+00:00

I have a servlet filter in my Java app to ensure that users are

  • 0

I have a servlet filter in my Java app to ensure that users are using up-to-date URI for articles and categories. The problem is, that according to the profiler results this filter takes (self) about 40% of the total time for request (even for simple URI “/”) (the inner actions are non-trivial, its dynamic web page with huge menu, article ranking etc.).

public class NameFilter implements Filter {

    private ArticleServiceIface articleService;
    private CategoryServiceIface categoryService;
    private UrlRewriteServiceIface urlRewriteService;
    private Pattern pattern = Pattern.compile("^(?>.*?)/(article|category)/(\\d+)/(?>.*)$");

    public void init(FilterConfig filterConfig) throws ServletException {
        ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(filterConfig.getServletContext());
        articleService = (ArticleServiceIface) ctx.getBean("articleService");
        categoryService = (CategoryServiceIface) ctx.getBean("categoryService");
        urlRewriteService = (UrlRewriteServiceIface) ctx.getBean("urlRewriteService");
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        String uri = ((HttpServletRequest) request).getRequestURI();
        Matcher matcher = pattern.matcher(uri);
        String currUri;
        if (matcher.matches()) {
            if (matcher.group(1).equals("article")) {
                Long articleId = Long.valueOf(matcher.group(2));

                ArticleDTO a = articleService.getById(articleId);
                currUri = urlRewriteService.getUrl(a.getId());
            } else {
                Long categoryId = Long.valueOf(matcher.group(2));

                CategoryDTO c = categoryService.getById(categoryId);
                currUri = urlRewriteService.getCategoryUrl(c.getId());
            }
        } else { //does not match neighter article nor category
            chain.doFilter(request, response);
            return;
        }
        if (currUri.equals(uri)) {
            chain.doFilter(request, response);
        } else {
            HttpServletResponse res = (HttpServletResponse) response;
            res.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
            res.setHeader("Location", currUri);
            res.getWriter().close();
        }


    }

    public void destroy() {
    }
}

I have spend few hours debugging and profiling it, tried many different ways to formulate the regexp, but the results are always the same.

The bottlenect seems to be in the matches method, which gets called resursively and at some point it calls pattern matching iteratively (few thousand times) for some reason…

Thanks for any suggestions.

edit: Profiler results (seems pretty strange to me…according to debugger this should be parsing of URI == “/” )


EDIT2: current regexp:

 private static Pattern pattern = Pattern.compile(".*?/(article|category)/(\\d+)/.*");

the results are still the same. I’ll try to measure it with

  System.out.print(System.currTimeMillis - time)

EDIT3: conclusion: its probably netbeans profiler bug…

I have tried this code and URI “/”

    long time = System.currentTimeMillis();
    if (matcher.matches()) {
        if (matcher.group(1).equals("article")) {
            Long articleId = Long.valueOf(matcher.group(2));

            ArticleDTO a = articleService.getById(articleId);
            currUri = urlRewriteService.getUrl(a.getId());
        } else {
            Long categoryId = Long.valueOf(matcher.group(2));

            CategoryDTO c = categoryService.getById(categoryId);
            currUri = urlRewriteService.getCategoryUrl(c.getId());
        }
    } else { //does not match neighter article nor category
        System.out.println(System.currentTimeMillis() - time);
        ....

The output is allways 0. So it seems to me that netbeans profiler is adding time to this method for some reason.

But thank you all for your help and cooperation, I have learned few regex tricks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:19:06+00:00Added an answer on May 22, 2026 at 3:19 pm

    There is no need to use Lookbehinds in your pattern actually. Following code works for me and in fairly quick time:

    long l = System.currentTimeMillis();
    Pattern p = Pattern.compile("^.*?/(article|category)/(\\d+)/.*$");
    Matcher m = p.matcher("/category/1012/Grafy");
    System.out.println("Matches: " + m.matches());
    System.out.println("Group1: " + m.group(1) + ", Group2: " + m.group(2));
    System.out.println("Time taken: " + (System.currentTimeMillis()-l));
    

    OUTPUT

    Matches: true
    Group1: category, Group2: 1012
    Time taken: 0
    

    EDIT Try find() intead of matches() like this:

    long l = System.currentTimeMillis();
    p = Pattern.compile("/(article|category)/(\\d+)/");
    m = p.matcher("/en/article/123/articleName");
    System.out.println("Matches: " + m.find());
    System.out.println("Group1: " + m.group(1) + ", Group2: " + m.group(2));
    System.out.println("Time taken: " + (System.currentTimeMillis()-l));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have standard servlet-mapping for Dispatcher Servlet - /app/* . I have controller that
I have a servlet that is used for many different actions, used in the
I have a servlet that I would like to run within ColdFusion MX 7.
I have a Java servlet which generates XML, translates it with an XSLT stylesheet,
I have a file upload form that is being posted back to a servlet
I have many years of experience in Java including Swing, Servlet and JDBC, but
I have inherited a Java application (servlets) that runs under Tomcat. For historical reasons,
I have a tomcat server proxied by lighttpd 1.4. The problem is, that HttpServleTRequest.getRemoteAddr
When you use JSF, you'll have the controller servlet javax.faces.webapp.FacesServlet that will be mapped
I'm new to Java and have been asked to create an applet/servlet (not sure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.