How to check if requested URL is a servlet (in a filter)?












0















I have a filter that maps onto all requests:



@WebFilter({ "/*" })


I use this filter to do url mapping so that i can have vanity urls for my jsps and to handle 404s, however it also means i have to manually add the servlets to process the request chain normally:



String requri = ((HttpServletRequest) request).getRequestURI(); //get requested url
String page = "";

if(requri.equals("/contact/") || requri.equals("/contact"))
page = "contact.jsp";

else if(requri.equals("/about/") || requri.equals("/about"))
page = "about.jsp";

else if(requri.equals("/SomeServlet"))
chain.doFilter(request, response); //if requested url is a servlet, then carry on normally

//send requested url to servlet which will then forward user to the appropriate page
request.getRequestDispatcher("/GetPage?page="+page).forward(request, response);
return;


It all works great.



But is there a way to get a list of the url mappings for all the servlets? So that i don't have to manually list each url mapping for the servlets.. i.e:



else if(requri.equals("/SomeServlet") || requri.equals("/AnotherServlet") || requri.equals("/SomeOtherServlet"))
chain.doFilter(request, response); //if requested url is a servlet, then carry on normally


The ideal:



else if(isAServlet(requri))
chain.doFilter(request, response); //if requested url is a servlet, then carry on normally









