Passing values from servlet to jsp

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.

This is the code that i wrote in "InstantSMS.jsp"

  $("#search").click(function() {

        var keyword = $("#customerName").val();

            type : "GET",
            url : "SearchingClass",
            data : {
                key : keyword

        }).success(function() {

            $.getJSON('SearchingClass', function(data) {



        .error(function(request, error) {

            $().toastmessage('showWarningToast', "Error! ");


Here the 'SearchingClass' is the name of the servlet. this is the code of ''

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

    String keyword = request.getParameter("key");
    SmsMessagesService search = new SmsMessagesService();
    ArrayList<Customers> result = new ArrayList<Customers>();
    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<String, Object> data = new HashMap<String, Object>();
        data.put("messageRes", table);

    // Write response data as JSON.
    response.getWriter().write(new Gson().toJson(data));    

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {



Now the problem is after i press the search button the searching process or the doGet method run 2 times.

In 1st run output "For testing Keyword is ::: john " 2nd run "For testing Keyword is ::: "

I think that the 2nd run is happening because of "$.getJSON()" method. How to avoid this ?

How can i use $.getJSON() method only to get the "String table" but not to run the "doGet()" method ?

Is there another way to get a string from servlet to jsp using ajax ?

Can i pass this keyword using $.getJSON() method ?

Thank you !


You can use shorthand AJAX call.

var keyword = $("#customerName").val();

$.get("SearchingClass", {key:$keyword}, function(data) {


I'm not 100% following the code, but I'm imagining that you only want to query the servlet once, with the keyword you're passing it. The .ajax() method is therefore already doing the same job as .getJSON() which is just an alias for .ajax() anyhow.

Shot in the dark, but my guess is that you want this:

// ...
}).success(function(data) {
// ...

Need Your Help

Bash Script works but not in when executed from crontab

linux bash shell cron crontab

I am new to linux and the script below is just an example of my issue:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.