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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:50:03+00:00 2026-05-18T11:50:03+00:00

I’m setting up a web app on GlassFish and I’m wondering if there is

  • 0

I’m setting up a web app on GlassFish and I’m wondering if there is a way to configure servlet mapping from the URL root to only apply in cases where a file or directory doesn’t exist at the specified URL.

Currently, this will route everything through my Spring dispatcher:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           version="3.0">
 <servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>
   org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>
   /jsp/index.jsp
  </welcome-file>
 </welcome-file-list>
</web-app>

Is it somehow possible to preserve the above behavior unless a file or directory exists at a given path? Am I trying to solve this problem in the wrong place?

UPDATE: What about URL rewriting? Does GlassFish provide any degree of flexibility like Apache does with mod_rewrite? That way I could intercept things before it even reaches the servlet mapping stage.

  • 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-18T11:50:04+00:00Added an answer on May 18, 2026 at 11:50 am

    Hacky, but doable: map it as <error-page> on an <error-code> of 404.

    <servlet>
        <servlet-name>errorServlet</servlet-name>
        <servlet-class>com.example.ErrorServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>errorServlet</servlet-name>
        <url-pattern>/error</url-pattern>
    </servlet-mapping>
    
    <error-page>
        <error-code>404</error-code>
        <location>/error</location>
    </error-page>
    

    You might want to change the status code in the servlet whenever you want to return a “valid” response.

    An alternative is to let the Spring servlet listen on a more specific url-pattern. E.g. /pages/*. You could then if necessary bring a Filter in front (on /*) which does roughly the following:

    if (new File(servletContext.getRealPath(request.getRequestURI())).exists()) {
        chain.doFilter(request, response);
    } else {
        request.getRequestDispatcher("/pages" + request.getRequestURI()).forward(request, response);
    }
    

    so that you don’t see/need /pages in the URL.


    Update: as per your new question:

    UPDATE: What about URL rewriting? Does GlassFish provide any degree of flexibility like Apache does with mod_rewrite? That way I could intercept things before it even reaches the servlet mapping stage.

    The JSP/Servlet equivalent of Apache HTTPD’s mod_rewrite addon is the Tuckey’s UrlRewriteFilter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm interested in microtypography issues on the web. I want a tool to fix:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
I know there's a lot of other questions out there that deal with this

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.