Connecting Tech Pros Worldwide Forums | Help | Site Map

how to request to servlet from Ajax

Newbie
 
Join Date: Jul 2007
Location: India
Posts: 8
#1: Jul 6 '07
How to request to servlet from Ajax and also I got the DOM object in the servlet through Business Logic. Now how will i pass the DOM object from serlvet to Clientside. Where in the client Side i am using Ajax.


kindly advice me.

ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,440
#2: Jul 6 '07

re: how to request to servlet from Ajax


Its Better to ask this question with the Coding that you made so far.
Because the coding might explain the situation rather than 1000 words that you've posted with your question. Thanks!
Newbie
 
Join Date: Jul 2007
Location: India
Posts: 8
#3: Jul 7 '07

re: how to request to servlet from Ajax


Post: how to request to servlet from Ajax
--------------------------------------------------------------------------------

How to request to servlet from Ajax and also I got the DOM object in the servlet through Business Logic. Now how will i pass the DOM object from serlvet to Clientside. Where in the client Side i am using Ajax.
Kindly advice me.

Re: how to request to servlet from Ajax
--------------------------------------------------------------------------------

Its Better to ask this question with the Coding that you made so far.
Because the coding might explain the situation rather than 1000 words that you've posted with your question. Thanks!

************************************************** ************************************************** ************************************************** **********************************


Here is the Buss Logic Code from which i am getting DOM Object



package Mypackage;


import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.print.Doc;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import java.util.*;

public class RstoXml {

ArrayList al_conc = new ArrayList();
ResultSet rs = null;
public RstoXml() {
}
public static Document toDocument(ResultSet rs)throws ParserConfigurationException, SQLException{

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();


Element root = doc.createElement("Concept");
doc.appendChild(root);

ResultSetMetaData rsmd = rs.getMetaData();
int col_count = rsmd.getColumnCount();

for(int i=0;i<col_count;i++){
Element Selement = doc.createElement("concept"+i);
Object obj = rs.getObject(1);
Selement.appendChild(doc.createTextNode(obj.toStri ng()));
root.appendChild(Selement);
}
return doc;
}

}


Here is the another Buss Logic from here i am passing the DOM object to servlet

import Mypackage.*;
import java.util.*;
import java.sql.*;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class QueryPopulate {

Connection con=null;
Statement stat=null;
ResultSet rs =null;
Document doc = null;
dbaseconnection dbase = new dbaseconnection();
ArrayList conc_list = new ArrayList();
RstoXml xl;
/** Creates a new instance of QueryPopulate */
public QueryPopulate() {

}
public Document PopulateConcept(){
String qry = "select distinct ConceptName from Concept";
con = dbase.getconobj();
try {
stat = con.createStatement();
rs = stat.executeQuery(qry);
while(rs.next()) {
conc_list.add(rs.getString(1));
}
doc= RstoXml.toDocument(rs);
} catch (SQLException ex) {
ex.printStackTrace();
}catch (ParserConfigurationException pce) {
pce.printStackTrace();
}
return doc;

}
}


Here is the servelet code from here I need to pass the DOM object to client side.

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
System.out.println("HaiHello");

request.getParameterValues("slt_concept");
doc = qp.PopulateConcept();
Newbie
 
Join Date: Jul 2007
Location: India
Posts: 8
#4: Jul 7 '07

re: how to request to servlet from Ajax


Re: how to request to servlet from Ajax

Post: how to request to servlet from Ajax
--------------------------------------------------------------------------------

How to request to servlet from Ajax and also I got the DOM object in the servlet through Business Logic. Now how will i pass the DOM object from serlvet to Clientside. Where in the client Side i am using Ajax.
Kindly advice me.

Re: how to request to servlet from Ajax
--------------------------------------------------------------------------------

Its Better to ask this question with the Coding that you made so far.
Because the coding might explain the situation rather than 1000 words that you've posted with your question. Thanks!

************************************************** ************************************************** ************************************************** **********************************


Here is the Buss Logic Code from which i am getting DOM Object



package Mypackage;


