One 、JSP Instruction Introduction

JSP Instructions (directive) Is for JSP Designed for the engine , They don't directly produce any visible output , Just tell the engine what to do with it JSP The rest of the page .

stay JSP 2.0 There are three directives defined in the specification :

  • page Instructions

  • Include Instructions

  • taglib Instructions

JSP The basic syntax format of the instruction :<%@ Instructions Property name =" value " %>
for example :

1 <%@ page contentType="text/html;charset=gb2312"%>

If an instruction has more than one attribute , These attributes can be written in one instruction , It can also be written separately .
for example :

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

You can also write :

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

Two 、Page Instructions

page Instructions are used to define JSP Various properties of the page , No matter what page The command appears in JSP Where in the page , It's all about JSP page , In order to keep the program readable and follow good programming habits ,page It's better to put the instructions in the whole JSP Start of page . for example :

JSP 2.0 As defined in the specification page The complete syntax of the instruction :

 1 <%@ page
2    [ language="java" ]
3    [ extends="package.class" ]
4    [ import="{package.class | package.*}, ..." ]
5    [ session="true | false" ]
6    [ buffer="none | 8kb | sizekb" ]
7    [ autoFlush="true | false" ]
8    [ isThreadSafe="true | false" ]
9    [ info="text" ]
10    [ errorPage="relative_url" ]
11    [ isErrorPage="true | false" ]
12    [ contentType="mimeType [ ;charset=characterSet ]" | "text/html ; charset=ISO-8859-1" ]
13    [ pageEncoding="characterSet | ISO-8859-1" ]
14    [ isELIgnored="true | false" ]
15 %>

2.1、page The directive import attribute

stay Jsp On the page ,Jsp The engine will automatically import the following packages

  • java.lang.*

  • javax.servlet.*

  • javax.servlet.jsp.*

  • javax.servlet.http.*

Can be in one page The directive import Property to introduce more than one class or package , Use commas... Between each package or class (,) Separate

for example :

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

The above statement can also be rewritten to use more than one page The directive import Property to introduce each package or class separately

for example :

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

