title: Java JSP - scriptlets usage with if()-else()
body: <p>Good day, all.</p>
  
  <p>I have the following setup:</p>
  
  <ul>
  <li>A JSP that outputs an HTML form to browser, then when one of the buttons is clicked after user enters an amount in the
  text field,</li>
  <li>The data is sent to a servlet that performs certain calculations and sends result data back to the JSP.</li>
  <li>The JSP then shows the result data and keeps running so user can make more iterations using the program.</li>
  </ul>
  
  <p>I am trying to code the JSP such that when it runs, it first checks for existence of 2 session attributes, "balance"
  and "formattedBal". If these attributes do not exist (meaning this is the very first time running the JSP after the
  user just hit its URL address), the JSP execution must create and assign these session attributes. If these attributes
  do exist (meaning this was not the initial launch of the JSP but a consequent one, with servlet having reverted the user
  to the JSP and sent result data, hence non-null session attributes to it), the JSP must simply execute the HTML output,
  showing the values of said session attributes in appropriate coded places.</p>
  
  <p>The following is the code of the JSP:</p>
  
  <pre><code>&lt;html&gt;
      &lt;hr&gt;        &lt;!--Horizontal line.--&gt;
      &lt;title&gt;Online Bank ATM Simulator&lt;/title&gt;        &lt;!--Title to show on browser title bar.--&gt;
      &lt;h1 align = "center"&gt;Bank ATM Simulation&lt;/h1&gt;   &lt;!--Page heading, centered on page.--&gt;
  
      &lt;SCRIPT LANGUAGE = JAVASCRIPT&gt;
      &lt;!--
  
      function checkAttributes()
      {
      &lt;%
      // Set balance and formatted balance as session attributes.
      if(request.getSession().getAttribute("balance") = null &amp;&amp; request.getSession().getAttribute("formattedBal") = null)
      {
          request.getSession().setAttribute("balance", 0);
          request.getSession().setAttribute("formattedBal", "$0");
      }
      %&gt;
      }
      --&gt;
      &lt;/SCRIPT&gt;
  
      &lt;body onLoad = "checkAttributes()", "amount.focus()"&gt;               &lt;!--Set focus to the text-field.--&gt;
          &lt;form method = "POST" action = "../servlet/JSPBank"&gt;    &lt;!--Form method and submission address.--&gt;
              &lt;center&gt;        &lt;!--Tag to center the following output on page.--&gt;
              Amount: 
              &lt;input type = "text" name = "amount" id = "amount" size = "20"&gt;&lt;br&gt;&lt;br&gt; &lt;!--Amount text field.--&gt;
              Balance: 
              &lt;%=(String)request.getSession().getAttribute("formattedBal")%&gt; + "&lt;br&gt;&lt;br&gt;  &lt;!--Current formatted balance shown.--&gt;
              &lt;button name = "balButton" value = "Balance"&gt;Balance&lt;/button&gt;   &lt;!--"Balance" button.--&gt;
              &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;  &lt;!--Spacers.--&gt;
              &lt;button name = "depButton" value = "Deposit"&gt;Deposit&lt;/button&gt;   &lt;!--"Deposit" button.--&gt;
              &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;  &lt;!--Spacers.--&gt;
              &lt;button name = "withdrButton" value = "Withdraw"&gt;Withdraw&lt;/button&gt;  &lt;!--"Withdraw" button.--&gt;
              &lt;/center&gt;       &lt;!--Tag to end centering of output on page.--&gt;
          &lt;/form&gt;     &lt;!--End of form.--&gt;
      &lt;/body&gt;
      &lt;br&gt;
      &lt;hr&gt;        &lt;!--Horizontal line.--&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The problem is, I'm getting an error that points to line 11 - the &lt;% tag. Whether I use a function or not, the error
  remains and reads exactly as follows:</p>
  
  <p><strong>HTTP Status 500 -
  type Exception report
  message
  description The server encountered an internal error () that prevented it from fulfilling this request.
  exception
  org.apache.jasper.JasperException: Unable to compile class for JSP
  An error occured at line: 11 in the jsp file: /bank.jsp
  Generated servlet error:
  Syntax error on token "=", != expected
      org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
      org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
      org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:397)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
      org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:296)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)</strong></p>
  
  <p>I have been researching this and related errors, but to not much avail.</p>
  
  <p>What makes no sense is the error states that I am using an = sign, but it expects an != combination, while as you see
  line pointed to there is never an = sign in the first place.</p>
  
  <p>I'm new to Java and it is possible this is a simple mistake somewhere, could you please help to identify it?</p>
  
  <p>I need to use scriptlets in the JSP whenever I need to utilize "standard" Java commands.</p>
  
  <p>Also, if the described and coded way to check for existence of these session attributes and their conditional
  assigning is not correct, please advise how I would better achieve this.</p>
  
  <p>Thank you very much!</p>