title: How to save the value of a variable in java
body: <p>I have a requirement that i need to save the value of a variable. My problem is that i need to send a value from an webpage to servlet where the value of the variable is null first time but when i slect a value from the select box it gooes to the servlet and it is going with the value but my problem is here i need to refesrh the page after selecting the value.  so now when i am doing that the value becomes again zero and the operation is not happening can i save the value of the variable after selecting some values from the selection ??</p>
  
  <p>here is my code..</p>
  
  <pre><code>   &lt;body&gt;
      Select Country:
      &lt;select id="country"&gt;
          &lt;option&gt;Select Country&lt;/option&gt;
          &lt;option value="1"&gt;1&lt;/option&gt;
          &lt;option value="2"&gt;2&lt;/option&gt;
          &lt;option value="3"&gt;3&lt;/option&gt;
  
      &lt;/select&gt;
  
      &lt;input type="button" value="Reload page" onclick="reloadPage()"&gt;
  &lt;/body&gt;
  
  
  &lt;script&gt;
      function reloadPage(){
  
          location.reload();
      }
  &lt;/script&gt;  
  
  
  
   &lt;script&gt;
          $(document).ready(function() {
              $('#country').change(function(event) {  
                  var $country=$("select#country").val();
                  $.get('JsonServlet',{countryname:$country},function(responseJson) {   
                      var $select = $('#states');                           
                      $select.find('option').remove();                          
                      $.each(responseJson, function(key, value) {               
                          $('&lt;option&gt;').val(key).text(value).appendTo($select);      
                      });
                  });
              });
          });          
      &lt;/script&gt;
  </code></pre>
  
  <p>and this is my servlet</p>
  
  <pre><code>public class JsonServlet extends HttpServlet {
  
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
  
      PrintWriter out = response.getWriter();
      String value = request.getParameter("countryname");
      System.out.println("comes from ajax" + value);
      JsonGenerator generator = new JsonGenerator();
      Entry entry = null;
      if (value != null) {
  
          HttpSession session = request.getSession();
  
  session.setAttribute("value", value);
          entry = generator.aMethod2Json(value);
          Gson g = new Gson();
  
          response.setContentType("application/json");
          response.setCharacterEncoding("UTF-8");
          response.getWriter().write(g.toJson(entry));
  
  
      } else {
          entry = generator.aMethod2Json("1");
          Gson g = new Gson();
  
          response.setContentType("application/json");
          response.setCharacterEncoding("UTF-8");
          response.getWriter().write(g.toJson(entry));
  
  
      }
  
  
  
  }
  
  // &lt;editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."&gt;
  /**
   * Handles the HTTP
   * &lt;code&gt;GET&lt;/code&gt; method.
   *
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      processRequest(request, response);
  }
  
  /**
   * Handles the HTTP
   * &lt;code&gt;POST&lt;/code&gt; method.
   *
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      processRequest(request, response);
  }
  
  /**
   * Returns a short description of the servlet.
   *
   * @return a String containing servlet description
   */
  @Override
  public String getServletInfo() {
      return "Short description";
  }// &lt;/editor-fold&gt;
  </code></pre>
  
  <p>}</p>