Tuesday, October 9, 2012

AJAX Program for Firefox, Ajax does not work with Firefox, Ajax not working in firefox but works with IE, Ajax for Mozilla

There are many questions are floating on internet that AJAX code doesnot work for Mozilla FireFox. And interesting there is no such exact solution for this. Some days back I have also posted one article on my blog regarding one simple tutorial on Ajax and it was very fine with IE. One day I got one comment that my tutorial is not working for Firefox. I asked this question to one of my friend and she gave the solution.

Complete Example:

[Please follow my prior posting to setup this tutorial]

1. ShowStudentInfo.jsp (C:\Ajax_workspace\blog_demo\WebContent\ShowStudentInfo.jsp)
2. StudentInfo.java (C:\Ajax_workspace\blog_demo\src\StudentInfo.java) This is a servlet.

ShowStudentInfo.jsp

<html>
<head>
<title>Binod Java Solution AJAX</title>
<script type="text/javascript">
var request; function getName(){
var roll = document.getElementById("roll").value;
var url = "http://localhost:8080/blog_demo/StudentInfo?roll="+roll;

if(window.ActiveXObject){
request = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
request = new XMLHttpRequest();
}

request.onreadystatechange = showResult;
request.open("POST",url,true);
request.send(null);
}
function showResult(){
if(request.readyState == 4){
if ( request.status == 200 ) {
var response = request.responseXML;
var students = response.getElementsByTagName("Student");
var student = students[0];

document.getElementById("NamelH1").innerHTML = student.getElementsByTagName("Name")[0].childNodes[0].data;
document.getElementById("HostelH1").innerHTML = student.getElementsByTagName("Hostel")[0].childNodes[0].data;
document.getElementById("ContactH1").innerHTML = student.getElementsByTagName("Contact")[0].childNodes[0].data;


}
}
}
</script>
</head>
<body>
<h2>GET STUDENT INFO</h2>
<br>
Enter Roll Number
<input type="text" id="roll">
<input type="button" value="Get Name" onclick="getName();" />
<br>
Name :
<span id="NamelH1"></span>
<br>
Hostel :
<span id="HostelH1"></span>
<br>
Contact :
<span id="ContactH1"></span>
<br>
</body>
</html>

StudentInfo.java

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

public class StudentInfo extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;
public StudentInfo() { super(); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String roll = request.getParameter("roll");
PrintWriter out = response.getWriter();
response.setContentType("text/xml");
System.out.println(getResult(roll)); out.println(getResult(roll));
}
public String getResult(String roll){
String name = "";
String hostel = "";
String contact = "";
if(roll.equalsIgnoreCase("110")){
name = "Binod Kumar Suman"; hostel = "Ganga"; contact = "999999999";
} else if(roll.equalsIgnoreCase("120")){
name = "Pramod Kumar Modi"; hostel = "Godawari"; contact = "111111111111";
} else{ name = "Roll Number not found"; }
String result = "<Students>";
result += "<Student>"; result += "<Name>" + name + "</Name>";
result += "<Hostel>" +hostel + "</Hostel>";
result += "<Contact>" +contact + "</Contact>";
result += "</Student>"; result += "</Students>";
return result;
}
}

This code also work well with IE.

0 comments:

Post a Comment