how to call a class in jsp (newbie) ?

F

f_Parlant

Hi,

This is the most simple sax parser.
I took it from a java book and transformed it into a jsp page.


But after the class description, how do I tell it to WORK !
I guess I must "call" a function, and pass arguments ... but what

I've tried something like (with no succes):

<%
BandReader montest = new BandReader();
montest.main("webapps/JSPWiki/bands.xml");
%>

**********
*sax2.jsp*
**********
<%@ page import="org.xml.sax.helpers.XMLReaderFactory" %>
<%@ page import="org.xml.sax.XMLReader" %>
<%@ page import="org.xml.sax.SAXException" %>
<%@ page import="org.xml.sax.Attributes" %>
<%@ page import="org.xml.sax.helpers.DefaultHandler" %>

<!%
public class BandReader extends DefaultHandler
{
public static void main(String[] args) throws Exception
{
System.out.println("Here we go ...");
BandReader readerObj = new BandReader();
readerObj.read(args[0]);
}

public void read (String fileName) throws Exception
{
XMLReader readerObj =
XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
readerObj.setContentHandler (this);
readerObj.parse (fileName);
}

public void startDocument() throws SAXException
{
System.out.println("Starting ...");
}

public void endDocument() throws SAXException
{
System.out.println("... Finished");
}

public void startElement(String uri, String localName, String qName,
Attributes atts) throws SAXException
{
System.out.println("Element is " + qName);
}
}
%>

//HOW DO I FINISH ??
// I've tried something like

<%
BandReader montest = new BandReader();
montest.main("webapps/JSPWiki/bands.xml");
%>

***** end code ******

Thanks for your help
François
 
J

James

well firstly I would put the class you're trying to create into a java
file.. compile it and put it in your WEB-INF/classes directory.
defining a class in a jsp is a bad idea even if it is possible.

after that is done (you may need to restart your web server) you
should be able to import your class similarly to how you imported all
of those xml classes.

then the following code snippet should create the class and call the
read method properly.

<%
BandReader readerObj = new BandReader();
readerObj.read("somefile");
%>

good luck!
 
P

Phil Hanna

Well, for one thing, you've got the opening JSP declaration delimiter
wrong:

<!%
public class BandReader extends DefaultHandler
....


should be

<%!
public class BandReader extends DefaultHandler
....


but I don't understand why you're trying to run a standalone
console-mode Javaa application inside a JSP page. Where do you expect
all those System.out.println statements to print? They won't be
displayed in the browser.
 
F

f_Parlant

James said:
BandReader readerObj = new BandReader();
readerObj.read("somefile");
%>


thanks, it works in web-inf/classes ;-),
But the output is in the is in the dos-java window. I guess I need to adapt
a bit more the code to make the output in the browser.

Thank you for your help.
François
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top