Question

I have a custom router and forward requests to specified servlets after some processes. So servlets are actually private in web.xml but router itself.

Is it possible to not set url-pattern for a servlet and make it unaccessible from web? Currently i'm using a big hash as url-pattern and checking some flags which don't exist in direct requests.

Was it helpful?

Solution

in answer to your very nice comment, here you are:

@WebServlet(urlPatterns = "/WEB-INF/test")
public class Test extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        resp.getWriter().println("helloooooooooooo");
    }

}

and

@WebServlet(urlPatterns = "/router")
public class Router extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        request.getRequestDispatcher("/WEB-INF/test").forward(request, response);
    }
}

and this works perfectly:

screenshot

while test servlet is not accessible

screenshot2

however, i suggest you to be nicer with people trying to help you :)

OTHER TIPS

Since youre classes are not really used as Servlets I would suggest that they are not implemented as servlets. Move away from mapping in your web.xml and use a HasMap<String, Class> instead for the mapping.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top