0

I am new to JQuery. I have a servlet which will fetch data from the database and the result is kept is request and the same result is retrieved in jsp file. Now i have to call the servlet using ajax jquery to load the data. I am doing as below. But it is not loading. Please help me.

$('#myForm #revert').click(function() {

        $.ajax({
            type: "GET",
            url: "./myController",
            success: function(msg) {
                <span style="color:green;font-weight:bold">Successful</span>');
            },
            error: function(ob,errStr) {
                //Todo

            }
        });

        });

Servlet code:

//Service call gets data and the result is kept in request scope as below
request.setAttribute("myresult", result);
request.getRequestDispatcher("/WEB-INF/myScreen.jsp").forward(request, response);

Thanks!

user1016403
  • 12,151
  • 35
  • 108
  • 137

1 Answers1

1

Ajax is not a normal HTTPRequest,You canot Forward or sendRedirect a Ajax request

Since it is Asynchronous,you need to write the response for Ajax request

PrintWriter out = resp.getWriter();
out.println(resultString);
return;

Please read @Balusc great answer :How to use Servlets and Ajax?

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • suresh, Thanx for ur reply. How can the data is forwarded to jsp? do i need do any changes in jquery? Thanks – user1016403 Aug 07 '13 at 10:22
  • @user1016403 A complete example ..from `HTML` to `Response writing` is given there.If you still have a doubt,Please let me know. – Suresh Atta Aug 07 '13 at 10:24
  • it just says how display small text.But here i get list of objects from database which needs to be displayed in jsp table. Please help me... – user1016403 Aug 07 '13 at 10:27
  • @user1016403 send that list as a Json string to client.That is the only way I know. – Suresh Atta Aug 07 '13 at 10:31
  • @user1016403 This is the [best link](http://www.programming-free.com/2012/09/ajax-with-servlets-using-jquery-and-json.html#.UgIjA5IepGQ) I can provide for you ,so far. – Suresh Atta Aug 07 '13 at 10:35