2.2、page The directive errorPage attribute

  • errorPage Property must be set using relative path , If the “/” start , Means relative to the present Web The root directory of the application ( Note that it's not the site root ), otherwise , Represents relative to the current page

  • Can be in web.xml Use... In the document <error-page> The element is the whole Web Application settings error handling page .

  • <error-page> Elements have 3 Sub elements ,<error-code>、<exception-type>、<location>

  • <error-code> The child element specifies the wrong status code , for example :<error-code>404</error-code>

  • <exception-type> The child element specifies the fully qualified name of the exception class , for example :<exception-type>java.lang.ArithmeticException</exception-type>

  • <location> Child elements are specified with “/” The path of the error handling page at the beginning , for example :<location>/ErrorPage/404Error.jsp</location>

  • If something is set up JSP Page errorPage attribute , So in web.xml The error handling set in the file will not work on the page .

2.3、 Use errorPage Property indicates the error page to jump to after an error

such as Test.jsp The page has the following code :

1 <%@ page language="java" import="java.util.*" errorPage="/ErrorPage/error.jsp" pageEncoding="UTF-8"%> 
2 <html>
3 <head>
4 <title> test page The directive errorPage attribute </title>
5 </head>
6 <body>
7 <%
8 // This line of code is bound to go wrong , Because the divisor is 0, An exception will be thrown as soon as it runs    9 int x = 1/0;10 %>11 </body>12 </html>

stay Test.jsp in ,page The directive errorPage attribute Point out the error and jump to "/ErrorPage/error.jsp",error.jsp The page code is as follows :1 <%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>

2 <html>

3   <head>

4     <title> Error message friendly prompt page </title>

5   </head>

6   <body>

7           I'm sorry , Something went wrong , Please contact the administrator to solve !

8   </body>

9 </html>

The operation results are as follows :

2.4、 stay web.xml Use in <error-page> Label for the whole web Application settings error handling page

for example : Use <error-page> Label configuration for 404 Error handling page

web.xml Under the code of

 1 <?xml version="1.0" encoding="UTF-8"?> 
2 <web-app version="3.0"
3    xmlns="http://java.sun.com/xml/ns/javaee"
4    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
6    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
7 <display-name></display-name>
8 <welcome-file-list>
9 <welcome-file>index.jsp</welcome-file>
10 </welcome-file-list>
11
12 <!-- in the light of 404 Error handling page -->
13 <error-page>
14 <error-code>404</error-code>
15 <location>/ErrorPage/404Error.jsp</location>
16 </error-page>
17
18 </web-app>

404Error.jsp The code is as follows : 1 <%@ page language="java" import="java.util.*"pageEncoding="UTF-8"%>

2 <html>

3   <head>

4     <title>404 Error friendly prompt page </title>

5     <!-- 3 Seconds later, it will automatically jump back to the home page  -->

6     <meta http-equiv="refresh"content="3;url=${pageContext.request.contextPath}/index.jsp">

7   </head>

8   <body>

9     <img alt=" I'm sorry , The page you are going to visit is not found , Please contact the administrator !"

10    src="${pageContext.request.contextPath}/img/404Error.png"/><br/>

11     3 Seconds later, it will automatically jump back to the home page , If there is no jump , Please click on <ahref="${pageContext.request.contextPath}/index.jsp"> here </a>12  </body>

13 </html>

When visiting a nonexistent web Resource time , Will jump to at web.xml Configured in 404 Error handling page 404Error.jsp, As shown in the figure below :

2.5、 About the web.xml Use in <error-page> Label for the whole web The error handling page of application settings is in IE The solution that can't jump

What needs to be noted here is , If the error page is smaller , So when the access server does not exist web Error in resource or access server IE You can't jump to the wrong page under the browser , It is shown that ie Own error page , And in Firefox and google browsers ( Other browsers have not been tested ) There is no problem of attention .

We can prove it through the following experiments

stay web.xml Middle configuration 500 Error friendly prompt page in case of error

1 <!-- in the light of 500 Error handling page -->
2 <error-page>
3 <error-code>500</error-code>
4 <location>/ErrorPage/500Error.jsp</location>
5 </error-page>

500Error.jsp The code of the page is as follows :

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
2 <html>
3 <head>
4 <title>500( Server error ) Error friendly prompt page </title>
5 <!-- 3 Seconds later, it will automatically jump back to the home page -->
6 <meta http-equiv="refresh" content="3;url=${pageContext.request.contextPath}/index.jsp">
7 </head>
8 <body>
9 <img alt=" I'm sorry , Server error !"
10    src="${pageContext.request.contextPath}/img/500Error.png"/><br/>
11 3 Seconds later, it will automatically jump back to the home page , If there is no jump , Please click on <a href="${pageContext.request.contextPath}/index.jsp"> here </a>
12 </body>
13 </html>

500Error.jsp The byte size of the page

stay IE8 The result of running under Browser :

stay IE Next visit Test.jsp appear 500 After error , It is shown that ie Own error page , Not the one we customized 500 Error page , And in the google With Firefox, we can jump to the customized one 500 Wrong page , As shown in the figure below :

A lot of people have this problem , There are two ways to solve this problem :

1、 modify IE Browser settings ( Not recommended )

Operation steps : stay IE【 Tools 】->【Internet Options 】->【 senior 】 Tick off 【 Show friendliness http Error message 】

After this setup , After the server access error, you can directly jump to our customized 500 Wrong page , As shown in the figure below :

This method needs to modify the configuration of the client browser , This is not recommended .

2. Don't modify IE Make sure to customize the size of the error page under the browser settings >1024 byte

modify 500Error.jsp, Add more content , Make the page larger in bytes , The modified 500Error.jsp The code for is as follows :

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
2 <html>
3 <head>
4 <title>500( Server error ) Error friendly prompt page </title>
5 <!-- 3 Seconds later, it will automatically jump back to the home page -->
6 <meta http-equiv="refresh" content="3;url=${pageContext.request.contextPath}/index.jsp">
7 </head>
8 <body>
9 <img alt=" I'm sorry , The server Something went wrong , Please contact the administrator to solve !" 10    src="${pageContext.request.contextPath}/img/500Error.png"/><br/>
11 3 Seconds later, it will automatically jump back to the home page , If there is no jump , Please click on <a href="${pageContext.request.contextPath}/index.jsp"> here </a>
12 </body>
13 </html>

So I added a few more Chinese , Give Way 500Error.jsp A few more bytes ,500Error.jsp Now the number of bytes is as follows :

stay IE Next visit , When the server goes wrong , You can jump to 500Error.jsp This customized error page , As shown in the figure below :

After testing , When customizing the error page size=617bytes when , stay IE8 Now you can go to the customized error page , Other versions of IE The browser is not tested , But to be on the safe side , Customized error page size Better than 1024bytes.

2.6、 Use page Ordered isErrorPage Property to explicitly declare the page as an error page

If a certain jsp The page is the error handling page of the system , Then the proposal will page The directive isErrorPage attribute ( The default is false) Set to "true" To declare this explicitly Jsp Page is an error handling page .

for example : take error.jsp The page is explicitly declared as an error handling page

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isErrorPage="true"%> 
2 <html>
3 <head>
4 <title> Error message friendly prompt page </title>
5 </head>
6
7 <body>
8           I'm sorry , Something went wrong , Please contact the administrator to solve !
9 </body>
10 </html>

take error.jsp After the page is explicitly declared as an error handling page , What are the benefits , The advantage is Jsp The engine is turning jsp Translate the page into Servlet When , stay Servlet Of _jspService Method will declare a exception object , Then it will run jsp Error exception information is stored in exception In the object , As shown below :

because Servlet Of _jspService Method exception object , Then you can error.jsp Page usage exception object , So you can do it Jsp Get the error exception information in the page , as follows :

If not set isErrorPage="true", So in jsp Page is not available exception Object's , Because in Servlet Of _jspService Method does not declare a exception object , As shown below :

Jsp Yes 9 Large built-in objects , And in general exception The object is Jsp Page is not available , Only settings page The directive isErrorPage The attribute is "true" To explicitly declare Jsp Page is an error handling page before it can be in Jsp Page usage exception object .

3、 ... and 、include Instructions

stay JSP There are two sentence forms for inclusion in :

  1. @include Instructions

  2. <jsp:include> Instructions

3.1、@include Instructions

@include Can contain any file , Of course , Just include the contents of the document .

include Instructions are used to introduce other JSP page , If you use include Instruction introduced other JSP page , that JSP The engine will put these two JSP Translate into a servlet. therefore include Instruction introduction is also known as static introduction .

grammar :<%@ include file="relativeURL"%>, Among them file Property is used to specify the path of the introduced file . The path to “/” start , Represents the present web application .

include Instruction details note :

  1. The introduced file must follow JSP grammar .

  2. The imported file can use any extension , Even if its extension is html,JSP The engine will follow suit jsp The way the page handles its contents , In order to see knowingly ,JSP It is recommended to use .jspf(JSP fragments( fragment )) As extension of static import file .

  3. Due to the use include The instructions will relate to 2 individual JSP page , And will 2 individual JSP Translate into a servlet, So this 2 individual JSP Page instructions cannot conflict ( except pageEncoding Except for the guide bag ).

include Instruction usage example :

newly build head.jspf Page and foot.jspf page , Respectively as jsp The head and tail of the page , Stored in WebRoot Under the jspfragments In the folder , The code is as follows :

head.jspf Code :

1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2 <h1 style="color:red;"> Page header </h1>

foot.jspf Code :

1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2 <h1 style="color:blue;"> At the end of the page </h1>

stay WebRoot Create one under the folder IncludeTagTest.jsp page , stay IncludeTagTest.jsp Page usage @include Instruction import head.jspf Page and foot.jspf page , The code is as follows :

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
3 <html>
4 <head>
5 <title>jsp Of Include Instruction test </title>
6 </head>
7
8 <body>
9 <%-- Use include Tag introduction introduces other JSP page --%>
10 <%@include file="/jspfragments/head.jspf" %>
11 <h1> Main content of web page </h1>
12 <%@include file="/jspfragments/foot.jspf" %>
13 </body>
14 </html>

The operation results are as follows :

So let's see jsp The engine will be IncludeTagTest.jsp Translate into IncludeTagTest_jsp Source code after class , find Tomcat Server's work\Catalina\localhost\JavaWeb_Jsp_Study_20140603\org\apache\jsp In the directory IncludeTagTest_jsp.java, As shown in the figure below :

open IncludeTagTest_jsp.java, The code inside is as follows :

package org.apache.jsp;

import javax.servlet.*;

import javax.servlet.http.*;

import javax.servlet.jsp.*;

import java.util.*;

import java.util.*;

import java.util.*;

public final class IncludeTagTest_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("/jspfragments/head.jspf");

_jspx_dependants.add("/jspfragments/foot.jspf");

}

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=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\">\r\n");

