First, the JSP tag introduction
JSP tags, also known as JSP Action (JSP actions) element, it is used to provide business logic function in JSP page, avoid writing Java code directly in JSP page, which makes JSP page difficult to maintain.
Second, JSP common tags
There are three common tags for JSP:
- <jsp:include> tags
- <jsp:forward> tags
- <jsp:param> tags
2.1. <jsp:include> Label
The <jsp:include> tag is used to insert the output of another resource into the output of the current JSP page, which is called dynamic Introduction when the JSP page executes.
Grammar:
<jsp:include page= "relativeurl | <%=expression%>" flush= "True|false"/>
The page property is used to specify the relative path to the resource being introduced, and it can also be obtained by executing an expression.
The Flush property specifies whether the output of the current JSP page is flushed to the client first when the output of the other resource is inserted.
Example: using jsp:include tags to introduce resources
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Operation Result:
2.2<jsp:include> the difference between a label and an include directive
<jsp:include> tags are dynamically introduced,<jsp:include> tags involving 2 jsp pages will be translated into 2 Serlvet, the Serlvet content is merged at execution time. The include directive is a static introduction, and the 2 JSP pages involved are translated into a serlvet, where the content is merged at the source file level.
Use the following example to illustrate the difference between a <jsp:include> tag and an include directive
DEMO.JSP:
Import= "java.util.*" pageencoding= "UTF-8"%> <%! int i=1000%>
Use the include directive and the <jsp:include> label, respectively, containing the statements, including the above demo.jsp
Example: Using @include to include content
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%! int i=10; The value of I in%>
At this point, the JSP was compiled with an error, as follows:
The error is that the variable i has been repeatedly defined
Run jspincludetagdemo01.jsp with the following results:
After running, it is found that there is a duplicate definition of variable i error message, because the static contains the entire content is included in, and then processed, belongs to the first containing post-processing. because the included page demo.jsp defines a variable i, and contains the page jspincludetagdemo01.jsp itself defines a variable i, so the server in the process of jspincludetagdemo01.jsp this page will find that there are two duplicate fixed Variable i, so it will be an error.
And if you are using <jsp:include> dynamic inclusion, observe the following procedure:
Example: Using dynamic inclusion
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
Operation Result:
The findings are displayed correctly and do not affect each other because the use of jsp:include belongs to dynamic inclusion, which means that the individual pages are processed first, and then the processed results are included in the process.
Whether it's a <jsp:include> tag or an include directive, they all merge two JSP page contents, so the two pages don't have duplicate HTML global schema tags, otherwise the content output to the client will be a cluttered HTML document.
2.3.differences between *.JSPF extension files in Jsp:include, @include, and C:importThe JSP specification recommends using. JSPF (JSP fragments) as the extension of the static ingest file. Today, inadvertently found that a JSP file named JSPF Extension, and then include into another JSP file, found that only with "@include" instruction, the contents of the JSPF file will be parsed and executed in the JSP instructions and tag, and use "JSP: Include "and Jstl" C:import "are useless, JSPF file is treated as a plain text file.
For example, there is now a HEAD.JSPF page and a FOOT.JSPF page
Head.jspf
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
Foot.jspf
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
First use the "@include" directive to "HEAD.JSPF and foot.jspf" in clude to the Includetagtest.jsp page, as follows:
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Run the includetagtest.jsp page and run the following results:
The contents of the JSPF file are parsed and executed with JSP directives and tags, and the browser resolves the source code generated by the Jspincludetagtest.jsp page, as shown here:
Then use the <jsp:include> tab to "HEAD.JSPF and foot.jspf" Include to the jspincludetagtest.jsp page as follows:
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Run the jspincludetagtest.jsp page and run the following results:
View the source code generated by the browser parsing jspincludetagtest.jsp page as follows:
As you can see, <%@ inhead.jspf and FOOT.JSPF page language= "java" import= "java.util.*" pageencoding= "UTF-8"% > does not parse execution, but is output to the page intact as text content, under IE can't see <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> Output, run under Google and Mozilla Firefox can see the output <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>, as follows:
This means that the JSPF file Tomcat server is treated as a plain text file, not as a JSP page to parse the execution, then how to solve this problem? How to enable the Tomcat server to parse the Java code and tags in the *.JSPF file, as in several workarounds:
Workaround One: Modify the Web. xml file to add a mapping to the extension *.JSPF file
As shown below:
<!--let the JSPF extension also become a file that is processed by the JSP servlet. -<servlet-mapping> <servlet-name>jsp</servlet-name> <url-pattern>*. jspf</url-pattern> </servlet-mapping> <!--let the JSP extension be the same file that the JSP servlet handles. -<servlet-mapping> <servlet-name>jsp</servlet-name> <url-pattern>*. Jsp</url-pattern> </servlet-mapping>
The above configuration can also be abbreviated as follows:
<servlet-mapping><servlet-name>jsp</servlet-name><url-pattern>*.jsp</url-pattern ><!--Let the JSPF extension also become a file that is processed by the JSP servlet. --><url-pattern>*.jspf</url-pattern></servlet-mapping>
2.3. <jsp:forward> LabelThe <jsp:forward> tag is used to forward requests to another resource.
Grammar:
<jsp:forward page= "relativeurl | <%=expression%>"/>
The page property is used to specify the relative path of the resource to which the request is forwarded, and it can also be obtained by executing an expression.
Example: Using the <jsp:forward> tag jump page
forwarddemo01.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%--use <jsp:forward> Tags jump to Forwarddemo02.jsp--%><jsp:forward page= "/forwarddemo02.jsp"/>
forwarddemo02.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
The results of the operation are as follows:
From the page display effect, the page has completed the jump, but the address bar has not changed, the address bar is displayed in the address is forwarddemo01.jsp, but the content of the page is forwardemo02.jsp content. Because this jump belongs to the server-side jump. As long as the server-side jump, the address bar has never changed.
2.4. <jsp:param> LabelWhen using <jsp:include> and <jsp:forward> tags to introduce or forward requests to other resources, you can use the <jsp:param> tag to pass parameters to this resource.
Syntax 1:
<jsp:include page= "relativeurl | <%=expression%>" >
<jsp:param name= "parametername" value= "parametervalue|<%= expression%>"/>
</jsp:include>
Syntax 2:
<jsp:forward page= "relativeurl | <%=expression%>" >
<jsp:param name= "parametername" value= "parametervalue|<%= expression%>"/>
</jsp:include>
The Name property of the <jsp:param> tag is used to specify the parameter name, and the Value property is used to specify the parameter value. Multiple <jsp:param> tags can be used to pass multiple parameters in <jsp:include> and <jsp:forward> tags.
Example: Using <jsp:param> tags to pass parameters to a contained page
jspincludetagdemo03.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
inc.jsp
Import= "java.util.*" pageencoding= "UTF-8"%>
Use the <jsp:include> tab in the jspincludetagdemo03.jsp page to include the inc.jsp page, using <jsp:param/> The label passed two parameters to the inc.jsp page Parm1 and Parm2
The results of the jspincludetagdemo03.jsp page run as follows:
Example: Using <jsp:param> tags to pass parameters to a page to jump
forwarddemo03.jsp
Import= "java.util.*" pageencoding= "UTF-8"%><%--use <jsp:forward> tag to jump to forwarddemo04.jsp--% ><%--using <jsp:param> tags to pass parameters to forwarddemo04.jsp--%><jsp:forward page= "/forwarddemo04.jsp" > <jsp:param name= "ref1" value= "Hello"/> <jsp:param name= "Ref2" value= "GaCl"/></jsp:forward>
forwarddemo04.jsp
Import= "java.util.*" pageencoding= "UTF-8"%>
The results of the operation are as follows:
Tags for JSP