【Java Web】页面包含

来源:互联网 发布:淘宝怎么选爆款 编辑:程序博客网 时间:2024/04/28 02:12

页面包含分为两种:静态包含和动态包含。

【静态包含】

语法格式:

<%@ includefile="/header.jsp" %>

实例

index.jsp

<%@ pagelanguage="java" import="java.util.*"pageEncoding="GB18030"%><%@ includefile="/header.jsp" %><%@ includefile="/content.jsp" %><%@ includefile="/footer.jsp" %>

header.jsp

<html><head><metahttp-equiv="Content-Type" content="text/html;charset=GB18030"><title>Inserttitle here</title></head><body><h1>header</</h1><hr>

content.jsp

yangtianchangxiaochumenqu

footer.jsp

<hr>footer</body></html>

要看的结果有三个:

第一,编译的Java类结果


第二,运行的页面效果:


第三,编译的Java类代码:

package org.apache.jsp;import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase    implements org.apache.jasper.runtime.JspSourceDependent {  private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();  private static java.util.List _jspx_dependants;  static {    _jspx_dependants = new java.util.ArrayList(3);    _jspx_dependants.add("/header.jsp");    _jspx_dependants.add("/content.jsp");    _jspx_dependants.add("/footer.jsp");  }  private javax.el.ExpressionFactory _el_expressionfactory;  private org.apache.AnnotationProcessor _jsp_annotationprocessor;  public Object getDependants() {    return _jspx_dependants;  }  public void _jspInit() {    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();    _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());  }  public void _jspDestroy() {  }  public void _jspService(HttpServletRequest request, HttpServletResponse response)        throws java.io.IOException, ServletException {    PageContext pageContext = null;    HttpSession session = null;    ServletContext application = null;    ServletConfig config = null;    JspWriter out = null;    Object page = this;    JspWriter _jspx_out = null;    PageContext _jspx_page_context = null;    try {      response.setContentType("text/html; charset=GB18030");      pageContext = _jspxFactory.getPageContext(this, request, response,      null, true, 8192, true);      _jspx_page_context = pageContext;      application = pageContext.getServletContext();      config = pageContext.getServletConfig();      session = pageContext.getSession();      out = pageContext.getOut();      _jspx_out = out;      out.write('\r');      out.write('\n');      out.write("<html>\r\n");      out.write("<head>\r\n");      out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=GB18030\">\r\n");      out.write("<title>Insert title here</title>\r\n");      out.write("</head>\r\n");      out.write("<body>\r\n");      out.write("\t<h1>header</</h1>\r\n");      out.write("\t<hr>");      out.write('\r');      out.write('\n');      out.write("yangtianchangxiaochumenqu\r\n");      out.write("\r\n");      out.write("\r\n");      out.write("<hr>\r\n");      out.write("footer\r\n");      out.write("</body>\r\n");      out.write("</html>");      out.write(" \r\n");      out.write("\r\n");      out.write('\r');      out.write('\n');    } catch (Throwable t) {      if (!(t instanceof SkipPageException)){        out = _jspx_out;        if (out != null && out.getBufferSize() != 0)          try { out.clearBuffer(); } catch (java.io.IOException e) {}        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);      }    } finally {      _jspxFactory.releasePageContext(_jspx_page_context);    }  }}

【动态包含】

语法格式:

(1)不带参数:<jsp:include page="/footer.jsp"/>

(2)带参数:

           <jsp:includepage="/footer.jsp"/>

            <jsp:paramvalue="这是参数" name="参数名" />

            </jsp:include>

实例:

只改动了上例中的index.jsp页面,如下:

<%@ pagelanguage="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%><%@ includefile="/header.jsp" %><%@ includefile="/content.jsp" %> <%-- <%@ includefile="/footer.jsp" %> --%> <jsp:includepage="/footer.jsp"></jsp:include>

同样看两个结果,

第一,编译的Java类:


第二,运行的页面效果:


第三,Java类代码:

index_jsp.java:

