0

I tried to set default servlet by this way:

@WebServlet({"/abc", "","/"})
public class GreetingServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {
        getServletContext().getRequestDispatcher("/asd/ind.html").forward(request,response);
    }

    @Override
    public String getServletInfo() {
        return "The Hello servlet says hello.";

    }
}

The file /asd/ind.html is exist, when I remove "/" like :

@WebServlet({"/abc", ""})

the redirection works fine when I hit:

contextpath/abc contextpath/ contextpath

But with "/" like

@WebServlet({"/abc", "","/"})

It turns out

javax.servlet.ServletException: AS-WEB-CORE-00089

When I hit any URL at all, even the previous ones that did work.

Could anyone please give me an explanation? the "/" should make the servlet default which means any unmapped URL should redirected to GreetingServlet.

Aladdin
  • 1,207
  • 2
  • 16
  • 26
  • "/" does not match any unmapped url. It just means that when you don't supply a path (e.g. www.example.com) it gets assigned to that servlet. – krato Mar 07 '16 at 11:01
  • This is working for me, there is no problem with / but if you use /*, AS-WEB-CORE-00089 is thrown because of redirect loop. Can you check it. Maybe your web.xml have /* – Yusuf K. Mar 07 '16 at 11:23
  • @krato As per servlet specidication A string containing only the / character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null. – Aladdin Mar 07 '16 at 13:35
  • @YusufK. no web.xml hasn't .. I tried to remove it at all and the same problem. – Aladdin Mar 07 '16 at 13:36
  • Why do you need to add '/' ? – Yusuf K. Mar 07 '16 at 13:41
  • @YusufK., kindly I know this could be done by welcome page, but I need to know what's the problem of above code, for now I'm pretty sure the problem is from forward because I commented out it and print test message and it works fine. – Aladdin Mar 07 '16 at 13:49
  • @user3305603 I tried your code on my server(glassfish 4.1.1). Only '/*'is causing the AS-WEB-CORE-00089 exception, maybe some definitions of your server may cause that. – Yusuf K. Mar 07 '16 at 13:51
  • @YusufK. did you try that with : getServletContext().getRequestDispatcher("/asd/ind.html").forward(request,response); ?? – Aladdin Mar 07 '16 at 13:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105565/discussion-between-yusuf-k-and-aladdin). – Yusuf K. Mar 07 '16 at 13:54
  • Related: http://stackoverflow.com/q/4140448 – BalusC Mar 07 '16 at 19:26

1 Answers1

1

The problem happened because the default servlet maps the URI to the static path if it's not mapped to any servlet, but what I did is redefining the default servlet, so when I redirect to /asd/ind.html and this URI is not mapped to any servlet the default serlvet is called and in this case the default one is GreetingServlet, this causes infinite loop.

Aladdin
  • 1,207
  • 2
  • 16
  • 26