Understanding include in JSP (ii): Jsp-include test

Source: Internet
Author: User
Tags include
Include|js First Example:

File1:

<%
String str = "AAAAA";
%>
<jsp:include page= "jsp-include-slave.jsp"/>

<%=str%>

File2:

<%
str = str + "BBBBB";
%>

Results:

Error accessing File1, indicating that variable STR cannot be resolved in file2.

Analysis:

When requesting File1, Tomcat first converts two JSP files to servlet (. java) files. You then attempt to compile both into. class files, at which point File1 compiles normally, and file2 does not compile.

Conclusion:

Unlike the Include-file directive, which merges two files into a servlet file for compilation and execution, Jsp-include requires two files to be relatively independent and complete.





A second example:

File1:

<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>
<%
Request.setattribute ("str", "AAAAAAAAAAAAAAAA");
%>
<jsp:include page= "jsp-include-slave.jsp"/>


File2:

<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>
<%
String str = (string) request.getattribute ("str");
Out.println (str);
%>

Results:

Aaaaaaaaaaaaaaaa

Analysis:

Because two pages are relatively independent, you can pass requests in between. That is, when a request is file1, File1 automatically requests file2.

Conclusion:

Using Jsp-include's two pages is a request relationship, not a relationship. This is different from include-file.





A third example:

File1:

<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>
<%
Request.setattribute ("str", "AAAAAAAAAAAAAAAA");
%>
<jsp:include page= "jsp-include-slave.jsp"/>
<br>
<%
String str = (string) request.getattribute ("str");
Out.println (str);
%>


File2:

<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>
<%
String str = (string) request.getattribute ("str");
Request.setattribute ("str", str+ "bbbbbbbbbbbbbbb");
%>

Results:

aaaaaaaaaaaaaaaabbbbbbbbbbbbbbb

Conclusion:

The two-page request relationship is bidirectional.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.