CS6320:  SW Engineering of Web Based Systems

 

JSP (or servlet) calling a utility class after importing it

1) must have all utility classes in a package (e.g. packagename)

2) package should be a directory called packagename that is inside of /WEB-INF/classes directory (this is the webapp that the jsp is deployed in). Mode should be 705. All of the filemodes of the classes in the package directory(s) should be 705.

ALTERNATIVELY: can place in a jar file in the WEB-INF/lib directory

  • make directory structure of package. Suppose package is mypackage.options. Then you will need a mypackage directory and a subdirectory in it of options.
  • suppose top directory is mypackage inside of a directory called source_code.
  • compile all of the code with packagenames at the top of each source code file
  • jar up the package by going to the directory that contains the mypackage directory and type:
    jar -cfv jarfilename.jar mypackage
    the result will be a file jarfilename.jar that contains all of the package mypackage classes. The filemode should be 705 of the jar file.

 

 

example,

package = hi (location WEB-INF/classes/hi)

have file HiClass.class in the package directory.

<html>

<body>
Creating HiClass instance and calling makeMessage method on it

<hr>
<br>

<%@ page import="hi.*" %>

<%
       HiClass hi = new HiClass();
       out.println( hi.makeMessage("Lynne");
%>


</body>
</html>

HiClass.java file

package hi;

public class HiClass {

public String makeMessage(String s) {

     String b = "Hello " + s;

     return b; }

}

 
© Lynne Grewe