Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servlet @WebServlet urlPatterns

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?

like image 332
kakacii Avatar asked May 16 '13 03:05

kakacii


People also ask

What is the use of @WebServlet annotation?

Use the @WebServlet annotation to define a servlet component in a web application. This annotation is specified on a class and contains metadata about the servlet being declared. The annotated servlet must specify at least one URL pattern. This is done by using the urlPatterns or value attribute on the annotation.

How do I map a servlet in web XML?

Configuring and Mapping a Servlet This is done using the <servlet> element. Here you give the servlet a name, and writes the class name of the servlet. Second, you map the servlet to a URL or URL pattern. This is done in the <servlet-mapping> element.

What's a WebServlet?

A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers.

Can we configure multiple servlets in web XML file?

You can declare multiple servlets using the same class with different initialization parameters. The name for each servlet must be unique across the deployment descriptor.


2 Answers

  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)

like image 69
Glen Best Avatar answered Oct 12 '22 15:10

Glen Best


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());
like image 31
Snehal Patel Avatar answered Oct 12 '22 15:10

Snehal Patel