title: JSP: How to check if a user is logged in?
body: <p><img src="https://i.stack.imgur.com/kWAUq.jpg" alt=""></p>
  
  <p>I'm learning Java Web, but I have some problems and I need help. 
  I use a template.jsp in which I include header.jsp, footer.jsp, login.jsp (the left side of the template ) and <code>${param.content}.jsp</code>. For each page named  X.jsp I made another jsp with the following content, because I want each page to have the same layout:</p>
  
  <pre><code>&lt;jsp:include page="template.jsp"&gt;
      &lt;jsp:param name="content" value="X"/&gt;
      &lt;jsp:param name="title" value="Start navigate"/&gt;`enter code here`
  &lt;/jsp:include&gt;
  </code></pre>
  
  <p>When I click on the Review link,for example, I want to be redirect to Review.jsp, but I have some problems. 
  In footer.jsp I have something like this:  </p>
  
  <pre><code>(...)
  &lt; a href =" Review.jsp "&gt; Review &lt;/a&gt;
  (...)
  </code></pre>
  
  <p>After login, I try to click Review, it sends me to the Review.jsp, but it shows me that I'm not logged in. I use Spring Framework, Tomcat 7, Eclipse, MySQL.  When I log in, I create a cookie:</p>
  
  <pre><code>String timestamp = new Date().toString();
  String sessionId = DigestUtils.md5Hex(timestamp);
  db.addSession(sessionId, username, timestamp);
  Cookie sid = new Cookie("sid", sessionId);
  response.addCookie(sid);
  </code></pre>
  
  <p>For each method, I have something like this (I saw this in a tutorial):</p>
  
  <pre><code>@RequestMapping(value = "/writeReview", method = RequestMethod.GET)
  public String nameMethod(@CookieValue("sid") String sid,...)
  </code></pre>
  
  <p>Using the sid, I can find out who's the user. I have a database with the user's account and some other tables. The problem is that when I click review or any other, it shows me that I'm not logged in. What can I do? </p>
  
  <p><strong>UPDATE</strong>:<br>
  I use JavaBean for user and login.jsp. The JSP that has the text box for the login, I have a GET method when I click on the button to log in. </p>
  
  <pre><code>@RequestMapping(value = "/login", method = RequestMethod.GET)
  public ModelAndView createCookie(
    @RequestParam("username") String username, @RequestParam("password") String password,
    HttpServletRequest request, HttpServletResponse response) throws SQLException {
        //code for creating the cookie here, after testing if the user is in my database
  }
  </code></pre>
  
  <p>For each page, I send the sid and I have an attribute for the model, username:</p>
  
  <pre><code>public String methodName(@CookieValue(required = false) String sid, Model model) {
      if (sid != null) {
          User user = getUserBySid(sid);
          model.addAttribute("user", user);
      }
      (..other data.)
      return "nameJSP.jsp";
  }
  </code></pre>
  
  <p>I check in each JSP if the username is not empty, so that's how I see if a user is logged in. The application goes well, it passes parameters if I don't click on the links from the header or footer. The problem is that I have to pass , let's say, a parameter from a JSP who's the actual content of the layout to the JSP referred by the footer and this JSP will be the next content of my layout. The layout only recognize content and title:</p>
  
  <pre><code>&lt;title&gt;${param.title}&lt;/title&gt;
  (I didn't paste all the code, I use a table &lt;table&gt;....)
  &lt;%@ include file="header.jsp"%&gt;
  &lt;%@ include file="login.jsp"%&gt;  
  &lt;jsp:include page="${param.content}.jsp"/&gt; 
  &lt;%@ include file="footer.jsp"%&gt;
  </code></pre>
  
  <p>So how can I include a parameter in this JSP which will be received from another JSP? Also it will have to be accessed by layout.jsp and sent to the footer or the header?</p>
  
  <pre><code>&lt;jsp:include page="layout.jsp"&gt;
      &lt;jsp:param name="content" value="X"/&gt;
      &lt;jsp:param name="title" value="Start navigate"/&gt;
  &lt;/jsp:include&gt;
  </code></pre>