import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.print.Doc;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import java.util.*;

public class RstoXml {

ArrayList al_conc = new ArrayList();
ResultSet rs = null;
public RstoXml() {
}
public static Document toDocument(ResultSet rs)throws ParserConfigurationException, SQLException{

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();


Element root = doc.createElement("Concept");
doc.appendChild(root);

ResultSetMetaData rsmd = rs.getMetaData();
int col_count = rsmd.getColumnCount();

for(int i=0;i<col_count;i++){
Element Selement = doc.createElement("concept"+i);
Object obj = rs.getObject(1);
Selement.appendChild(doc.createTextNode(obj.toStri ng()));
root.appendChild(Selement);
}
return doc;
}

}


Here is the another Buss Logic from here i am passing the DOM object to servlet

import Mypackage.*;
import java.util.*;
import java.sql.*;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class QueryPopulate {

Connection con=null;
Statement stat=null;
ResultSet rs =null;
Document doc = null;
dbaseconnection dbase = new dbaseconnection();
ArrayList conc_list = new ArrayList();
RstoXml xl;
/** Creates a new instance of QueryPopulate */
public QueryPopulate() {

}
public Document PopulateConcept(){
String qry = "select distinct ConceptName from Concept";
con = dbase.getconobj();
try {
stat = con.createStatement();
rs = stat.executeQuery(qry);
while(rs.next()) {
conc_list.add(rs.getString(1));
}
doc= RstoXml.toDocument(rs);
} catch (SQLException ex) {
ex.printStackTrace();
}catch (ParserConfigurationException pce) {
pce.printStackTrace();
}
return doc;

}
}


Here is the servelet code from here I need to pass the DOM object to client side.

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
System.out.println("HaiHello");

request.getParameterValues("slt_concept");
doc = qp.PopulateConcept();

------------------------------*********************-------------------------------------------------------


Here is the Ajax part code:

<script>
function body_onload(){
createHttpRequest();

}
var request;
var url="/BudgetAmount/Mypackage/controllerservlet";
var bool=true;
var reqtype="Get";
var ref_val;

function createHttpRequest(){
if(window.XmlHttpRequest){
request = new XMLHttpRequest();
initRequest(reqtype,url,bool);
return false;
}
else if(window.ActiveXObject){
request = new ActiveXObject("Msxml2.XMLHTTP");

if(!request)
request = new ActiveXObject("Microsoft.XMLHTTP");

if(request){
initRequest(reqtype,url,bool);
}else
alert("error on Page");

}
return false;
}
function initRequest(reqtype,url,bool){
request.onreadystatechange = handleResponse;
request.open(reqtype,url,bool);
request.send(null);
return false;
}
function handleResponse(){
if(request.readystate==4 && request.status==200){
parseMessage(request.responseXML);
}
return false;
}
function parseMessage(doc){
var elements = doc.documentElement;
if(elements.hasChildNodes() && elements!=null){
var concept_names = elements.childNodes;
for(var i=0;i<concept_names.length;i++){
document.all("slt_concept").options[i+1].text=concept_names[i].nodevalue;
}
}
else

return false;
}
</script>
Familiar Sight
 
Join Date: May 2007
Location: Jaipur , India
Posts: 202
#5: Jul 7 '07

re: how to request to servlet from Ajax


Quote:

Originally Posted by ajaxrand

Its Better to ask this question with the Coding that you made so far.
Because the coding might explain the situation rather than 1000 words that you've posted with your question. Thanks!


Hi

I have ssen your coding. You have not applied Ajax till now. Steps that can help you with java/jsp use.

1. Ajax works with browser. So make a proper file prefers jsp. So that you can
Interact with browser.

2. Through Ajax you should call jsp page, This jsp page can interact with the java classes and servlets.

3. Direct applying ajax to the servlets , I think not possible.
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,400
#6: Jul 7 '07

re: how to request to servlet from Ajax


Sorry, guys. I moved this here without check the OP's history first.
My bad.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#7: Jul 7 '07

re: how to request to servlet from Ajax


Merged threads.
Reply