The difference between @include and Jsp:include in JSP

Source: Internet
Author: User

1 Preface

Java developers probably know that there are two ways to introduce other files in a JSP into a project.

<% @include file= "xxx.jsp"%>   <jsp:include page= "xxx.jsp" ></jsp:include>

We may use both of these methods, but perhaps many people do not name the difference between the two ways. Let's take a look at the following two examples

2 straight to the point

(1)/include/include.jsp

<%@ Page Language="Java"pageencoding="UTF-8"%><%StringPath=Request.getcontextpath ();%><H1>Include Page</H1>

(2)/index1.jsp page

<%@ Page Language="Java"pageencoding="UTF-8"%><%StringPath=Request.getcontextpath ();%><!DOCTYPE HTML><HTML>  <Head>  </Head>  <Body>      <H1>Index page</H1>       <%@include File="/include/include.jsp"%>  </Body></HTML>

(3)/index2.jsp

<%@ Page Language="Java"pageencoding="UTF-8"%><%StringPath=Request.getcontextpath ();%><!DOCTYPE HTML><HTML>  <Head>  </Head>  <Body>      <H1>Index page</H1>      <Jsp:includepage= "/include/include.jsp"></Jsp:include>  </Body></HTML>

Now we have access to index1.jsp and the results are as follows

So we continue to visit index2.jsp and the results are as follows

Why, how index1.jsp why visit error, ah, it seems @include and Jsp:include is a difference it. Only by understanding the underlying differences between the two ways of @include and jsp:include, we know why the index1.jsp page is wrong. Just keep looking and you'll understand.

3 @include and Jsp:include internal differences

To demonstrate the correct effect of @include, we'll write two more pages

(1)/include/include1.jsp

<%@ Page Language="Java"pageencoding="UTF-8"%><H1>Include1 page</H1>

(2)/index3.jsp

<%@ Page Language="Java"pageencoding="UTF-8"%><%StringPath=Request.getcontextpath ();%><!DOCTYPE HTML><HTML>  <Head>  </Head>  <Body>      <H1>Index page</H1>      <%@include File="/include/include1.jsp" %>      </Body></HTML>

We visit the index3.jsp page

OK, we found the result is correct, can compare the include1.jsp and include.jsp see where the change. We know that when we visit a JSP page, the servlet container (such as Tomcat) compiles the JSP into the servlet Java code. Let's look at the servlet Java code generated by Tomcat for Index1.jsp and index3.jsp. In Tomcat we can find the code for Tomcat to compile the JSP into a servlet in the "/tomcat directory/work/catalina/localhost/project name/".

(1) index3.jsp corresponding servlet code

(2) The servlet code corresponding to the index2.jsp page

The include.jsp corresponding servlet code was also generated when accessing ndex2.jsp

Servlet code generated from Tomcat we can draw the difference between the two

4 Conclusion

@include: Pre-compiling the page request, all the code is included, processed together, all the code together, compiled into a servlet

Jsp:include all the code is processed separately, compiled when the page is requested, compiled into multiple servlets, the page syntax is relatively independent, and the results of the code (processing results) are combined after processing is complete.

5 back to the question again

Why this article in the beginning of the question index1.jsp page error, you know, because the include.jsp page and index1.jsp page contains the following code.

<%      = request.getcontextpath ();   %>

Because we adopt the Include method, so that the page request before all the code is included in the first and then processed together, so that the code has two path variables. This does not conform to the Java syntax. All of them reported 500 mistakes. Why index2.jsp is not wrong, because the use of the jsp:include way, such two pages are compiled separately, and finally the compilation results are merged. All will not go wrong.

Knock it off, get it done, finish!

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.