Java – Cannot redirect with the response.sendRedirect

javajspjstlregexservlets

I googled and googled for hours on how to make a redirect in jsp or servlets.
However when i try to apply it, it doesn't work.

Code that i have inside jsp page:

<%
    String articleId = request.getParameter("article_id").toString();
    if(!articleId.matches("^[0-9]+$"))
    {
       response.sendRedirect("index.jsp");
    }
%>

I know from debugging that regexp works and if any time, articleId is not number, the if goes inside, however when it reaches response.sendRedirect it doesn't actually makes redirect.

Do I miss something very fundamental in this case ?

Thanks in advance.

Best Solution

You should return after redirecting:

response.sendRedirect("index.jsp");
return;