Problem with SAXParser when using with servlets.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Vijay_Anand
Posted On:   Tuesday, June 22, 2004 02:45 PM

Hai All, Problem with SAXParser when using with servlets. I am finding problem in retrieving the values from enddocument method of DocumentHandler from my dopost method of servlet. But am able to retrieve the values from other method. i am having a global variable sid which will be assigned some value in enddocument method of DocumentHandler, which i need to use in doPost method. But when i try to do so its giving the value 0, instead of original value. The sample code which i am using is as follows. ----------------------------------------------- import java.io.IOException;    More>>

Hai All,



Problem with SAXParser when using with servlets.

I am finding problem in retrieving the values from
enddocument method of DocumentHandler from my dopost
method of servlet. But am able to retrieve the
values from other method.



i am having a global variable sid which will be assigned


some value in enddocument method of DocumentHandler, which


i need to use in doPost method. But when i try to do so its


giving the value 0, instead of original value.


The sample code which i am using is as follows.

-----------------------------------------------


import java.io.IOException;

import java.io.StringReader;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.xml.sax.*;

import org.xml.sax.helpers.XMLReaderFactory;



public class test extends HttpServlet implements
DefaultHandler

{

long result;

long sid; // Global variable.

String str;



public void doGet(HttpServletRequest req,
HttpServletResponse resp) throws ServletException,
IOException

{

doPost(req,resp);

}



public void doPost(HttpServletRequest req,
HttpServletResponse resp) throws ServletException,
IOException

{

try

{

XMLReader parser = XMLReaderFactory.createXMLReader();

ContentHandler handler = new test();

parser.setContentHandler(handler);

str = " HP Services 800-22- 1984 ";

InputSource is = new InputSource(new StringReader(str));

parser.parse(is);

changethevalue();

System.out.println("The value of result is " + result);

System.out.println("The value of sid from end Document is: " + sid); // This is printing zero, instead
// of 2005.

}

catch (Exception e)

{

System.err.println(e);

}

}



public void changethevalue()

{

result = 25;

}



public void endDocument() throws SAXException

{

System.out.println(" inside the end document");

sid=2005;

}




Can any one let me know how to get the value assigned in the end document method.


Any help would be appreciated.

Thanks in advance

   <<Less

Re: Problem with SAXParser when using with servlets.

Posted By:   Anjani_Kalyan  
Posted On:   Wednesday, June 23, 2004 02:43 AM

The sid value is set only in the handler object and not in the "this" object. So for printing the value of 2005 you need to do this handler.sid and not just sid
The other option you have is in parser.setContentHandler method pass "this" instead of "handler"
About | Sitemap | Contact