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?
ASKER
OK, help me out here a bit (beginner, you know). Let say I created the following in .../WEB-INF/Classes/myClas s.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?
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");
myc.testif("this string");
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?
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.
ASKER
Same problem. How does it know to look in WEB-INF/classes?
org.apache.jasper.JasperEx ception: 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.ja va
package common;
class myClass {
public void testif(String aString) {
System.out.println(aString );
}
}
org.apache.jasper.JasperEx
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.ja
package common;
class myClass {
public void testif(String aString) {
System.out.println(aString
}
}
> class in WEB-INF/classes/myClass.ja va
directory needs to match package, and you need the class not the source
class in WEB-INF/classes/common/myC lass.class
directory needs to match package, and you need the class not the source
class in WEB-INF/classes/common/myC
Can you post your web.xml. I can help you
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-entr y-name>
<env-entry-value>abc</env- entry-valu e>
<env-entry-type>java.lang. String</en v-entry-ty pe>
</env-entry>
<display-name>Hello, World Application</display-name>
<display-name>OHPRS</displ ay-name>
<description>
Member web access.
</description>
</web-app>
<?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-value>abc</env-
<env-entry-type>java.lang.
</env-entry>
<display-name>Hello, World Application</display-name>
<display-name>OHPRS</displ
<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();
--------------------------
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
http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html
ASKER
We're still gettin' nowhere fast here. I changed myClass to public. Error:
org.apache.jasper.JasperEx ception: 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/tomca t/lib/cata lina.jar:/ usr/local/ tomcat/bin /tomcat-ju li.jar:/us r/local/to mcat/commo n/lib/sqlj dbc.jar
org.apache.jasper.JasperEx
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/tomca
where is the class file?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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!
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!
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.
Make sure they are in a directory structure that matches their package name.