How to save the value of a variable in java

Viewed 5828

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 ??

here is my code..

   <body>
    Select Country:
    <select id="country">
        <option>Select Country</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>

    </select>

    <input type="button" value="Reload page" onclick="reloadPage()">
</body>


<script>
    function reloadPage(){

        location.reload();
    }
</script>  



 <script>
        $(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) {               
                        $('<option>').val(key).text(value).appendTo($select);      
                    });
                });
            });
        });          
    </script>

and this is my servlet

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));


    }



}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
 * Handles the HTTP
 * <code>GET</code> 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
 * <code>POST</code> 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";
}// </editor-fold>

}

3 Answers
Related