how to call a class in jsp (newbie) ?

Discussion in 'Java' started by f_Parlant, Sep 18, 2003.

  1. f_Parlant

    f_Parlant Guest

    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
    f_Parlant, Sep 18, 2003
    #1
    1. Advertising

  2. f_Parlant

    James Guest

    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!

    "f_Parlant" <> wrote in message news:<bkd103$ovj$>...
    > 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
    James, Sep 19, 2003
    #2
    1. Advertising

  3. f_Parlant

    Phil Hanna Guest

    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.
    --
    Phil Hanna
    Author of JSP 2.0: The Complete Reference
    http://www.philhanna.com
    Phil Hanna, Sep 20, 2003
    #3
  4. f_Parlant

    f_Parlant Guest

    "Phil Hanna" <> a écrit :
    > Well, for one thing, you've got the opening JSP declaration delimiter
    > wrong:


    Thanks, you are my third eye ;-)
    François
    f_Parlant, Sep 20, 2003
    #4
  5. f_Parlant

    f_Parlant Guest

    "James" <> a écrit
    <%
    > 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
    f_Parlant, Sep 20, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,720
    Thomas Weidenfeller
    Oct 12, 2005
  2. Replies:
    1
    Views:
    533
    Andrea Desole
    Dec 14, 2005
  3. Replies:
    0
    Views:
    4,338
  4. Replies:
    2
    Views:
    501
  5. zalek
    Replies:
    1
    Views:
    354
Loading...

Share This Page