1

I'm kind of a JSP noob (and I'm forced to write old school because of school) and I have this pure java file that I wrote. I want to use that FooBar.java file and the class inside it (packagename.name.FooBar) inside my .jsp files. My questions are:

  • Where should I place my .java files? Some places told me to put them in /src and some told me to put them in /WebContent/WEB-INF/classes. Both of these don't work.
  • How can I import them correctly? I'm trying <%@ page import = "packagename.name.*" %> and it doesn't work in both cases above (when the package is in src or in classes.

EDIT: Now I've tried compiling them and putting them in WEB-INF/classes/packagename/name, but I still get errors:

Only a type can be imported. packagename.name.FooBar resolves to a package

And then of course these (because it didn't import correctly): FooBar cannot be resolved to a type.

What am I doing wrong?

EDIT: Thank you everyone! If you're wondering, here's what solved my problem:

  • As @user7294900 mentioned, you can only import .class files and not .java files. Use the javac command to compile files - here's more information.
  • If you get the resolves to a package error ensure that the files are in the right place, for example if you have C class in package a.b you need the C.class file to be in WEB-INF/classes/a/b/C.class. If it is, try simply restarting your IDE/server.
NonameSL
  • 1,405
  • 14
  • 27
  • [here](https://stackoverflow.com/questions/239147/how-do-you-import-classes-in-jsp) is how to import the classes. Regarding the location, you can put it in `src` folder, they will be compiled and put into `classes`. – Darshan Mehta Jun 18 '17 at 10:50
  • @DarshanMehta how exactly and when do they get compiled into `classes` from `src`? – NonameSL Jun 18 '17 at 12:30

1 Answers1

1

JSP file can import files with class extension and not java extension.

Class file is a compiled java file.

You need to compile you java file which will create FooBar.class

One option is to save FooBar.class file in a jar under /WEB-INF/lib

Second option is save FooBar.class inside your classes folder under relevant pack for example if your package is a.b /WEB-INF/classes/a/b/FooBar.class

Ori Marko
  • 56,308
  • 23
  • 131
  • 233