title: Checking if user is logged in or not using sessions: java servlet
body: <p>I have two type of users: admin and user.
  Here's what I am doing in <strong>Login servlet</strong></p>
  
  <pre><code>protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      PrintWriter out = response.getWriter();
      String user=request.getParameter("user");
      String pass=request.getParameter("pass");
  
      HttpSession session=request.getSession();
  
  
      String name="user",type;
      try
      {
          Class.forName("com.mysql.jdbc.Driver");
          Connection con=DriverManager.getConnection(url,user,pass);
  
          PreparedStatement ps=con.prepareStatement("select utype from users where email=? and pass=?");
  
          ps.setString(1, user);
          ps.setString(2, pass);
  
          ResultSet rs=ps.executeQuery();
  
          if(rs.next()) 
          {
              type=rs.getString(1);
              if(type.equals("a"))
              {
                  session.setAttribute("loggedIn", "admin");
                  RequestDispatcher rd=request.getRequestDispatcher("admin.jsp");
                  rd.forward(request, response);
              }
              else
              {
                  session.setAttribute("loggedIn", "user");
                  RequestDispatcher rd=request.getRequestDispatcher("home.jsp");
                  rd.include(request, response);
              }
  
  
          }
          else
          {
              request.setAttribute("message","Username or password error!");
                  RequestDispatcher rd=request.getRequestDispatcher("login.jsp");
              rd.include(request, response);
          }
      }
      catch(Exception e)
      {
          e.printStackTrace();
      }
  
  }
  </code></pre>
  
  <p>And on <strong>account.jsp</strong> page I am checking is user is logged in or not.Here's the code:</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%&gt;
   &lt;%@ include file="headpl.jsp" %&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
          &lt;script&gt;
                //have some code here
          &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
  
      &lt;%
          if(session.getAttribute("loggedIn").equals("admin"))
              {
                  response.sendRedirect("admin.jsp");
            }else
             if(!session.getAttribute("loggedIn").equals("user"))
          {
              response.sendRedirect("index.jsp");
           }  
     %&gt;
       //some html code here
    &lt;/body&gt;
    &lt;/html&gt;
  </code></pre>
  
  <p>The code was running fine before I added the session checks.
  Now, when the admin is logged in and types the URL of the account page, he is redirected to <strong>admin.jsp</strong> (that's fine), when user is logged in he is allowed t go to <strong>account page</strong>  , But when no one is logged in I get an exception:</p>
  
  <pre><code>  exception
  
  org.apache.jasper.JasperException
  org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:370)
  org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
  org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
  root cause
  
  java.lang.NullPointerException
  org.apache.jsp.account_jsp._jspService(org.apache.jsp.account_jsp:128)
  org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
  org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
  org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
  org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
  note The full stack trace of the root cause is available in the Apache Tomcat/5.5.11 logs.
  </code></pre>
  
  <p>I think that the <code>NullPointerException</code> is due to the fact that no session variable exists since No one is logged in. I saw several answers on SO but none of them worked for me.</p>
  
  <p>Any suggestions?</p>