Link to home
Create AccountLog in
Avatar of Mark
Mark

asked on

Best way to organize common functions?

I am fairly new to both JAVA and jsp. I am writing a jsp web app. now. At the moment, I am doing development in an unpacked directory. I have a couple of function that will be used by almost every program. What is the best way to create a module to hold these? I'd rather skip the bother of jar files at the moment. Can I leave them unpacked in my context structure? If so, would I put them in my WEB-INF/classes directory?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

yes you can put classes in WEB-INF/classes.
Make sure they are in a directory structure that matches their package name.
Avatar of Mark
Mark

ASKER

OK, help me out here a bit (beginner, you know). Let say I created the following in .../WEB-INF/Classes/myClass.java:

class myClass {
    public void testif(String aString) {
        System.out.println(aString);
    }
}

I compiled it with javac. I put the following reference into my jsp program, tried to run it and got the following error:

An error occurred at line: 164 in the jsp file: /login.jsp
myClass cannot be resolved
161: </td>
162:
163: <%
164:  myClass.testif("this string");
165: %>

What did I do wrong?
myClass myc = new myClass();
myc.testif("this string");
Avatar of Mark

ASKER

An error occurred at line: 164 in the jsp file: /login.jsp
myClass cannot be resolved to a type
161: </td>
162:
163: <%
164: myClass myc = new myClass();%>

It must not be automatically picking it up from .../WEB-INF/classes. What do you think?
you need to put it in a package.
Avatar of Mark

ASKER

Same problem. How does it know to look in WEB-INF/classes?

org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 164 in the jsp file: /login.jsp
common.myClass cannot be resolved to a type
161: </td>
162:
163: <%
164: common.myClass myc = new common.myClass();%>

class in WEB-INF/classes/myClass.java

package common;

class myClass {
    public void testif(String aString) {
        System.out.println(aString);
    }
}
> class in WEB-INF/classes/myClass.java

directory needs to match package, and you need the class not the source

class in WEB-INF/classes/common/myClass.class
Can you post your web.xml. I can help you
Avatar of Mark

ASKER

Sorry, mis-type. The myClass.class is ALSO in WEB-INF/classes. Here is my web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

<env-entry>
  <env-entry-name>myConstant</env-entry-name>
  <env-entry-value>abc</env-entry-value>
  <env-entry-type>java.lang.String</env-entry-type>
</env-entry>

    <display-name>Hello, World Application</display-name>
     <display-name>OHPRS</display-name>

    <description>
        Member web access.
    </description>

</web-app>

Please compile it  this way. Your class must be public.
-----------------------------------------------
package common;
public class myClass {
    public void testif(String aString) {
        System.out.println(aString);
    }
}
-------------------------------------------------------
In your JSP   you can use  
common.myClass myc = new common.myClass();  
or you can use  
<%@ page import="common.myClass"%>
and
myClass myc = new myClass();  
>How does it know to look in WEB-INF/classes?  
http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html
Avatar of Mark

ASKER

We're still gettin' nowhere fast here. I changed myClass to public. Error:

org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 211 in the jsp file: /pensionLogin.jsp
common.myClass cannot be resolved to a type
208: </td>
209:
210: <%
211: common.myClass myc = new common.myClass();%>
212: <%= myc.testif("this string");
213: %>
214:

here's the code:

package common;

public class myClass {
    public void testif(String aString) {
        System.out.println(aString);
    }
}

CLASSPATH=/usr/local/tomcat/lib/catalina.jar:/usr/local/tomcat/bin/tomcat-juli.jar:/usr/local/tomcat/common/lib/sqljdbc.jar
where is the class file?
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Mark

ASKER

it was in WEB-INF/classes. I moved it to WEB-INF/classes/common and it worked, finally! Also, if I change the class, I have to restart tomcat.

This is obscure! Nothing that I've come across so far in the docs say it has to be in classes/common, including the link given to me by rrz@871311. That link says,

"All unpacked classes and resources in the /WEB-INF/classes directory of your web application archive, ... are made visible to the containing web application, but to no others."

Doesn't mention classes/common. I guess this is one more of those things yer just supposed to KNOW!
Avatar of Mark

ASKER

OH, is it because I named it common? I have to create directory structures under classes named with my class name. OK, I missed that point. I was mentally connecting it with $CATALINA_HOME/common. I guess "common" was a poor choice on my part for a class name (if I wanted to avoid confusion).
If you want people to help you, then you should spread the points around.