Servlet @WebServlet urlPatterns
Asked Answered
V

2

10

This is a quick question but I couldn't find a quick answer. Now I have a servlet BaseServlet, when user request any of the url below:

host
host/
host/BaseServlet

It should always refer to the same servlet and redirect to the homepage.

When I set

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

Only

host/
host/BaseServlet

works

If I set

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

The BaseServlet will be requested constantly in loop ...

Why?

Edit: BaseServlet does a forward to the index.html hid in WEB-INF folder and that's it.

getServletContext().getRequestDispatcher("/WEB-INF/index.html").forward(request,response);

The servlet spec says "A string containing only the / character indicates the "default" servlet of the application." So I want the BaseServlet to be my default. Why it doesn't work?

Vidovic answered 16/5, 2013 at 3:49 Comment(3)
What do you do with the response?Stipple
Do some pre-processing, and then response with a webpage.Vidovic
That webpage might be making the servlet re-execute in the loop you spoke about. Show us.Stipple
J
12
  1. As you state in your Q, if you want the following:

    host/
    host/BaseServlet
    

    Use

    @WebServlet({"/BaseServlet", ""})
    
  2. If you want the following:

    host
    

    Add this to your welcome file (you can't specifiy welcome files using annotations)

    <welcome-file-list>
        <welcome-file>/BaseServlet</welcome-file>
    </welcome-file-list>
    
  3. The servlet spec says "A string containing only the '/' character indicates the "default" servlet of the application."

    But it says straight afterwards

    In this case the servlet path is the request URI minus the context path and the path info is null.

    In other words, if your URL is

    host
    

    then the servlet path will be

    "" (empty string)
    

    so you will need a welcome file list (but index.htm[l] and index.jsp in the webapp directory, not WEB-INF, are included implicitly as a starting welcome file list)

Jermainejerman answered 28/6, 2013 at 3:16 Comment(0)
H
2

Edit:

If you want to pre-process then you can use Filter with url-pattern "/*" and dispatcher set to REQUEST that way it will ignore forward.

Last value "/" means all request.

Checkout discussion at: http://www.coderanch.com/t/366340/Servlets/java/servlet-mapping-url-pattern

And inside Servlet another forward request for index.html is generated which is also intercepted by servlet.

If you try @WebServlet({"/BaseServlet", "/"}) which is same as @WebServlet({"/BaseServlet", "", "/"}) will result in same error.

You can check this by typing following output statement in servlet:

System.out.println(req.getRequestURL());
Hekate answered 22/5, 2013 at 11:38 Comment(1)
Thanks. Sorry I didn't reply in time but I don't really understand what you are saying. The servlet spec says "A string containing only the / character indicates the "default" servlet of the application." So I want the BaseServlet to be my default. Why it doesn't work?Vidovic

© 2022 - 2024 — McMap. All rights reserved.