2

I have a index.jsp which has included header.jsp and frontpage.jsp as follow:

<body>
...
    <%@ include file="include/header.jsp"%>
...
<table>...<td> <%@ include file="include/frontpage.jsp"%></td>....

In header.jsp:

...
    String __jspName = this.getClass().getSimpleName().replaceAll("_", ".");
    System.out.println("[header.jsp] used user quota = "+usedNum);
...

In frontpage.jsp:

...
    String __jspName = this.getClass().getSimpleName().replaceAll("_", ".");
    System.out.println("[frontpage.jsp] "front_url = " + fp_front_url);
...

Actually, this is an accident that I forget to remove one of the declaration. But when I run index.jsp under Tomcat 6. It works normally and in catalina.out (note:I omit the value of front_url for security reason)

...
[header.jsp] used user quota = 0
[frontpage.jsp] front_url = ...
...

My confusion is: "Why JVM doesn't report a 'variable redefinition' exception?"

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
Scott Chu
  • 972
  • 14
  • 26

1 Answers1

2

Your two JSP files are compiled to two different servlets by the JSP compiler. The variable in each case is scoped to its respective servlet class, so it's very much the same as if you would declare a field with the same name in two separate Java classes: no conflict occurs.

This is obvious in your code, as you're relying on the class name of your compiled JSP page to set the __jspName variable:

String __jspName = this.getClass().getSimpleName().replaceAll("_", ".");

This yields header.jsp and frontpage.jsp for your two included files, indicating that they've been compiled to two separate classes.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • But I thought what you said is for jsp:include. I thought @include is just like macro. If not, what's the difference between jsp:include and @include=? – Scott Chu Jul 13 '17 at 08:42