PDA

View Full Version : ajax is not working


ganny
08-11-2009, 07:25 AM
Hi,

I have created a webpage which is calling the ajax function for sending the values to servlet. Actually the servlet get the values from the JSP page and stores in the database. All went in right direction but, my servlet (ajax URL) is not working.. Since submitted data are not saving in the database. I got only the message "Thank you for contacting us" after submitting and details submitted are not saved in the database.

Pls find the below ajax.js file and advise me whether i am passing values in the correct way and suggest me if i did anything wrong in it.



function getXmlHttpRequestObject() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if(window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
} else {
alert("Your Browser doesnt support Ajax!\nIt's about time to upgrade don't you think?");
}
}

var searchReq = getXmlHttpRequestObject();

function contactsinit() {

if (searchReq.readyState == 4 || searchReq.readyState == 0) {

var N = trim(document.getElementById("txtName").value);
var CO = trim(document.getElementById("txtCompany").value);
var T = trim(document.getElementById("txtTelephone").value);
var E = trim(document.getElementById("txtEmail").value);
var CE = trim(document.getElementById("txtCountry").value);
var DE = trim(document.getElementById("txtDept").value);
var CT = trim(document.getElementById("txtCargoType").value);
var Q = trim(document.getElementById("txtQueries").value);
var D = trim(document.getElementById("txtCurrDate").value);

searchReq.open("GET", 'webQueryServlet?txtName=' + N + '&txtCompany=' + CO + '&txtTelephone=' + T +'&txtEmail=' + E + '&txtCountry=' + CE + '&txtDept=' +DE + '&txtCargoType=' + CT + '&txtQueries=' + Q + '&txtCurrDate=' +D, true);
searchReq.onreadystatechange = nameSearch;
searchReq.send(null);
}
}

=====

function nameSearch() {
if (searchReq.readyState == 4) {
var response = searchReq.responseText;
var ret = "";
ret = ret + "<br><br><br><table border='0' align='center' cellpadding='0' cellspacing='0'>";

ret = ret + "<tr><td></td></tr><tr><td class = 'contenttext'>Thank you for contacting us. </td></tr> </table><br><br>";
document.getElementById("contactsform").innerHTML = ret;

}
}

ckeyrouz
08-11-2009, 03:13 PM
Where are you calling the method contactsinit?

ganny
08-13-2009, 08:10 AM
Thanks for replying me..

as i already explained i am calling the Ajax function (contactsinit()) from a jsp page. The jsp is working perfectly and send the values to contactsinit(). But, only in ajax i am facing problem that servlet is not working (details are not saving into database).

For you reference i am giving JSP source code where am calling contactsinit() in jsp.

Kindly assist me some 1 since i have stuck at this issue.


<input type="button" name="Button2" class="btnface" value="Send" onClick="validate()" >


function validate() {

var y = document.contacts.txtDept.value;

if((document.contacts.txtName.value == "") || (document.contacts.txtCountry.value == "") || (document.contacts.txtDept.value == "") || (document.contacts.txtCompany.value == "")) {
alert("Some of the Fields are empty");
return false;
}

if((document.contacts.txtTelephone.value == "") && (document.contacts.txtEmail.value.length == "")) {
alert("please enter the either Telephone Number or Email");
return false;
}

if(document.contacts.txtEmail.value.length == "") {
}
else
{
if (! isValidEmail(document.contacts.txtEmail.value)) {
alert("Please enter a valid email address");
return false;
}
}


if (y == "Container Status") {
if (document.contacts.txtnumbers.value == "")
{
alert("Please provide Booking number");
return false;
}
}


if (y == "Rate Request") {
if ((document.contacts.loadcountri.value == "") && (document.contacts.dischcountri.value == "") && (document.contacts.loadport.value == "") && (document.contacts.dischport.value == "") && (document.contacts.eqptype.value == "") && (document.contacts.txtCommodity.value == "") )
{
alert("Please fill all the fields for Rate request");
return false;
}
}


if (document.contacts.txtQueries.value == "") {
alert("Please enter your comment/query in the Comment/Query field.");
return false;
}

contactsinit();
return true;
}

ganny
08-13-2009, 10:27 AM
i will also give you the servlet code here, please check it and advise if anything problem with my servlet coding. Please urgently help anyone here...


package web.webservlet;

/**
*
* @author Administrator
*/