package org.apache.jsp;import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase    implements org.apache.jasper.runtime.JspSourceDependent {  private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();  private static java.util.List _jspx_dependants;  static {    _jspx_dependants = new java.util.ArrayList(2);    _jspx_dependants.add("/header.jsp");    _jspx_dependants.add("/content.jsp");  }  private javax.el.ExpressionFactory _el_expressionfactory;  private org.apache.AnnotationProcessor _jsp_annotationprocessor;  public Object getDependants() {    return _jspx_dependants;  }  public void _jspInit() {    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();    _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());  }  public void _jspDestroy() {  }  public void _jspService(HttpServletRequest request, HttpServletResponse response)        throws java.io.IOException, ServletException {    PageContext pageContext = null;    HttpSession session = null;    ServletContext application = null;    ServletConfig config = null;    JspWriter out = null;    Object page = this;    JspWriter _jspx_out = null;    PageContext _jspx_page_context = null;    try {      response.setContentType("text/html; charset=GB18030");      pageContext = _jspxFactory.getPageContext(this, request, response,      null, true, 8192, true);      _jspx_page_context = pageContext;      application = pageContext.getServletContext();      config = pageContext.getServletConfig();      session = pageContext.getSession();      out = pageContext.getOut();      _jspx_out = out;      out.write('\r');      out.write('\n');      out.write("<html>\r\n");      out.write("<head>\r\n");      out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=GB18030\">\r\n");      out.write("<title>Insert title here</title>\r\n");      out.write("</head>\r\n");      out.write("<body>\r\n");      out.write("\t<h1>header</</h1>\r\n");      out.write("\t<hr>");      out.write('\r');      out.write('\n');      out.write("yangtianchangxiaochumenqu\r\n");      out.write("\r\n");      out.write("\r\n");      out.write("\r\n");      out.write("\r\n");      org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "/footer.jsp", out, false);      out.write('\r');      out.write('\n');    } catch (Throwable t) {      if (!(t instanceof SkipPageException)){        out = _jspx_out;        if (out != null && out.getBufferSize() != 0)          try { out.clearBuffer(); } catch (java.io.IOException e) {}        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);      }    } finally {      _jspxFactory.releasePageContext(_jspx_page_context);    }  }}
footer_jsp.java:

package org.apache.jsp;import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;public final class footer_jsp extends org.apache.jasper.runtime.HttpJspBase    implements org.apache.jasper.runtime.JspSourceDependent {  private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();  private static java.util.List _jspx_dependants;  private javax.el.ExpressionFactory _el_expressionfactory;  private org.apache.AnnotationProcessor _jsp_annotationprocessor;  public Object getDependants() {    return _jspx_dependants;  }  public void _jspInit() {    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();    _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());  }  public void _jspDestroy() {  }  public void _jspService(HttpServletRequest request, HttpServletResponse response)        throws java.io.IOException, ServletException {    PageContext pageContext = null;    HttpSession session = null;    ServletContext application = null;    ServletConfig config = null;    JspWriter out = null;    Object page = this;    JspWriter _jspx_out = null;    PageContext _jspx_page_context = null;    try {      response.setContentType("text/html");      pageContext = _jspxFactory.getPageContext(this, request, response,      null, true, 8192, true);      _jspx_page_context = pageContext;      application = pageContext.getServletContext();      config = pageContext.getServletConfig();      session = pageContext.getSession();      out = pageContext.getOut();      _jspx_out = out;      out.write("<hr>\r\n");      out.write("footer\r\n");      out.write("</body>\r\n");      out.write("</html>");    } catch (Throwable t) {      if (!(t instanceof SkipPageException)){        out = _jspx_out;        if (out != null && out.getBufferSize() != 0)          try { out.clearBuffer(); } catch (java.io.IOException e) {}        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);      }    } finally {      _jspxFactory.releasePageContext(_jspx_page_context);    }  }}

【区别】

         <%@ includefile=""%>指令在编写代码的时候一般放在最顶部,主要附加的是被重用的代码,这里所说的附加是指把源代码原封不动的附加过来,所以附加的页面代码要如同上例中,最后的结果是把几个页面的代码原封不动的放在一起,是一个正确的页面描述。

         <jsp:include>标签导入一个重用文件的时候,这个文件是经过编译的,通俗点说就是附加这个要导入文件经过编译后的效果,所以可以含有与当前jsp程序中重复的内容,因为在附加过来之前就会被解析掉。

        综上,静态包含是先将几个页面内容原封不动放在一个页面中,然后再进行编译。动态包含是先将各个页面进行编译,然后将编译之后的Java类根据包含顺序合并在一起。

0 0