Problem with static classes from include files

If I try to include some static class from JSP file like
<%@
  include file="../includes/someclass.jsp"
%>
With content:
static class SomeClass {.....}
IDEA can't to interpret class from this file, and marks SomeClass red color.
What can I do with it?

3 comments
Comment actions Permalink

If you look at the details of the message form the inspection that is highlighting this as an error, it explains why:

Classes from default package must bot be referenced from JSP file
Such references will not be resolved during compilation of the JSP because a compiled class for the JSP will be usually placed in non-default package (e.g. "org.apache.jsp" in case of Tomcat, "com.ibm._jsp" in case of WebSphere) but the import from the default package is prohibited in Java.

Your best course of action is to move SomeClass out of the JSP and have it as a standard Java class that gets included in your web application, and imported into the JSP where you want to use it. This would also keep with generally accepted best practices. Placing class definitions within a JSP is considered a poor practice. It muddles separation of concerns, and makes the code harder to maintain. And as demonstrated from the issue you are running into, it limits their use.

0
Comment actions Permalink

I understood that is better way. But can I setup IDEA 11 such way to support classes from includes?

0
Comment actions Permalink

It's not a case that IDEA will not let you do it. It's a case of IDEA warning you that doing it will result in non working code. You can turn off that particular inspection if you like and the red will go away. But your code will not work in most web servers. Here's why...

A web server compiles a JSP page into a Servlet class when it is first loaded. Different containers "place" the compiled Servlets representing JSP pages into different packages. For example, Tomcat places them in the package org.apache.jsp. In the case of WebSphere, the package is com.ibm._jsp. To demonstrate this, create the following simple JSP page and run it. I named it mypage.jsp and ran it in Tomcat.

<html>
<body>
<% String x = null; %>
The lowercase value of x is: <%= x.toLowerCase()%>
</body>
</html>

<html>
<body>
The lowercase value is: <%= SomeClass.value() %>

<%!
    public static class SomeClass
    {
       public static String value()
       {
           String x = null;
           return x.toLowerCase();
       }
    }
%>
</body>
</html>


and place it in the includes directory, notice this time our exception stacktrace shows:

    org.apache.jsp.includes.mypage_jsp$SomeClass.value(mypage_jsp.java:24)

This means the fully qualified name of SomeClass, when used in Tomcat, is org.apache.jsp.includes.mypage_jsp$SomeClass  But when it is used in another WebServer, such as WebSphere, it will have a different package such as  com.ibm._jsp.includes.mypage_jsp$SomeClass If you want to use SomeClass in another JSP, you need to import it. The problem is, the package needed in the import statement will be different depending on what web server you run your code in. And even if you tried to fully qualify the import for a specific web server, it will still not work. You'll get a ClassNotFoundException when you try to execute the code since that package is not in the classpath.

So in the end, this is simply a case that JSP's do not work the way you are trying to use them. It has nothing to do with IDEA not letting you do something. IDEA is simply warning you.

0

Please sign in to leave a comment.