index.jsp
AjaxServlet.java<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>AJAX - codesstore.blogspot.com</title> <script type="text/javascript"> var XMLHttpRequestObject = false; if (window.XMLHttpRequest) { XMLHttpRequestObject = new XMLHttpRequest(); } else if (window.ActiveXObject) { XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP"); } function getData(requestlink,parameter,value,responsedivid){ if(XMLHttpRequestObject) { var obj = document.getElementById(responsedivid); var link=requestlink+"?"+parameter+"="+value; XMLHttpRequestObject.open("GET", link); XMLHttpRequestObject.onreadystatechange = function() { if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { obj.innerHTML = XMLHttpRequestObject.responseText; } } XMLHttpRequestObject.send(null); } } function postData(requestlink,parameter,value,responseid){ if(XMLHttpRequestObject) { var obj = document.getElementById(responseid); var link=requestlink; XMLHttpRequestObject.open("POST", link); XMLHttpRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8'); var parameters=parameter+"="+value; XMLHttpRequestObject.onreadystatechange = function() { if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { obj.innerHTML = XMLHttpRequestObject.responseText; } } XMLHttpRequestObject.send(parameters); } } </script> </head> <body> <input type="button" value="get type A Data" onclick="getData('AjaxServlet','type','A','response')"> <input type="button" value="get type B Data" onclick="getData('AjaxServlet','type','B','response')"> <div id="response"></div> <input type="button" value="get type A Data using POST" onclick="postData('AjaxServlet','type','A','post-response')"> <input type="button" value="get type B Data using POST" onclick="postData('AjaxServlet','type','B','post-response')"> <div id="post-response"></div> </body> </html>
package codesstore.ajaxtest; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class AjaxServlet extends HttpServlet { private static final long serialVersionUID = 1L; public AjaxServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); String type = request.getParameter("type"); if (type.equals("A")) { String data = "You selected Type A"; out.print(data); } else if (type.equals("B")) { String data = "You selected Type B"; out.print(data); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); String type = request.getParameter("type"); if (type.equals("A")) { String data = "You selected Type A. POST method used"; out.print(data); } else if (type.equals("B")) { String data = "You selected Type B. POST method used"; out.print(data); } } }
No comments:
Post a Comment