title: How to write a variable from jsp to html div after downloading file
body: <p>I am trying to build a very simple application. There will be a password field. If an user enters wrong password, then a message will be shown "Password incorrect.". If the password is correct, then I have to do two tasks: </p>
  
  <ol>
  <li><p>A file will be downloaded (suppose a.txt). The file resides in WEB-INF folder.</p></li>
  <li><p>Show message "password is correct"</p></li>
  </ol>
  
  <p>I tried to show the message in the following way :</p>
  
  <pre><code>&lt;div style=""&gt;
    &lt;%=passwordMatchText%&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>When an user enters correct password, the file is downloaded but the text inside the div does not changes. It seems jsp does not write the value of the passwordMatchText inside the div. I have printed the text with system.out.print(); the text is printed correctly in console, but it is not written in the div correctly. </p>
  
  <p>I have also tried to put the value in a session variable and write in the div. But none of them worked. How can i sovle the problem</p>
  
  <pre><code>&lt;%@ page trimDirectiveWhitespaces="true" %&gt;
  &lt;%@ page language="java" %&gt;
  &lt;%@ page import="java.util.ArrayList" %&gt;
  &lt;%@ page import="java.io.FileInputStream" %&gt;
  &lt;%@ page import="java.io.FileOutputStream" %&gt;
  &lt;%@ page import="java.io.File" %&gt;
  
  &lt;%
      String header=request.getHeader("user-agent");
      String fileName="a.txt";
  
      String originalPassword="1";
  
      String passwordMatchText="";
      String passwordTextStyle="";
  
      String password = request.getParameter("password");
  
  if(password==null  ||!password.equals(originalPassword)){
      passwordMatchText="Password does not match.";
      passwordTextStyle="color:red;";
  }
      else {
          passwordMatchText="Password matched.";
          passwordTextStyle="color:green;";
  
          String baseFilePath = request.getServletContext().getRealPath("/") + "WEB-INF/" + fileName;
  
          File newFile= new File(baseFilePath);
          if(newFile.exists()){
              ServletOutputStream outputStream = response.getOutputStream();
              FileInputStream inputStream = new FileInputStream(baseFilePath);
              int fileLength = inputStream.available();
  
              response.setContentType("application/octet-stream");
              response.setContentLength(fileLength);
              response.setHeader("Content-Disposition","attachment;filename=" + fileName);
  
              byte[] outputByte = new byte[4096];
              // copy binary content to output stream
              while (inputStream.read(outputByte, 0, 4096) != -1) {
                  outputStream.write(outputByte, 0, 4096);
                  //out.write(outputByte.toString());
              }
  
              inputStream.close();
              outputStream.flush();
              outputStream.close();
  
          }
          else {
              response.getWriter().println("File does not exists.");
          }
  
      }
  %&gt;&lt;!DOCTYPE HTML&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;html:base /&gt;
          &lt;meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1"&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
          &lt;meta name="GENERATOR" content="Microsoft FrontPage 5.0"&gt;
          &lt;meta name="ProgId" content="FrontPage.Editor.Document"&gt;
          &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
  
      &lt;/head&gt;
      &lt;body&gt;
          &lt;!-- header menu closed  --&gt;
          &lt;div &gt; &lt;!-- container for body --&gt;
              &lt;div  style="border:1px solid black;margin-top:10px;padding:10px;"&gt; &lt;!-- div for content --&gt;
                  &lt;form  action="test2.jsp" method="POST"&gt;
                  &lt;div&gt;
  
                          &lt;div style=""&gt;&lt;%=passwordMatchText%&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;  
  
                      &lt;div &gt;
                          &lt;div sytle ="width:100px"&gt;
                              Enter Password :                
                          &lt;/div&gt;
                          &lt;div &gt;
                              &lt;input type="password" name="password" size="20" style="width: 100%;" /&gt;                    
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div &gt;
                          &lt;div &gt;
                              &lt;input class="cmd" type="submit" value="Submit" name="B2"&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;                                  
                  &lt;/form&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;!-- body closed  --&gt;
  
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>