title: Good practices to follow while mixing Javascript and JSP?
body: <p>In an ideal world I would like to separate out Javascript to a completely different file and include it in the JSP page. But there are cases were I struggle to follow this rule simply because dynamically generated Javascript is so much easier to write !!</p>
  
  <p>Couple of examples :</p>
  
  <p>1) Locale specific error messages in alert boxes.</p>
  
  <pre><code>&lt;%
  Locale locale = ..//get current locale
  %&gt;
  
  &lt;script language="JavaScript"&gt;
  function checkMessage() {
    if(document.form.msg.value=='') {
      alert(&lt;%= *LocaleHelper.getMessage(locale,"please_provide_message")* %&gt;); //get the locale specific message . mixing Javascript and JSP !!!
    }
  }
  </code></pre>
  
  <p>2) Initializing values .Sometimes you need to get values using JSP which will be used inside a javascript method</p>
  
  <pre><code>function computeExpiry () {
   var creationDate= &lt;%= creationDate =%&gt;
   var currentDate = document.form.date.value;
   var jsCreationDate= converToDate(creationDate);
   return currentDate&gt;creationDate ;
  }
  </code></pre>
  
  <p>3) Initializing config objects dynamically</p>
  
  <pre><code>var myConfig = {
       modal:true,
       resize:true,
     &lt;% if (lastPage) { %&gt;
       showPreviousButton  :true,  
       showNextButton : false ,
       showSubmitButton : true,
     &lt;%}  else  {%&gt;
     showPreviousButton  :true,  
       showNextButton : true ,
       showSubmitButton : false,
     &lt;%} %&gt;  
  </code></pre>
  
  <p>As you can imagine, without any kind of conventions , all our JSPs will be a unsightly mix of Javascript and JSP, hard to understand and maintain, with lots of non-reusable javascript code</p>
  
  <p>I am not looking for a perfect solution. I know its easier to do this than try to maintain a pure Javascript and JSP separation. I am looking for suggestions to ease this process and hope lots of people have experience worth sharing.</p>