1

I am new in Servlets. I am trying to map a URL using the wildcard (*), but it's not working the way i expected.

Here is my servlet class.

@WebServlet(urlPatterns = {"/A/*"})
public class TestServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().write("Working...");
    }
}

The above servlet is working for both example.com/A and example.com/A/car. I want to work the servlet only for the the second option which is example.com/A/whatEver. How can i do that ?

In simple: I just want to work the servlet if there's anything after example.com/A.

Any help will greatly appreciated.

4

1 回答 1

2

只需在为 null 或为空等于使用HttpServletResponse#sendError()( SC_NOT_FOUND404)调用。HttpServletRequest#getPathInfo()/

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String pathInfo = request.getPathInfo();

    if (pathInfo == null || pathInfo.isEmpty() || pathInfo.equals("/")) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }

    // ... (continue)
}
于 2015-08-15T06:38:17.470 回答