title: How to pass a javascript variable value from a JSP page to a Servlet?
body: <p>I have a JSP page named <code>menu.jsp</code>. On this page the user has to choose a choice from a menu. I have implemented <code>onclick("myredirect(choiceValue)")</code> on each menu item using JavaScript. Tthis function uses confirm pop-up box to check whether the user want to continue with his current choice. If user select ok, then it'll return true and it will be stored in <code>check</code> variable. I want to send this as <code>choiceValue</code> to a servlet named <code>CheckLogin.java</code> which checks whether the user is logged in using session tracking. If the user isn't logged in he'll be redirected to <code>login.jsp</code> page, otherwise he'll be redirected to <code>instructions.jsp</code> page. </p>
  
  <p>I have implemented the whole stuff as below. I want to use the <code>choiceValue</code> throughout the session, but it's not working as I expected.</p>
  
  <p><code>menu.jsp</code></p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  &lt;!--
      function myredirect(choiceValue){
      var check=window.confirm("You have Choosen "+choiceValue+" !\n Do you want to continue?");
          if(check){
              window.location.replace("checklogin.do?choice="+choiceValue);
              //window.location.replace("instructions.jsp?choice="+choiceValue);
          }
      }
  //--&gt;
  &lt;/script&gt;
  </code></pre>
  
  <p><code>CheckLogin.java</code></p>
  
  <pre><code>public class CheckLogin extends HttpServlet {
  
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
          //cache controlling
          response.setHeader("Pragma", "no-cache");
          response.setDateHeader("Expires",0);
          response.setHeader("Cache-Control", "no-cache");
  
          HttpSession session=request.getSession(false);
  
          if(session==null)                                   //checking existing session
              response.sendRedirect("login.jsp");
          else if(session.isNew())
              response.sendRedirect("login.jsp");
          else{
              //setting choiceValue to session object so that it can be used further
              String choiceValue=request.getParameter("choice");
              session.setAttribute("choice",choiceValue);
              response.sendRedirect("instructions.jsp");      //redirecting to instruction.jsp page
          }
      }
  </code></pre>