aspose file tools*
The moose likes JSP and the fly likes Calling Java classes from JSP page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Calling Java classes from JSP page" Watch "Calling Java classes from JSP page" New topic
Author

Calling Java classes from JSP page

Brian Mulvany
Greenhorn

Joined: Oct 19, 2004
Posts: 28
Hi
Im wondering how easy it is to call Java classes from JSP pages. Its probably really simple when you know how.
Basically I have Java classes that search a website for deatils and return the results in XML. I want to be able to call these classes from a JSP page.
Any help is much appreciated.
Brian
Anthony Watson
Ranch Hand

Joined: Sep 25, 2003
Posts: 327
To use your classes in a JSP page, you need to import them into the page like so:


You can import many classes by delimiting the import attribute's value with commas.

To actually call a method on your class, you include your code within scriptlet marks like so:



It is generally prefered to do most/all of your Java coding within servlets, (or better yet, classes that servlets delegate work to), and then forward to JSP pages to render the view.
[ December 06, 2004: Message edited by: Anthony Watson ]

Anthony W.<br />MCP, SCJP 1.4, SCJD, SCWCD 1.3, SCWCD 1.4, SCBCD
Brian Mulvany
Greenhorn

Joined: Oct 19, 2004
Posts: 28
Hi again
Im sorry if I seem stupid or annoying at this stage but I have only started this stuff and I dont quite follow the code below.

<%@ page import="fully.qualified.ClassName" %>

I have a java class which is saves as Search.java and that is the class I want to call so Do I type
<%@ page import="Searcj.java" %>
I dont understand fully.qualified.ClassName.
If you could just explain that I would appreciate it a lot.
Thanks
Brian
Anthony Watson
Ranch Hand

Joined: Sep 25, 2003
Posts: 327
A fully qualified class name is a class that is preceded with its package name.

Example Class:

The fully qualified class name for the above class would be com.mycompany.utils.MyClass

You would import this into your JSP page for use with this:


If your class does not have a package specified like the one above, you could just use this:
Brian Mulvany
Greenhorn

Joined: Oct 19, 2004
Posts: 28
Hi Anthony Watson
Many thanks for your help
Im sure that will help
Its much appreciated
Brian Mulvany
Mike Schultz
Greenhorn

Joined: Dec 14, 2004
Posts: 3
I was doing the same thing and the server says:

---------------------------
Generated servlet error:
/apps/tomcat/work/Catalina/localhost/msibayan/org/apache/jsp/me4_jsp.java:6: '.' expected
import me4customClass;
^
1 error
------------------------------

Can someone please help me?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

"mike six",

We're pleased to have you here with us here on the Ranch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Place your classes in java packages other than the default.
Mike Schultz
Greenhorn

Joined: Dec 14, 2004
Posts: 3
ok, I'm still trying to work it out. >< I have someone here helping me, I've already tried to put it in some package. Thanks anyway.
Brian Mulvany
Greenhorn

Joined: Oct 19, 2004
Posts: 28
Hi
Its me again I still havent solved the problem with importing Java classes into a JSP pageusing Tomcat running on my loacal machine.
When I enter in 'U2' for example and hit return it supposed to use U2 as a paramater for the Java class and return what cds it finds from CDWow.com.
When I run the Java classes from Jbuilder they return the details I am looking for.
Below is the code I used to import the java class and also the error message im getting.

<%@ page import="parser.JSPSearchBuddy" %>

org.apache.jasper.JasperException: Unable to compile class for JSPNote: sun.tools.javac.Main has been deprecated.

C:\Documents and Settings\01832077\Desktop\Apache Tomcat 4.0\work\Standalone\localhost\_\CD\searchresults$jsp.java:4: Class parser.JSPSearchBuddy not found in import.
import parser.JSPSearchBuddy;

What folder do my classes go in the Tomcat Webapps folder, maybe that is what Im doing wrong
Thanks
Brian
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

The class file for a class named parser.JSPSearchBuddy should be at:

WEB-INF/classes/parser/JSPSearchBuddy.class
Brian Mulvany
Greenhorn

Joined: Oct 19, 2004
Posts: 28
Thanks for that advice
They werentstored that way so I tried it but Im still getting the same error.

org.apache.jasper.JasperException: Unable to compile class for JSPNote: sun.tools.javac.Main has been deprecated.

C:\Documents and Settings\01832077\Desktop\Apache Tomcat 4.0\work\Standalone\localhost\_\CD\searchresults$jsp.java:4: Class parser.JSPSearchBuddy not found in import.
import parser.JSPSearchBuddy;


Thanks
Brian
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Are you sure that you are putting them in the WEB-INF for the right web app?
Brian Mulvany
Greenhorn

Joined: Oct 19, 2004
Posts: 28
I have tried putting them inside a WEB-INF folder inside the CD folder (which is the folder my jsp pages are in) and also in the WEB-INF folder just in the ROOT folder.
Below are the directories I have used to put the classes in.
C:\Documents and Settings\01832077\Desktop\Apache Tomcat 4.0\webapps\ROOT\CD\WEB-INF\classes\parser
and
C:\Documents and Settings\01832077\Desktop\Apache Tomcat 4.0\webapps\ROOT\WEB-INF\classes\parser
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Maybe an example will help.
Go to http://simple.souther.us
download SimpleBean.war to your webapps folder and Tomcat will deploy it for you.

There is a bean in there that is accessed from a JSP page.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Unless you have done something special in Tomcat's server.xml file, it will not recognize webapps\ROOT\CD as a web app. If you move the CD folder to webapps\CD you will have better results.

Please look through the Tomcat documentaion and familiarize yourself with how it recognizes web applications.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling Java classes from JSP page