share|improve this question





























    0















    I have a filter that maps onto all requests:



    @WebFilter({ "/*" })


    I use this filter to do url mapping so that i can have vanity urls for my jsps and to handle 404s, however it also means i have to manually add the servlets to process the request chain normally:



    String requri = ((HttpServletRequest) request).getRequestURI(); //get requested url
    String page = "";

    if(requri.equals("/contact/") || requri.equals("/contact"))
    page = "contact.jsp";

    else if(requri.equals("/about/") || requri.equals("/about"))
    page = "about.jsp";

    else if(requri.equals("/SomeServlet"))
    chain.doFilter(request, response); //if requested url is a servlet, then carry on normally

    //send requested url to servlet which will then forward user to the appropriate page
    request.getRequestDispatcher("/GetPage?page="+page).forward(request, response);
    return;


    It all works great.



    But is there a way to get a list of the url mappings for all the servlets? So that i don't have to manually list each url mapping for the servlets.. i.e:



    else if(requri.equals("/SomeServlet") || requri.equals("/AnotherServlet") || requri.equals("/SomeOtherServlet"))
    chain.doFilter(request, response); //if requested url is a servlet, then carry on normally


    The ideal:



    else if(isAServlet(requri))
    chain.doFilter(request, response); //if requested url is a servlet, then carry on normally









    share|improve this question



























      0












      0








      0








      I have a filter that maps onto all requests:



      @WebFilter({ "/*" })


      I use this filter to do url mapping so that i can have vanity urls for my jsps and to handle 404s, however it also means i have to manually add the servlets to process the request chain normally:



      String requri = ((HttpServletRequest) request).getRequestURI(); //get requested url
      String page = "";

      if(requri.equals("/contact/") || requri.equals("/contact"))
      page = "contact.jsp";

      else if(requri.equals("/about/") || requri.equals("/about"))
      page = "about.jsp";

      else if(requri.equals("/SomeServlet"))
      chain.doFilter(request, response); //if requested url is a servlet, then carry on normally

      //send requested url to servlet which will then forward user to the appropriate page
      request.getRequestDispatcher("/GetPage?page="+page).forward(request, response);
      return;


      It all works great.



      But is there a way to get a list of the url mappings for all the servlets? So that i don't have to manually list each url mapping for the servlets.. i.e:



      else if(requri.equals("/SomeServlet") || requri.equals("/AnotherServlet") || requri.equals("/SomeOtherServlet"))
      chain.doFilter(request, response); //if requested url is a servlet, then carry on normally


      The ideal:



      else if(isAServlet(requri))
      chain.doFilter(request, response); //if requested url is a servlet, then carry on normally









      share|improve this question
















      I have a filter that maps onto all requests:



      @WebFilter({ "/*" })


      I use this filter to do url mapping so that i can have vanity urls for my jsps and to handle 404s, however it also means i have to manually add the servlets to process the request chain normally:



      String requri = ((HttpServletRequest) request).getRequestURI(); //get requested url
      String page = "";

      if(requri.equals("/contact/") || requri.equals("/contact"))
      page = "contact.jsp";

      else if(requri.equals("/about/") || requri.equals("/about"))
      page = "about.jsp";

      else if(requri.equals("/SomeServlet"))
      chain.doFilter(request, response); //if requested url is a servlet, then carry on normally

      //send requested url to servlet which will then forward user to the appropriate page
      request.getRequestDispatcher("/GetPage?page="+page).forward(request, response);
      return;


      It all works great.



      But is there a way to get a list of the url mappings for all the servlets? So that i don't have to manually list each url mapping for the servlets.. i.e:



      else if(requri.equals("/SomeServlet") || requri.equals("/AnotherServlet") || requri.equals("/SomeOtherServlet"))
      chain.doFilter(request, response); //if requested url is a servlet, then carry on normally


      The ideal:



      else if(isAServlet(requri))
      chain.doFilter(request, response); //if requested url is a servlet, then carry on normally






      java servlets java-ee






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 17:55







      Jonathan Laliberte

















      asked Nov 13 '18 at 17:39









      Jonathan LaliberteJonathan Laliberte

      1,6042824




      1,6042824
























          1 Answer
          1






          active

          oldest

          votes


















          1














          If you are in JEE >= JEE 7 there is ServletContext.getServletRegistrations(). It returns a map from servlet names to ServletRegistration objects. These have a getMappings() method that gets you the URL mappings for that servlet. You will probably have to process these a bit, e.g. apply the servlet URL mapping rules manually, to see which servlet, if any, would get called.






          share|improve this answer
























          • That's brilliant, just what was needed. Thanks!

            – Jonathan Laliberte
            Nov 14 '18 at 11:15











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286677%2fhow-to-check-if-requested-url-is-a-servlet-in-a-filter%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          If you are in JEE >= JEE 7 there is ServletContext.getServletRegistrations(). It returns a map from servlet names to ServletRegistration objects. These have a getMappings() method that gets you the URL mappings for that servlet. You will probably have to process these a bit, e.g. apply the servlet URL mapping rules manually, to see which servlet, if any, would get called.






          share|improve this answer
























          • That's brilliant, just what was needed. Thanks!

            – Jonathan Laliberte
            Nov 14 '18 at 11:15
















          1














          If you are in JEE >= JEE 7 there is ServletContext.getServletRegistrations(). It returns a map from servlet names to ServletRegistration objects. These have a getMappings() method that gets you the URL mappings for that servlet. You will probably have to process these a bit, e.g. apply the servlet URL mapping rules manually, to see which servlet, if any, would get called.






          share|improve this answer
























          • That's brilliant, just what was needed. Thanks!

            – Jonathan Laliberte
            Nov 14 '18 at 11:15














          1












          1








          1







          If you are in JEE >= JEE 7 there is ServletContext.getServletRegistrations(). It returns a map from servlet names to ServletRegistration objects. These have a getMappings() method that gets you the URL mappings for that servlet. You will probably have to process these a bit, e.g. apply the servlet URL mapping rules manually, to see which servlet, if any, would get called.






          share|improve this answer













          If you are in JEE >= JEE 7 there is ServletContext.getServletRegistrations(). It returns a map from servlet names to ServletRegistration objects. These have a getMappings() method that gets you the URL mappings for that servlet. You will probably have to process these a bit, e.g. apply the servlet URL mapping rules manually, to see which servlet, if any, would get called.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 21:27









          Nikos ParaskevopoulosNikos Paraskevopoulos

          33.9k96976




          33.9k96976













          • That's brilliant, just what was needed. Thanks!

            – Jonathan Laliberte
            Nov 14 '18 at 11:15



















          • That's brilliant, just what was needed. Thanks!

            – Jonathan Laliberte
            Nov 14 '18 at 11:15

















          That's brilliant, just what was needed. Thanks!

          – Jonathan Laliberte
          Nov 14 '18 at 11:15





          That's brilliant, just what was needed. Thanks!

          – Jonathan Laliberte
          Nov 14 '18 at 11:15


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286677%2fhow-to-check-if-requested-url-is-a-servlet-in-a-filter%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bressuire

          Vorschmack

          Quarantine