Correct in several JSP errors

zhaozj2021-02-16  43

Recently, I have to use JSP dynamics to use JSP. I am definitely simple, but I don't want to be so complicated, and I haven't found a good answer, the problem is as follows: ---------------- -------------------------- # file: one.jsp <%! String var1 = "China";%> ------ ---------------------------------- # file Two.jsp <%! String var1 = "America"; string VAR2 = "中l";%> -------------------------------------- # file Three.jsp <% int J = 1; if (j == 1) {%> <% @ include file = "one.jsp"%> <%} else {%> <% @ include file = "Two.jsp "%> <%}%> <% = var1%> <% = var2%> -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------

What is the result of doing three.jsp? a. Compilation error b. Showing China England a lot of people think that it is definitely a, because J = 1 is only included with one.jsp, two.jsp will not contain it, but the answer is B, and you know. why? Because @include wants to execute the other code of JSP, two files will be included! ---------------------------------------- If you say JSP: include include can solve the problem , Good, change three.jsp to the following: # file three.jsp <% int J = 1; string includefile = ""; if (j == 1) {includefile = "one.jsp";} else {includefile = "Two.jsp";}%>

Is B, no, it is a, compiled error! Tip VAR1 is undefined.

why? Because JSP: Include is dynamic, it is equivalent to compiling the included file to the included file. ----------------------------------------

The problem of INCLUDE is summarized as follows: [Quote GFZHX (small)] The dynamic contains the same page jump, which is equivalent to re-accessing another servlet, so of course, it will prompt the variables that are not defined, you want one Think, a class has declared a variable, the second class is directly used with this variable, can you say it? In fact, your situation and this example is very like, this is dynamic, and static contains you can take a look at the JSP compiled java file, it is directly containing the page you contain directly, then compile. So your problem is available in static. But no matter what, it is not recommended to use this form, which will cause many problems to the procedure, at least more difficult to maintain, can be said to be a bad programming style. It is recommended to solve problems in other ways. [Quote XIAO_YUER "] To use the variable defined in the introduction file, you can only use the @include instruction. It is also <% @ include "one.jsp"%>, but this is not dynamic under normal circumstances, which is imported when the JSP page is compiled. After the JSP compiles, these two files make a modification of the JSP server without detecting, because the JSP file containing the two JSPs itself has not changed. But very strange, weblogic6 seems to be. You can try it, but don't hold too much hope, because you are not very reasonable. In this case, you should introduce a Java class, which defines some variables in this class (according to your statement, it is constant, JSP is taken out without modifying it, then you can go back), then get it again in JSP The instance of that class is processed. That if you want to modify the values ​​of these constants, modify the Java class without modifying the JSP.

[自己 的 :-))] @Include includes static inclusion, is to add the included file to the included file and then compile, so this containing the language that is performed very much (for example, PHP), and JSP @ 开 开The statement must be executed before other statements, so as above, use if.else to determine if it is not, it is not possible, so if you do PHP this explanation language will feel uncomfortable (like me,: (() JSP : INCLUDE is a statically included manner that is different from @Include, jsp: incrude does not have the priority like @Include, ie, is not existing in other statements, so JSP: Include can selectively contain. But more importantly, with JSP: Include is equivalent to compiling two different files, so if you are only displayed in the file (for example, if you are pure HTML), this situation uses JSP. : INCLUDE and @include are the same, but if you want to use JSP: include to display the variables defined in the included file (why? See the reference above, don't you have described).

[Thank] GFZHX (small), xiao_yuer (small fish)

【版 主】 The problem address is as follows: http://www.9cbs.net/expert/faq/faq_index.asp? Id = 1380HTTP: //www.9cbs.net/ExPERT/topic/707/707524.xml? Temp = 1.226443e-02 I hope the moderator can correct it.

转载请注明原文地址:https://www.9cbs.com/read-26194.html

New Post(0)