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 8364863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:31:51+00:00 2026-06-09T12:31:51+00:00

I would like to have a Servlet handle all requests to JSP pages first.

  • 0

I would like to have a Servlet handle all requests to JSP pages first. The Servlet will be used to set server side variables depending on the requested JSP.

For example, what I would like to achieve is given the url:example.com/index.jsp, the Servlet first handles the request by parsing out the requested JSP (index.jsp), sets variables that are specific to the requested index.jsp (using request.setAttribute) and then dispatches to the actual index.jsp (eg. /WEB-INF/index.jsp). The JSP will then have the correct variables it needs.

My problem so far is that I’m using “/*” as the mapping pattern for my Servlet. It handles the request, then uses requestDispatcher.forward("/WEB-INF/index.jsp") which ends up in an infinite loop since this matches the “/*” pattern as well.

How should my Servlet(s) handle the requested url? What should the I use as the mapping pattern(s) in web.xml?

Is there a standard setup for this? I’m just looking for a “best practices” way to set up pre-processing for all my JSPs.

  • 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-06-09T12:31:52+00:00Added an answer on June 9, 2026 at 12:31 pm

    The /* is in first place a strange choice for a servlet URL pattern. This is normally exclusively to be used by filters. Servlets are by default namely also invoked on forwards and includes, but filters not. Using /* would completely override the container’s builtin JspServlet which ought to be invoked on *.jsp during a forward.

    Rather use a more specific URL pattern like /pages/*, /app/*, *.do, *.html, etcetera.

    When using a prefix servlet mapping, like /pages/* and you would like to hide the extra path from the URL, then you should keep the prefix servlet mapping as is, put all other resources in a common path (usually it are only the static resources like CSS/JS/images) and create an additional filter which checks if it’s a resource request and if so, then continue the chain or if not, then forward to the servlet. This does not change the URL. The servlet can in turn just safely forward to the JSP.

    Here’s an example assuming that your servlet is mapped on /pages/* and that all your (static) resources which should not be handled by the servlet are placed in /resources folder (you can just keep your JSPs in /WEB-INF, that part doesn’t need to be changed; the forward wouldn’t hit the filter anyway).

    HttpServletRequest req = (HttpServletRequest) request;
    String path = req.getRequestURI().substring(req.getContextPath().length());
    
    if (path.startsWith("/resources/")) {
        chain.doFilter(request, response);
    } else {
        request.getRequestDispatcher("/pages" + path).forward(request, response);
    }
    

    Finally just map the above filter on an URL pattern of /*.

    See also:

    • URL mapping in Tomcat to FrontController servlet
    • Design Patterns web based applications
    • Difference between / and /* in servlet mapping url pattern
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have a web server displaying the status of 2 of
I have a Servlet that sends back a JSON Object and I would like
I would like to load a jsp file from a servlet-class I got in
I have Java servlet-based web applications. I would like to implement some operations in
I have a servlet that I would like to run within ColdFusion MX 7.
We have a servlet that we post a zip file to. I would like
I would like to send only the requests ending in .html through dispatcher-servlet and
So in my project i would like have a nice treeview that has images.
I would like to have an AppWidget that designed like this one . Image:
I would like to have a more colorful Python prompt in the terminal, just

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.