out.write("<html>\r\n");

out.write("  <head>\r\n");

out.write("  \r\n");

out.write("    <title>jsp Of Include Instruction test </title>\r\n");

out.write("  \r\n");

out.write("  </head>\r\n");

out.write("  \r\n");

out.write("  <body>\r\n");

out.write("    ");

out.write("\r\n");

out.write("<h1 style=\"color:red;\"> Page header </h1>\r\n");

out.write("\r\n");

out.write("    <h1> Main content of web page </h1>\r\n");

out.write("    ");

out.write("\r\n");

out.write("<h1 style=\"color:blue;\"> At the end of the page </h1>\r\n");

out.write("\r\n");

out.write("  </body>\r\n");

out.write("</html>\r\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);

}

}

}

You can see ,head.jspf and foot.jspf The content of the page uses out.write The output to the browser shows .

3.2、 summary @include Instructions

Use @include Can contain any content , It doesn't matter what the suffix is . This kind includes other file contents to its own page @include A statement is called a static include , The function is to include other page contents , It belongs to static inclusion .

3.3、jsp:include Instructions

jsp:include Instructions Include... For dynamic , If the included page is JSP, Then, the results will be included after processing , And if it includes right and wrong *.jsp file , It just statically includes the contents of the file , Function and @include similar . I'll give you more details later

JavaWeb--- summary ( sixteen )JSP More related articles on directives

  1. javaweb Learning summary ( sixteen )——JSP Instructions

    One .JSP Instruction Introduction JSP Instructions (directive) Is for JSP Designed for the engine , They don't directly produce any visible output , Just tell the engine what to do with it JSP The rest of the page . stay JSP 2.0 There are three directives defined in the specification : pa ...

  2. javaweb( sixteen )——JSP Instructions

    One .JSP Instruction Introduction JSP Instructions (directive) Is for JSP Designed for the engine , They don't directly produce any visible output , Just tell the engine what to do with it JSP The rest of the page . stay JSP 2.0 There are three directives defined in the specification : pa ...

  3. javaweb Learning summary ( sixteen )——JSP Instructions ( turn )

    One .JSP Instruction Introduction JSP Instructions (directive) Is for JSP Designed for the engine , They don't directly produce any visible output , Just tell the engine what to do with it JSP The rest of the page . stay JSP 2.0 There are three directives defined in the specification : pa ...

  4. JavaWeb Study ( 15、 ... and )————JSP Instructions

    One .JSP Instruction Introduction JSP Instructions (directive) Is for JSP Designed for the engine , They don't directly produce any visible output , Just tell the engine what to do with it JSP The rest of the page . stay JSP 2.0 There are three directives defined in the specification : pa ...

  5. JavaWeb Study ( sixteen )————JSP Nine built-in objects in

    One .JSP Operation principle Every JSP The first time the page is visited ,WEB The container will give the request to JSP engine ( That is, a Java Program ) To deal with .JSP The engine will first JSP Translate into a _jspServlet( In essence, it's also a servlet ...

  6. javaweb( hexacosa- )——jsp Simple tag library development ( Two )

    One .JspFragment Class introduction javax.servlet.jsp.tagext.JspFragment Class is in JSP2.0 As defined in , Its instance object represents JSP A paragraph in the page corresponds to JSP Grammatically correct JSP fragment , ...

  7. JavaWeb Learning Notes 6 JSP

    JSP technology JSP Full name Java Server Pages, Is a dynamic web development technology . It USES JSP Tag in HTML Insert Java Code . The label is usually <% Begin with %> end . JSP It's a kind of Java s ...

  8. JavaWeb Study ( eighteen )————JSP label

    One .JSP Label Introduction JSP Labels are also called Jsp Action(JSP action ) Elements , It is used in Jsp The business logic function is provided in the page , To avoid the JSP Write it directly from the page java Code , cause jsp Pages are hard to maintain . Two .JSP Commonly used labels ...

  9. javaweb Learning summary ( hexacosa- )——jsp Simple tag library development ( Two )

    One .JspFragment Class introduction javax.servlet.jsp.tagext.JspFragment Class is in JSP2.0 As defined in , Its instance object represents JSP A paragraph in the page corresponds to JSP Grammatically correct JSP fragment , ...

Random recommendation

  1. [ turn ]Android - File read and write operations summary

      from :http://blog.csdn.net/ztp800201/article/details/7322110 Android - File read and write operations summary classification : Android2012-03-05 ...

  2. Python anaconda links to GOMP_4.0 and throws error

    ImportError: /usr/progtools/anaconda2/bin/../lib/libgomp.so.1: version `GOMP_4.0' not found (require ...

  3. mac 10.9.4 The configuration apache

    mac 10.9.x I've brought it with me apache, It can be opened as follows : 1. start-up sudo apachectl start After starting , visit http://localhost/ You should be able to see "It wor ...

  4. c++ Ku Daquan

    1.C++ Introduction of famous databases ——C++ Standard library  2.C++ Introduction of famous databases —— Quasi standard library Boost 3.C++ Introduction of famous databases ——GUI 4.C++ Introduction of famous databases —— Network communication  5.C++ Introduction of famous databases — ...

  5. Ruby on Rails Tutorial Chapter four Rails Behind the scenes Ruby And character string

    1. Auxiliary method 2. character string Input “irb” Get into Ruby Command line development environment , The starting method of the controller is executed from the command line “rails console”. (1) String connection >>"foo"+ ...

  6. Bottom menu bar ( 3、 ... and )Fragment+FragmentTabHost Achieve imitation Sina Weibo bottom menu bar

    One . Achieve the effect diagram Two . Project engineering structure 3、 ... and . Detailed code writing 1. Lord tab Layout interface ,main_tab_layout: Double click the code and select all 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

  7. Sublime text3 Use accumulation

    1.colorpicker Choose the color of . Shortcut key ctrl+shift+C 2.jsFormat format js Shortcut key ctrl+alt+f [ { "keys": ["ctrl+ ...

  8. 【Cocos2d-X Develop learning notes 】 The first 28 period : The use of music and sound effects in the game

    This tutorial series uses cocos2d-x-2.1.4( The latest version is 3.0alpha0-pre) ,PC development environment Windows7,C++ development environment VS2010 UI Play a very important role in the game , But it's not the only thing that attracts players ...

  9. be based on 2-channel network Image similarity discrimination based on

    One . Related theories This post mainly explains 2015 year CVPR An article about image similarity calculation :<Learning to Compare Image Patches via Convolutional Neur ...

  10. centos7.4 Under the python3.6 Installation

    1. System environment :centos 7.4 Minimize installation 2. setup script yum install wget      Install the download tool wget https://www.python.org/ftp/python/ ...