[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Best way to organize common functions?

Posted on 2007-10-06
17
Medium Priority
?
215 Views
Last Modified: 2013-11-23
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?
0
Comment
Question by:jmarkfoley
  • 7
  • 6
  • 3
  • +1
17 Comments
 
LVL 92

Expert Comment

by:objects
ID: 20028731
yes you can put classes in WEB-INF/classes.
Make sure they are in a directory structure that matches their package name.
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 20028935
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?
0
 
LVL 92

Expert Comment

by:objects
ID: 20028944
myClass myc = new myClass();
myc.testif("this string");
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:jmarkfoley
ID: 20029021
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?
0
 
LVL 92

Expert Comment

by:objects
ID: 20029153
you need to put it in a package.
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 20029230
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);
    }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 20029240
> 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
0
 
LVL 5

Expert Comment

by:rajaamirapu
ID: 20029896
Can you post your web.xml. I can help you
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 20030116
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>

0
 
LVL 28

Expert Comment

by:rrz
ID: 20031821
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();  
0
 
LVL 28

Expert Comment

by:rrz
ID: 20031828
>How does it know to look in WEB-INF/classes?  
http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 20038026
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
0
 
LVL 92

Expert Comment

by:objects
ID: 20038045
where is the class file?
0
 
LVL 92

Accepted Solution

by:
objects earned 1500 total points
ID: 20038067
should be:

WEB-INF/classes/common/myClass.class
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 20038654
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!
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 20038662
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).
0
 
LVL 28

Expert Comment

by:rrz
ID: 20045612
If you want people to help you, then you should spread the points around.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month17 days, 15 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question