import datasource.dataSourcecon;
/**
*
*
*/
import java.io.*;
import java.sql.*;

import javax.servlet.http.*;
import javax.servlet.*;
import javax.servlet.ServletContext;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.RequestDispatcher;

import java.io.IOException;
import java.sql.Connection;
import javax.servlet.ServletException;
import web.webdb.webQueryDB;


public class webQueryServlet extends HttpServlet {

Connection reqCon;


public void init(ServletConfig config)throws ServletException
{


super.init(config);
System.out.println("Init method in RequestDamageServlet");

}
public void processRequest(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{



try
{

String ContactorName = request.getParameter("txtName");
String ContactorCompany = request.getParameter("txtCompany");
String ContactorTelephone = request.getParameter("txtTelephone");
String ContactorEmail = request.getParameter("txtEmail");
String ContactorCountry = request.getParameter("txtCountry");
String ContactorDept = request.getParameter("txtDept");
String ContactorCargoType = request.getParameter("txtCargoType");
String ContactorQuery = request.getParameter("txtQueries");
String ContactDate = request.getParameter("txtCurrDate");


System.out.println("ContactorName : "+ContactorName);
System.out.println("ContactorCompany : "+ContactorCompany);
System.out.println("ContactorTelephone : "+ContactorTelephone);
System.out.println("ContactorEmail : "+ContactorEmail);
System.out.println("ContactorCountry : "+ContactorCountry);
System.out.println("ContactorDept : "+ContactorDept);
System.out.println("ContactorCargoType : "+ContactorCargoType);
System.out.println("ContactorQuery : "+ContactorQuery);
System.out.println("ContactDate : "+ContactDate);




//assign to vo
web.webvo.webQueryVo queryvo = new web.webvo.webQueryVo();

queryvo.Name = ContactorName;
queryvo.CompanyName = ContactorCompany;
queryvo.Telephone = ContactorTelephone;
queryvo.email = ContactorEmail;
queryvo.country = ContactorCountry;
queryvo.QueryType = ContactorDept;
queryvo.CargoType = ContactorCargoType;
queryvo.Query = ContactorQuery;
queryvo.CurDate = ContactDate;


System.out.println("below getParameterNames at the Data r") ;







System.out.println("DB--1");
webQueryDB reqdb = new webQueryDB();
System.out.println("DB--2");
System.out.println("test");
boolean ret = reqdb.setRequest(queryvo);
System.out.println("DB--3");
}
catch(Exception e)
{
System.out.println("Exception at getParameterValues"+e);
}


}

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
private String checknull(String value)
{
String retvalue;
if(value!=null)
{

retvalue=value.toUpperCase().trim();
return retvalue;
}
else
{

return retvalue="";
}
}
}

ckeyrouz
08-13-2009, 03:41 PM
I think the problem is in the servlet itself, the ajax call is being sent but the servlet does not send back anything.

So the response of the ajax call will be null.

You are asking the responseText in this line:
var response = searchReq.responseText;


but in fact the servlet sends nothing back,

if you need to send anything back with the servlet you should add the following lines somewhere at the end of your java method:

PrintWriter out = response.getWriter();
out.print(someString);


This variable someString will be caught in the responseText.

orca8767
08-13-2009, 03:53 PM
When using AJAX, older versions of IE do not support the XMLHttpRequest(); function. Try using this:

function ajax() {
var ua = navigator.userAgent.toLowerCase();
if (!window.ActiveXObject)
return new XMLHttpRequest();
else if (ua.indexOf('msie 5') == -1)
return new ActiveXObject("Msxml2.XMLHTTP");
else
return new ActiveXObject("Microsoft.XMLHTTP");
}

ckeyrouz
08-13-2009, 03:55 PM
That's what he has done. Check again the js function he has written.

orca8767
08-14-2009, 01:16 AM
Ah, I see that now. Hmm, I don't see much of a reason why this would occur. Pretty sure that your server-side programming is fine... -scratches head-

ckeyrouz
08-14-2009, 03:57 AM
In order for the servlet to push data from server to client it should use the PrintWriter object which can be instantiated from the response object.

I do not see where he has done it.

In general in a servlet you construct your xml string then when done you push it to the client this way:


PrintWriter out = response.getWriter();
out.print(zmlString);
out.flush(); //optional depends on your need
out.close()//optional depends on your need