Java - Checking for Cookies on jsp pages
<p>I'm making a web application that requires login authentication and I'm using cookies to do this. When you login, it creates a cookie. When you access any of the jsp pages I have this code to check if the cookie exists:</p>

<pre><code>&lt;%
String userName = null;

Cookie[] cookies = request.getCookies();
if(cookies !=null){
    for(Cookie cookie : cookies){
        if(cookie.getName().equals("ADMIN")) userName = cookie.getValue();
    }
}
if(userName == null) response.sendRedirect("login.jsp");
%&gt;
</code></pre>

<p>It works but I don't want to have to put this on all my jsp pages because it's unproductive. If I have to make a change to any of it, I want to have the code in a class/servlet/filter. I have tried putting it in filters and servlets, but I have had no luck in getting it to work. Does anyone have any suggestions?</p>