Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling Java classes from JSP page

 
Brian Mulvany
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Brian Mulvany
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anthony Watson
Many thanks for your help
Im sure that will help
Its much appreciated
Brian Mulvany
 
Mike Schultz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Place your classes in java packages other than the default.
 
Mike Schultz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class file for a class named parser.JSPSearchBuddy should be at:

WEB-INF/classes/parser/JSPSearchBuddy.class
 
Brian Mulvany
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that you are putting them in the WEB-INF for the right web app?
 
Brian Mulvany
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic