3

see you all know about including file in a jsp... i have 2 files

one.jsp two.jsp

in two.jsp i have below given code

Long something = 0;

in one.jsp code is

<%@include file='two.jsp'%>
<%@include file='two.jsp'%>

i have included same file two times,

so there will be error because something variable will be created 2 times, right?

so it means before including file second time i should check that, if once its included then i should not inculde it again, how to check that ?

as in php there are functions like include("file.php"); //that will include file

and include_once("file.php"); //this will include file if its not included before,

ANY ANY solutions for that????

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
Jayesh
  • 3,661
  • 10
  • 46
  • 76

1 Answers1

2

Indeed, having those two static includes will result in an error of having the same variable declared twice. Those imports, at compilation time, will append the code of the included page in the including one, and will then be compiled altogether.

I'm not aware of any include_once approach built-in in JSP, but you could do something similar by having a global Set (i.e. HashSet), declared as a variable in the top-level page, or as a request attribute (that you should be clearing when the processing ends) in which you could be adding the names of the pages already included.

one.jsp

<% HashSet<String> pagesSet = new HashSet<String>(); %>
...
<%@include file='two.jsp'/>

two.jsp

<% if (!pagesSet.contains("two.jsp"){ %>
   //... Remember to actually ADD two.jsp to pageSet, 
   // because it IS being included NOW.
   pageSet.add("two.jsp");

   // Entire contents of two.jsp
   // ....
<% } %>

Note this is quite similar to the #ifndef #define #endif pattern in C.

Take into account that using dynamic includes you would be avoiding the problem of having duplicate variables. The included page would execute on its own scope, and to access server-side variables you'll have to pass them through with <jsp:param> or in one of Request, Session or Application scopes. See this question's answer for the differences between static and dynamic includes: include directive and attribute name problem

Additionally, if you have a great number of those "conditional" static includes, you can end up hitting the 64K method limit

Community
  • 1
  • 1
Xavi López
  • 27,550
  • 11
  • 97
  • 161
  • Please, take into account the matisations I last introduced in the answer, and remember that scriptlets (java code in `<% %>` in JSP's) are considered [poor practice](http://stackoverflow.com/questions/3177733). You should be using [Unified Expression Language](http://download.oracle.com/javaee/5/tutorial/doc/bnahq.html) and [JSTL](http://download.oracle.com/javaee/1.4/tutorial/doc/JSTL.html#wp74644). – Xavi López Oct 16 '11 at 09:40
  • I'm also sorry, but this is not a code factory. Of course I've **not tested** this snippet, and now I won't. The suggestions in the answer should be enough. What problem are you having? – Xavi López Oct 17 '11 at 06:33
  • before including the two.jsp in one.jsp page i have to check that two.jsp is included or not in the one.jsp , if two.jsp is not included then it should be included. like this.... in your code two.jsp is called two times and that i dont wont, just give me tested code – Jayesh Oct 17 '11 at 07:23
  • As I said, there's no equivalent of PHP's `include_once()` that I know of, all you can do is do this checking yourself, with i.e. a `HashSet` that contains the names of already included pages. Take into account the differences between static and dynamic imports. If you use a static import, the page will **always** be included. So, the solution for you is to use dynamic imports (``), and put them inside `if`'s that check if the page has already been included. To check this, you can use a global `Set` variable declared in the top-level page, or put it in the request as an attribute. – Xavi López Oct 17 '11 at 07:39
  • dynamic include dont include file... but it will just include "response" given by that file in out put.... and in <%@include file="some.jsp"%> will include code of some.jsp file in to current page so i need solution use <%@include file="some.jsp"%> and i need this one.. static include... and you told me that hashset contains all included files , but it not true, i have checked it, so hashset not contains any file – Jayesh Oct 17 '11 at 07:51
  • Jayesh, of course, you have to add `"some.jsp"` to the `HashSet` once included. I've added this to the _untested snippet_... – Xavi López Oct 17 '11 at 07:59
  • but if we have 1000 jsp file to be included in one.jsp then we have to add all the jsp to the hashset, means we have to write 1000 lines for adding those 1000 jsp file, is this good programming to write 1000 lines ? so we should have one proper way that checks that two.jsp is included or not, have you any this type of solution ? – Jayesh Oct 17 '11 at 08:16
  • If you already have those includes coded, I'm afraid I don't see any easy solution. Unless you manage to manipulate the files externally with macros or something, like inserting at the start of the file that block with help of a filename variable. By the way, your sentence "before including the two.jsp in one.jsp page i have to check that two.jsp is included or not in the one.jsp , if two.jsp is not included then it should be included" speaks clearly of dynamic includes. – Xavi López Oct 17 '11 at 08:24
  • i am on chat with you personally, come on chat pls, or just check my ping msg. – Jayesh Oct 17 '11 at 08:29