2

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.

Solution
  • 31
  • 1
  • 2
  • 10
  • what is package of your java class? – nahab Jan 03 '14 at 12:48
  • Just an advice. **Class names starts with caps**. For example Ps123. – Sergi Jan 03 '14 at 12:49
  • i will take care about it. but it will not give me an error – Solution Jan 03 '14 at 12:50
  • myPackage? Then `<%@ page import="myPackage.ps123" %>` – Sergi Jan 03 '14 at 12:50
  • @Sergi .. i have done all this things.. still got same error. i cant solve my self thats why i have to ask over here – Solution Jan 03 '14 at 12:51
  • Now I understand. You get that error because your class starts with a lowercase. Rename the class as I said in the previous comment. Then the line should be `<%@ page import="myPackage.Ps123" %>`. – Sergi Jan 03 '14 at 12:52
  • 1
    Something else to consider: http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files – radimpe Jan 03 '14 at 12:54
  • @Sergi The type myPackage.Ps123 is not visible . i got this error – Solution Jan 03 '14 at 12:55
  • What IDE are you using? – Sergi Jan 03 '14 at 12:55
  • complied???? java or jsp??? – Solution Jan 03 '14 at 12:56
  • Bad choice in 2014. Download Eclipse or Netbeans. – Sergi Jan 03 '14 at 12:56
  • @Sergi why EClipse require?? i just want to make one java file and jsp file .. whats need of these kind of editor.. we can do easy with simple editor also. – Solution Jan 03 '14 at 12:58
  • @Aniket ... ofcourse... i have compile.. its basic need – Solution Jan 03 '14 at 12:58
  • With a text editor you do not have debugging, error handling, etc. ... Check this [link](https://www.cise.ufl.edu/~bn0/jsp_eclipse_tutorial/). – Sergi Jan 03 '14 at 12:59
  • @Sergi. this is simple error no need to debug – Solution Jan 03 '14 at 13:01
  • I know. The simple error is class starts with uppercase, package not. Your application doesn't find the class because you entered a package name (`mypackage.ps123` is a package name). I was just telling you that working with an IDE is infinitely better than a text editor. – Sergi Jan 03 '14 at 13:06
  • There's nothing *broken* by having a classname start with a lowercase letter, it's just not the recommended naming style. Do you also have a package or folder named ps123 in mypackage? – nitind Jan 03 '14 at 14:14
  • *"why EClipse require??"* - It is not *required*. It just makes things easier for you if you use an IDE. For the record, I took the plunge and switched to using Eclipse instead of emacs/vi in (I think) 2000. Maybe it is time that you did too. – Stephen C Jan 03 '14 at 14:26

1 Answers1

1

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; e.g. 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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I'm still not convinced. Now the error message doesn't match either the name your class or the name you used in your JSP! – Stephen C Jan 04 '14 at 07:30