javaweb-JSP instruction

A, JSP instructions Introduction

  JSP directive (directive) is designed for JSP engine, they do not directly produce any visible output, but simply tells the engine how to deal with the rest of the JSP page.

  Specification defines the JSP 2.0 CCP is three instructions:

  • page directive
  • include instruction
  • taglib directive       

  JSP basic instruction syntax: <% @ directive attribute name = "value"%>

  E.g:

1 <%@ page  contentType="text/html; charset=UTF-8""%>

  If an instruction has a plurality of attributes, the plurality of attributes which can be written in a single instruction can be written separately.

E.g:

1 <%@ page contentType="text/html; charset=UTF-8" %>
2 <%@ page import="java.util.Date" %>

It can also be written as:

1 <%@ page contentType="text/html; charset=UTF-8" import="java.util.Date"%>

Two, Page command

  page directive to define the various attributes of the JSP page, regardless of page directive appears in the JSP page where it is the role of the entire JSP page, in order to maintain readability and follow good programming practice, most page directive good starting position is on the whole JSP page, for example:

 

 

 

 Full page directive JSP 2.0 syntax defined in the specification;

<%@ page 
     [ language="java" ] 
     [ extends="package.class" ] 
     [ import="{package.class | package.*}, ..." ] 
     [ session="true | false" ] 
     [ buffer="none | 8kb | sizekb" ] 
     [ autoFlush="true | false" ] 
     [ isThreadSafe="true | false" ] 
     [ info="text" ] 
     [ errorPage="relative_url" ] 
     [ isErrorPage="true | false" ] 
     [ contentType="mimeType [ ;charset=characterSet ]" | "text/html ; charset=ISO-8859-1" ] 
     [ pageEncoding="characterSet | ISO-8859-1" ] 
     [ isELIgnored="true | false" ] 
%>

2.1, import property page directive

  Jsp pages, Jsp engine will automatically import the following package

  • java.lang. *
  • javax.servlet.*
  • javax.servlet.jsp.*
  • javax.servlet.http.*  

  Properties can be introduced in the import of a page in a plurality of instruction class or package, using a comma between each package or class wherein the (,) separated

E.g:

<%@page import="java.util.*,java.io.*,java.math.*"%>

The above statement can be written using a plurality of page directive impoert introduced separately to each packet attribute or category

E.g:

1 <%@page import="java.math.*"%>
2 <%@page import="java.io.*"%>
3 <%@page import="java.util.*"%>

Three, include instructions

  Jsp to include in the form of two statements:

  1. @ incude instruction

  2. <jsp: include> directive

3.1, @ include command

   @include can contain any file, of course, only the contents of the file included.

   i nclude instructions for the introduction of other JSP pages, if you use the include directive introduces other JSP page, the JSP engine will translate into a Servlet these two JSP. Therefore it includes instruction is introduced are also known as static introduced.

Syntax: <% @ the include file = "relativeURL"%> , where file attribute specifies the path to the file is introduced. The path begins with "/", said the representative of the current web application.

  1. File is introduced it must follow the JSP syntax

  2. The document introduced can use any name to expand, its expansion is immediate html, JSP engine also handles the content inside it in the manner jsp page, in order to see to know the name of Italy, JSP specification recommends the use of .jspf (JSP fragments ( )) extension as still introducing document.

  3. As will involve using the commands include JSP page 2, and will be translated into a two JSP the servlet, JSP pages so these two instructions can not conflict (except except pageEncoding guide and package).

include instruction usage examples:

  Head.jspf new page and the page foot.jspf respectively as head and tail jsp pages, stored in WebRoot jsp files under the folder, as follows:

 

head.jspf Code:

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<h1 style="color: red">页面头部</h1>

foot.jspf Code:

<%@ page language="java" pageEncoding="UTF-8"%>
<h1 style="color:blue">页面尾部</h1>

  WebContent folder is created under a includeAll.jsp page, the page in IncludeTagTest.jsp @include instruction is introduced head.jspf foot.jspf page and pages, as follows:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp的include指令测试</title>
</head> 
< Body > 
    <-! Use labels include the introduction of other JSP pages -> 
    <% @include File = " /jsp/head.jsp "  %> 
    < h1 > page body content </ h1 > 
    <% @include File = " /jsp/foot.jsp "  %> 
</ body > 
</ HTML >

result:

 

We look at the source code jsp engine after includeAll.jsp translated into IncludeAll_jsp class, find the Tomcat server work \ Catalina \ localhost \ test \ org \ apache find \ jsp directory IncludeTagTest_jsp.java, as shown below:

 

 

 

 

 Open includeAll_jsp.java, as shown in the following code:

package org.apache.jsp.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;

public final class includeAll_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {

  private static final javax.servlet.jsp.JspFactory _jspxFactory =
          javax.servlet.jsp.JspFactory.getDefaultFactory();

  private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;

  static {
    _jspx_dependants = new java.util.HashMap<java.lang.String,java.lang.Long>(2);
    _jspx_dependants.put("/jsp/foot.jspf", Long.valueOf(1570108097325L));
    _jspx_dependants.put("/jsp/head.jspf", Long.valueOf(1570108081184L));
  }

  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.tomcat.InstanceManager _jsp_instancemanager;

  public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
    return _jspx_dependants;
  }

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
  }

  public void _jspDestroy() {
  }

  public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
        throws java.io.IOException, javax.servlet.ServletException {

    final javax.servlet.jsp.PageContext pageContext;
    javax.servlet.http.HttpSession session = null;
    final javax.servlet.ServletContext application;
    final javax.servlet.ServletConfig config;
    javax.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
    javax.servlet.jsp.JspWriter _jspx_out = null;
    javax.servlet.jsp.PageContext _jspx_page_context = null;


    try {
      response.setContentType("text/html; charset=UTF-8");
      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\n");
      out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
      out.write("<html>\r\n");
      out.write("<head>\r\n");
      out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
      out.write("<title>jsp的include指令测试</title>\r\n");
      out.write("</head>\r\n");
      out.write("<body>\r\n");
      out.write("\t<!-- 使用include标签引入其它JSP页面 -->\r\n");
      out.write("\t");
      out.write("\r\n");
      out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
      out.write("<html>\r\n");
      out.write("<head>\r\n");
      out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\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 style=\"color: red\">页面头部</h1>\r\n");
      out.write("</body>\r\n");
      out.write("</html>");
      out.write("\r\n");out.write("\t
      <h1>网页主体内容</h1>\r\n");
      out.write("\t");
      out.write("\r\n");
      out.write("<body>\r\n");
      out.write("\t<h1 style=\"color:blue\"> End of the page </ h1 of> >body</
      out.write ("\ R & lt \ n-");\r\n");
      out.write("</html>");
      out.write("\r\n");
      out.write("</body>\r\n");
      out.write("</html>");
    } catch (java.lang.Throwable t) {
      if (!(t instanceof javax.servlet.jsp.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);
        else throw new ServletException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}

  We can see, the page content head.jspf and foot.jspf use out.write output to the browser shows.

3.2 summarizes @include instruction

  Use @include can contain any content, file extension does not matter what. This contains the contents of the file to the other @include statement called on the page itself contains static, but the role of the other content of the page include it, belongs to the static includes.

3.3, jsp: include directive

  jsp: include directive to include dynamic, if included in the page after the JSP, then the result contains the first deal, and if it contains a non-* .jsp file, the contents of the file still contains just come in, and @include similar function. Behind more specific description

 

Guess you like

Origin www.cnblogs.com/sacai/p/11620961.html