title: When do (jsp) scriptlets run their (Java) code?
body: <p>I was working through a null pointer exception on code like the following:</p>
  
  <pre><code>&lt;%
  SessionData session = getSessionData(request);
  Webpage webPage = null;
  if (session!= null) {
      webPage = session.getWebPage();
  }
  %&gt;
  
  &lt;script type="text/javascript"&gt; 
  
  //NullPointer happens here, webPage is null when the session is lost
  &lt;tags:ComboBox
      comboBox="&lt;%=webPage.getComboBox()%&gt;" /&gt;
  
  &lt;/script&gt;
  </code></pre>
  
  <p>I was surprised when I could move the ending of <code>if (session!=null</code> to after the javascript, which seems to ignore that code when the session was null.</p>
  
  <pre><code>&lt;%
  SessionData session = getSessionData(request);
  Webpage webPage = null;
  if (session!= null) {
      webPage = session.getWebPage();
  //} move this to below
  %&gt;
  
  &lt;script type="text/javascript"&gt; 
  
  //NullPointer happens here, webPage is null when the session is lost
  &lt;tags:ComboBox
      comboBox="&lt;%=webPage.getComboBox()%&gt;" /&gt;
  
  &lt;/script&gt;
  &lt;% } %&gt; //moved to here
  </code></pre>
  
  <p>Does the scriptlet for the <code>ComboBox</code> tag, inside the brackets, no longer run? I would think it would still try to get the combobox off the webpage, and still end up getting a null pointer. Am I incorrect in thinking that scriptlets all get their values before the code is actually ran?</p>
  
  <p>(<em>just thought I'd mention, there is an included script which redirects the page if there is no session. I get a NullPointer with the first section of code, and correctly redirect with the second section</em>)</p>