I need a method which must check if the session has a specific attribute or not. The problem is that when I want to check if the session has the attribute I get a null pointer exception. I think that I get this error because the session object is not initialized properly. I need this method because I want to render the page base on this attribute (if the user is logged in he must to show the button logout and vice versa). What I am doing wrong in my code?
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
public class SessionTest extends HttpServlet{
HttpSession session;
@PostConstruct
public void init(HttpServletRequest request) {
session = request.getSession(true);
session.setAttribute("user", "username");
}
public boolean getParamer(String user) {
return session.getAttribute(user) == null;
}
public static void main(String[] args) {
System.out.println(new SessionTest().getParamer("user"));
}
}