How to save variable value in java

I have a requirement that I need to save the value of a variable. My problem is that I need to send the value from the web page to the servlet, where the variable value is null for the first time, but when I select the value from in the select box it gets to the servlet and it comes with the value, but my problem here. I need to reset the page after selecting a value. so now, when I do this, the value becomes zero again, and the operation fails, I can 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> 

}

+7
java servlets
source share
3 answers

Hope the sample code can help you:

Simple counter

To demonstrate the life cycle of a servlet, we start with a simple example. Example 3-1 shows a servlet that counts and displays the number of calls to it. For simplicity, it displays plain text.

Example 3-1. Simple counter

 import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class SimpleCounter extends HttpServlet { int count = 0; public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/plain"); PrintWriter out = res.getWriter(); count++; out.println("Since loading, this servlet has been accessed " + count + " times."); } } 

Otherwise, if you want something more advanced, a good option is to use a holistic counter:

Example 3-2 Holistic counter

 import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class HolisticCounter extends HttpServlet { static int classCount = 0; // shared by all instances int count = 0; // separate for each servlet static Hashtable instances = new Hashtable(); // also shared public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/plain"); PrintWriter out = res.getWriter(); count++; out.println("Since loading, this servlet instance has been accessed " + count + " times."); // Keep track of the instance count by putting a reference to this // instance in a Hashtable. Duplicate entries are ignored. // The size() method returns the number of unique instances stored. instances.put(this, this); out.println("There are currently " + instances.size() + " instances."); classCount++; out.println("Across all instances, this servlet class has been " + "accessed " + classCount + " times."); } } 

This HolisticCounter tracks its own access account using the instance variable count, the total counter with the class variable classCount, and the number of instances with hashtable instances (another shared resource, which should be a class variable).

Ref. Java Servlet Programming by Jason

+6
source share

How to write your variable to a servlet session object. Thus, it remains active also during changes in the request area.

In addition, during each request, you can check the value of the variable and act accordingly.

+1
source share

You can make an ajax call to the servlet after selecting a value and before refreshing the page.

Or

Set the value in the session by calling the java script function.

 <script> function setMyVar(){ <% session.setAttribute( "username", Value ); %> } </script> 

To get the servlet side session attribute

  session.getAttribute("userName") 
0
source share

All Articles