JQuery Ajax Loading Panel in JSP

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JQuery Ajax Loading Panel</title>
<script type="text/javascript" src="jquery-1.4.1.js"></script>
<script type="text/javascript" src="jquery.blockUI.v2.34.js"></script>
<link type="text/css" rel="stylesheet" href="new.css" />
<link type="text/css" rel="stylesheet" href="js.css" />
<script type="text/javascript">
/*
Method 1 [Please wait by default panel will be displayed]
*/
$(document).ajaxStart($.blockUI).ajaxStop($.unblockUI);
    function ajaxTry(){       $.post(/CommonServlet",function(j){
            $('#tt').html(j.toString());
        });
    }
/*
Method 2[Customise image will come]
*/
$(document).ajaxStop($.unblockUI);
function ajaxTry(){       $.post(/CommonServlet",function(j){
            $('#tt').html(j.toString());
        });
    }
function ajaxLoad(){
        $.blockUI({ message: '<img src="109.gif" />' });
         ajaxTry();
 }   
</script>                  
</head>
    <body> 
         <table>
            <tr>
                <td>Email Id</td>
                <td>
                    <input type="text" id="txtMail" onblur="ajaxTry()">
                    <span id="tt"></span>
                </td>
            </tr>

        </table>
    </body>
</html>

Servlet for Ajax Call

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Administrator
 */
@WebServlet(name = "CommonServlet", urlPatterns = {"/CommonServlet"})
public class CommonServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {            
            Thread.sleep(1000);
            out.print("OK");
        } catch (Exception ex) {
            System.out.println("Exception : " + ex);
        } finally {
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
Advertisements
This entry was posted in AJAX, JQuery, JSP. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s