In-depth analysis of JavaWeb Item15 -- jsp tag
I. Introduction to JSP labels
JSP tags are also called Jsp Action elements. They are used to provide business logic functions on JSP pages, avoiding the difficulty of maintaining Jsp pages by writing java code directly on JSP pages.
Ii. Common JSP labels
Common jsp labels include the following three
Tag
Tag
Tag
2.1, Tag
The label is used to insert the output content of another resource into the output content of the current JSP page. This introduction method when executing the JSP page is called dynamic introduction.
Syntax:
pageAttribute is used to specify the relative path of the introduced resource. It can also be obtained by executing an expression.
flushAttribute specifies whether to refresh the output content of the current JSP page to the client when other resources are inserted.
Example: Use the jsp: include tag to introduce Resources
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<% -- Use the jsp: include tag to introduce other JSP pages -- %> Webpage subject content
Running result:
2.2, Differences between labels and include commands
Tags are dynamically introduced, The two JSP pages involved in the tag will be translated into two Servlets, and the content of these two servlets will be merged during execution.
The include command is a static introduction. The two JSP pages involved are translated into a servlet, whose content is merged at the source file level.
The following example is used to describe Labels andincludeCommand differences
Demo. jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%! int i=1000; %>
The I value in demo. jsp is: <% = I %>
UseincludeCommands and The tag contains the preceding two statements: demo. jsp
Example: Use @ include to include content
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%! int i=10;%>
The I value in JspIncludeTagDemo01.jsp is: <% = I %> <%@include file="/jspfragments/demo.jsp"%>
At this time, an error is prompted during jsp compilation, as shown below:
This error is about variables.iAlready defined
RunJspIncludeTagDemo01.jspThe result is as follows:
Repeated variables are found after running.iError message,Static inclusion includes all the content before processing.Because the page is includeddemo.jspDefines a variableiAnd contain pagesJspIncludeTagDemo01.jspA variable is defined.iSo the server is processingJspIncludeTagDemo01.jspOn this page, we will find two variables with repeated definitions.iTherefore, an error is reported.
If you are using Observe the following programs for dynamic inclusion:
Example: Use Dynamic inclusion
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
JspIncludeTagDemo02.jsp <%! int i=10; %> The I value in JspIncludeTagDemo02.jsp is: <% = I %>
Running result:
The result is displayed correctly and does not affect each other.jsp:includeDynamic inclusion refers to processing pages separately and then including the processed results after processing.
No matter Tag, orincludeCommand, which combines the content of the two JSP pages and outputs them. Therefore, do not duplicate the HTML global architecture labels on these two pages, otherwise, the content output to the client will be a messy HTML document.
2.3,*.jspfFile Extension injsp:include,@includeAndc:importDifference in
JSP specification is recommended.jspf(JSP fragments) as the extension of the static introduced file. I accidentally discovered today that I named a JSP filejspfExtension, and thenincludeTo anotherjspFile, found only use"@include"During instruction,jspfFile content will be parsed and executedjspCommands andtagAnd use"jsp:include"And JSTL"c:import"It's useless,jspfThe file is processed as a plain text file.
For example, there ishead.jspfPage andfoot.jspfPage
Head. jspf
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
Webpage Header
Foot. jspf
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
Webpage tail
First use"@include"Command"head.jspfAndfoot.jspf"IncludeIncludeTagTest.jspPage, as shown below:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<% -- Introduce the jspf page using the include tag -- %> <% @ include file = "/jspfragments/head. jspf" %> Webpage subject content <%@include file="/jspfragments/foot.jspf" %>
RunIncludeTagTest.jspPage. The running result is as follows:
jspfThe file content will be parsed and the jsp commands and tags in the file will be executed to view browser resolutionJspIncludeTagTest.jspThe source code generated on the page is as follows:
And then use "Tag"head.jspfAndfoot.jspf"IncludeJspIncludeTagTest.jspPage, as shown below:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<% -- Use the jsp: include tag to introduce other JSPf pages -- %> Webpage subject content
RunJspIncludeTagTest.jspPage. The running result is as follows:
View browser resolutionJspIncludeTagTest.jspThe source code generated on the page is as follows:
As you can see,head.jspfAndfoot.jspfIn<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>The parsing is not executed, but the text content is output to the page intact, and cannot be seen under IE.<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>In the google and Firefox browsers, you can see the output on the page.<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>, As shown below:
This descriptionjspfThe file Tomcat server is processed as a plain text file and is not parsed and executed as a jsp page. How can this problem be solved? How to make the tomcat server parse and execute*.jspfThe java code and labels in the file have the following solutions:
Solution 1: Modify the web. xml file and add the extension*.jspfFile ing
As follows:
Jsp
*. Jspf
Jsp
*. Jsp
The preceding configuration method can also be abbreviated as follows:
Jsp
*. Jsp
*. Jspf
Both methods have the same effect.
After such configuration information is added, the tomcat server can be parsed and executed normally.*.jspfFile, as shown below:
Solution 2: Modify the web. xml file of the Tomcat server and add a ing to the file with the extension *. jspf.
Find the web. xml file of the tomcat server, as shown below:
Find the Servlet named jsp, as shown below:
jsp
org.apache.jasper.servlet.JspServlet
fork
false
xpoweredBy
false
3
Then find the correspondingservlet-mappingThe configuration is as follows:
jsp
*.jsp
*.jspx
We can see that the Servlet named jsp only supports*.jspAnd*.jspxTwo extensions, so you can add one more extension here *.jspf , As shown below:
jsp
*.jsp
*.jspx
*.jspf
After such configuration, the Tomcat server can be properly parsed and executed*.jspfFile.
2.3, Tag
Tags are used to forward requests to another resource.
Syntax:
The page attribute is used to specify the relative path of the resource to which the request is forwarded. It can also be obtained by executing an expression.
Example: Use Tag jump page
Forwarddemo01.jsp
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %> <% -- Use
Jump to forwarddemo02.jsp -- %>
Forwarddemo02.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
Page after jump !!
The running result is as follows:
From the page display effect, the page has been redirected, but the address bar has not changed, and the address displayed in the address bar is stillFZ environment? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> hybrid + NeqyvTT2rf + zvHG97bLzPjXqqGj1rvSqsrHt/7O8cb3tsvM + fingerprint =" 24jspparam tag ">2.4, Tag
When using And You can use The tag sends parameters to the resource.
Syntax 1:
Syntax 2:
The name attribute of the tag is used to specify the parameter name, and the value attribute is used to specify the parameter value. In And Multiple tags can be used Tag to pass multiple parameters.
Example: Use The tag transmits parameters to the contained page.
JspIncludeTagDemo03.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
JspIncludeTagDemo03.jsp
Inc. jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
Receive the parameters passed from the JspIncludeTagDemo03.jsp page:<%=request.getParameter("parm1")%><%=request.getParameter("parm2")%>
InJspIncludeTagDemo03.jspUse on the page TagInc.jspPage included, use Tag OrientationInc.jspTwo parameters are passed on the page.parm1Andparm2
The JspIncludeTagDemo03.jsp page runs as follows:
Example: Use The tag transmits parameters to the page to jump.
Forwarddemo03.jsp
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %> <% -- Use
Tag jump to forwarddemo04.jsp -- %> <% -- Use
The tag passes a parameter to forwarddemo04.jsp -- %>
Forwarddemo04.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
Page after jump !!Receive the parameters passed from forwarddemo03.jsp:ref1:<%=request.getParameter("ref1")%>ref2:<%=request.getParameter("ref2")%>
The running result is as follows: