Tuesday, September 7, 2010

AJAX – First Example(Server return data in text format)

AJAX – First Example(Server return data in text format)



<%@page import="java.util.Calendar,

                                    java.util.GregorianCalendar" %>

<%

         Calendar cal = new GregorianCalendar();

        response.setContentType("text/xml");

        response.setHeader("Cache-Control", "no-cache");

        //for plain text:

    response.getWriter().write(cal.get(Calendar.HOUR)+":"+cal.get(Calendar.MINUTE)+":"+

    cal.get(Calendar.SECOND));

%>





<%

String customerID = request.getParameter("customerID");

if(customerID != "") {

      response.setContentType("text/xml");

      response.setHeader("Cache-Control", "no-cache");

      if(customerID.equals("1101820"))  

      {

                response.getWriter().write("<message>Anuj,Kumar</message>");    

        } else{

                response.getWriter().write("<message>Sandeep Chauhan</message>");    

        }

} else {

        //nothing to show

        response.setStatus(HttpServletResponse.SC_NO_CONTENT);

}

%>

Important things to take care

The developer must be aware of two things while writing the server side code:

–        The Content-Type must be set to text/xml

–        The Cache-Control must be set to no-cache.

The XMLHttpRequest object will process only requests that are of the Content-Type of

only text/xml, and setting Cache-Control to no- cache will keep browsers from locally

caching responses for cases in which duplicate requests for the same URL (including URL

parameters) may return different responses.

No comments:

Post a Comment