Thursday, June 25, 2009

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. Thanks MP.

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.

17 comments:

  1. Superb sir. Yes very exact solution. Now this code is working with IE as well as in Mozilla Firefox. Really good solution. I had struggled a lot to get this solution. Thanks MP also. :)

    Pattie

    ReplyDelete
  2. Superb sir. Yes very exact solution. Now this code is working with IE as well as in Mozilla Firefox. Really good solution. I had struggled a lot to get this solution. Thanks MP also. :)

    Pattie

    ReplyDelete
  3. Hi these are some awesome tutorials you have out here. Between could you post a tutorial on how one can use AJAX to receive data from database in JSP like the rst.getString("value"); I mean how do you send this kind of data back to the HTML page and will the HTML page be able to display these values properly in a tabular format:
    var response = request.responseText;

    where responseText will contain values like the rst.getString() values of database in JSP.

    Thanks a lot

    ReplyDelete
  4. Hi

    This is not working for me and i did the same as u said pls can u help me..

    Regards,
    Deepthi

    ReplyDelete
  5. Hi Deepthi, Can you please tell me what error you are getting?

    Thanks,
    Binod Suman
    http://binodsuman.blogspot.com

    ReplyDelete
  6. Hello Suman,

    Thanks for posting these example codes which is very useful and informative.I am a regular visitor to your blog.

    Regards,
    Prathibha

    ReplyDelete
  7. you haven't write new solution for us. where have you been for a long time. Your all examples are very useful and precious for me.
    Thanks and regard.
    Vikash Singh Rathore.
    Bangalore

    ReplyDelete
  8. Nice Tutorial Sir...

    Only few tutorial about ajax and java in internet.

    But in your blog i can get some useful example and code that can help me for learning ajax with java.

    But i suggest you to make your code in "code tag"

    i keep you for your new posting Sir!!!

    ReplyDelete
  9. I have followed the instruction and i have deployed into jboss.. but it is not working... some javascript error.

    ReplyDelete
  10. Hi suman thanks a lot your script is working fine when we give url like http://localhost/... but the when we will give live site (I mean url = "http://xxx.com/list like this) the response status zero is coming in firefox whts the plm please tell me

    Thanks
    somu
    hyderabad

    ReplyDelete
  11. hi sir
    this is not working for me. i try this code sir, it is run but i ll give the roll no. 110 after that, there is no response. the name, hostel , and contact details are not showed to me.
    by
    kavitha.n

    ReplyDelete
  12. hi
    this is not working. i tried this for mozilla firebox and (get-start-with-ajax-ajax-siimple.html) this also. i did the same, i dont have output,but i saw d output window. in output window i typed roll no. 110 and then click getname. there is no response.
    by
    kavitha.n

    ReplyDelete
  13. hi binod i am using this same code but no response is coming .....
    data is goin to th e servlet but no result coming

    ReplyDelete
  14. hi binod i am using this samw code but nothing is coming as response ...

    but on click the roll is going to the servlet

    ReplyDelete
  15. Thanks for this example.. I hav not changed the URL,And now its working.

    ReplyDelete
  16. Excellent work.
    It's working perfect.
    Thanks for posting this example...

    ReplyDelete
  17. when i am clicking submit button(GetName)it doesnt performing any task please reply

    ReplyDelete

You can put your comments here (Either feedback or your Question related to blog)