简体   繁体   中英

how to import java class in jsp page

Hello Every one i have one jsp page which is located at webapps/ROOT and one java file which is in WEB-INF/classes. i have compiled my java file and get .class file also. now i want to import my class file into jsp page. for this purpose i have create one folder "mypackage" and put my both (class and java) files in it.after this i have try to import into jsp file. for that i have write following code. and mypackage is in WEB-INF/classes folder.

       <%@ page  import="mypackage.Ps123" %>

here ps123 is my class name. and when i following code

      Ps123 p = new Ps123();

i got error

      Only a type can be imported.mypackage.ps123 resolves to a package
      ps123 cannot be resolved to a type

Tell me whats wrong with this code. if i dont import java class in jsp file . it works fine.

Lets start with the error message.

  Only a type can be imported.mypackage.ps123 resolves to a package
  ps123 cannot be resolved to a type

First point to make is that it is not the precise error message. Rather, you appear to have typed it in by hand ... and gotten it a bit wrong. (I find it hard to believe that an error message would be that badly punctuated!)

But assuming that the message is essentially correct, then we can infer something significant. The JSP compiler has actually found something called "mypackage.ps123", but that something is not a .class file that represents a Java class. Furthermore, since the compiler thinks that "mypackage.ps123" is a package we can infer that:

  • "WEB-INF/classes/mypackage/ps123" in your deployed webapp is a directory, OR
  • the "mypackage.ps123" package has been defined earlier in the classpath; eg by something in a Tomcat shared library directory.

The only other explanation I can think of is that your bad choice of classname is confusing the JSP compiler. If this is happening, then it is a compiler bug. But the fix would be simple, change your class name to conform to the Java style rules for identifiers: a class name should always start with an upper-case letter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM