Search code examples
jspimportduplicatesresolve

page import directives are duplicated


I have two pages. A and B.

A and B have to import the same package Such as

In page A,

<%@ page import="java.util.Calendar" %>
<%@ page import="java.util.TimeZone" %>

And in page B,

<%@ page import="java.util.TimeZone" %>

============================================

Problem is..

I have to include page B within the page A sometimes

and if I do that, it makes duplication exception.

Right now, my server doesn't throw the duplication exception

but I got the exception from the other server which is not mine.

It look just like this image.

"Only a type can be imported blar blar".

============================================

Is there any way to make sure that the package is imported only one time??

+++++++++++++++++++++++++++++++++

The image is just an example. Point is this sentence; "Only a type can be imported blar blar".

Let me make things clear.

  1. I made two pages. A and B.
  2. Each pages imports class T with 'page import' directive of jsp.

  3. Page B has a variable of class T.

    T inst = new T();

  4. Page A includes page B with 'include' directive of jsp.

  5. I access to the page A, I get this compile error sometimes

    Only a type can be imported. java.util.T resolves to a package.

I believe this is caused because I imported the same library in two different pages and I included page B in page A.

Is there any way to solve this without any server configuration??


Solution

  • Create a common file that is shared by each page A and B. You can write your import statements in it and only include it at the top of the main page. Avoid imports from A and B. That way you can make sure imports will not be duplicated. I have used this approach in many projects. In situation like this I envy PHP's require_once / include_once

    Hope this will help you.

    Enjoy