Understand the include (2): JSP-Include Test in JSP

xiaoxiao2021-03-06  120

First example:

File1:

<%

String str = "aaaaa";

%>

<% = STR%>

File2:

<%

Str = STR "BBBBB";

%>

result:

An error occurred while accessing file1, prompting the variable Str in File2.

analysis:

When requested File1, Tomcat first converts two JSP files to servlet (.java) files. Then try to compile the two into .class files, when File1 is compiled normally; File2 cannot be compiled by compilation.

in conclusion:

Unlike the incrude-file instructions to compile the two files into a servlet file, JSP-Include requires two files to be relatively independent, complete.

Second example:

File1:

<% @ page contenttype = "text / html; charset = GBK"%>

<%

Request.setttribute ("Str", "Aaaaaaaaaaaaaaaa");

%>

File2:

<% @ page contenttype = "text / html; charset = GBK"%>

<%

String str = (String) Request.getaTribute ("str");

Out.println (STR);

%>

result:

AAAAAAAAAAAAAAAAAAAAAAA

analysis:

Due to the relatively independent of the two pages, requests can be delivered. That is, when requesting file1, File1 will automatically request file2.

in conclusion:

Two pages using JSP-Include are a request relationship instead of a relationship. This is different from the include-file.

Third example:

File1:

<% @ page contenttype = "text / html; charset = GBK"%>

<%

Request.setttribute ("Str", "Aaaaaaaaaaaaaaaa");

%>


<%

String str = (String) Request.getaTribute ("str");

Out.println (STR);

%>

File2:

<% @ page contenttype = "text / html; charset = GBK"%>

<%

String str = (String) Request.getaTribute ("str");

Request.setttribute ("Str", Str "Bbbbbbbbbbbb");

%>

result:

Aaaaaaaaaaaaaaabbbbbbbbbbbbbbb

Conclusion: The request relationship between the two pages is two-way.

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

New Post(0)