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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
theapache64
  • 10,926
  • 9
  • 65
  • 108
  • it should work, what is the error your getting.? – ihappyk Aug 15 '15 at 06:13
  • No error, but it's working for both the urls. I just want to work the servlet if there's anything after **example.com/A**. – theapache64 Aug 15 '15 at 06:14
  • you mean to say it should work for example.com/a/* is it.? – ihappyk Aug 15 '15 at 06:18
  • The path is always relative to your web application context, typically this is the same as your project name: `example.com//A/*`... – home Aug 15 '15 at 06:19
  • then you can use example.com/a* – ihappyk Aug 15 '15 at 06:22
  • @iHappyk : It should work **only for** example.com/a/* – theapache64 Aug 15 '15 at 06:22
  • That's not possible via mapping URL pattern alone. How exactly do you want `example.com/A` to behave? Then we can post an answer for that. – BalusC Aug 15 '15 at 06:25
  • @home : The deployment root is fine.All other components working perfectly. – theapache64 Aug 15 '15 at 06:25
  • @BalusC : The **TestServlet** should only work when there's something after the **example.com/A/**, like **A/car** , **A/bat**, **A/cat/bat/rat**, is it possible? . The problem is it's still working even if there's nothing but just **example.com/A** – theapache64 Aug 15 '15 at 06:30
  • Yes. I already understand that part. I didn't ask that. I asked you how exactly the servlet should behave when `example.com/A` without pathinfo is requested. Should it return a 404 not found or so? Or should it forward to a specific resource? Etc. Then it's simply a matter of writing that code in servlet accordingly. Or, if this servlet is beyond your control, then via a filter. – BalusC Aug 15 '15 at 06:36
  • a 404 would be better. – theapache64 Aug 15 '15 at 06:37

1 Answers1

2

Just invoke HttpServletResponse#sendError() with SC_NOT_FOUND (404) when HttpServletRequest#getPathInfo() is null or empty or equals to /.

@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)
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555