Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing values from servlet to jsp
    primarykey
    data
    text
    <p>I'm going to implement a searching function for a sms managing system, where the user can enter a name(keyword) and get relevant information. </p> <p>This is the code that i wrote in "InstantSMS.jsp" </p> <hr> <pre><code> $("#search").click(function() { var keyword = $("#customerName").val(); $.ajax({ type : "GET", url : "SearchingClass", data : { key : keyword } }).success(function() { $.getJSON('SearchingClass', function(data) { $("#resultTable").html(data.messageRes); }); }) .error(function(request, error) { $().toastmessage('showWarningToast', "Error! "); }); }); </code></pre> <hr> <p>Here the 'SearchingClass' is the name of the servlet. this is the code of 'SearchingClass.java'</p> <hr> <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String keyword = request.getParameter("key"); SmsMessagesService search = new SmsMessagesService(); ArrayList&lt;Customers&gt; result = new ArrayList&lt;Customers&gt;(); System.out.println("For testing Keyword is ::: "+keyword); result = search.searchCustomer(keyword); String table = "in here i'll generate a code to draw a table using html" Map&lt;String, Object&gt; data = new HashMap&lt;String, Object&gt;(); data.put("messageRes", table); // Write response data as JSON. response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(new Gson().toJson(data)); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } </code></pre> <hr> <p>Now the problem is after i press the search button the searching process or the doGet method run 2 times.</p> <p>In 1st run output "For testing Keyword is ::: john " 2nd run "For testing Keyword is ::: "</p> <p>I think that the 2nd run is happening because of "$.getJSON()" method. How to avoid this ? </p> <p>How can i use $.getJSON() method only to get the "String table" but not to run the "doGet()" method ? </p> <p>Is there another way to get a string from servlet to jsp using ajax ?</p> <p>Can i pass this keyword using $.getJSON() method ? </p> <p>Thank you !</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload