Javaweb Learning Summary (19)--jsp label

Source: Internet
Author: User

Javaweb Learning Summary (19)--jsp tag One, 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

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> 2 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" > 3 

Operation Result:

  

2.2. The difference between <jsp:include> label and include directive

<jsp:include> tags are dynamically introduced, and the 2 JSP pages involved in <jsp:include> tags are translated into 2 servlets, and the contents of the 2 Servlets are merged at execution time.
The include directive is a static introduction, and the 2 JSP pages involved are translated into a servlet whose 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:

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>2 <%!3     int i=1000;4%>5 <H1 The value of I in >demo.jsp is:<%=i%>

Use the include directive and the <jsp:include> label, respectively, containing the statements, including the above demo.jsp

Example: Using @include to include content

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>2 <%!3     int i=10;4%>5 

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

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>2 

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:import

The 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

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>2 

Foot.jspf

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>2 

First use the "@include" directive to "HEAD.JSPF and foot.jspf" in clude to the Includetagtest.jsp page, as follows:

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> 2 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" > 3 

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:

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> 2 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" > 3 

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:

1     <!--let the JSPF extension also become a file that is processed by the JSP servlet. -2     <servlet-mapping> 3         <servlet-name>jsp</servlet-name> 4         <url-pattern> *.jspf</url-pattern> 5     </servlet-mapping> 6     <!--let the JSP extension be the same file that the JSP servlet handles. -7     <servlet-mapping> 8         <servlet-name>jsp</servlet-name> 9         <url-pattern> *.jsp</url-pattern>10     </servlet-mapping>

The above configuration can also be abbreviated as follows:

1     <servlet-mapping>2         <servlet-name>jsp</servlet-name>3         <url-pattern>*. Jsp</url-pattern>4         <!--let the JSPF extension also become a file that is processed by the JSP servlet. -->5         <url-pattern>*.jspf</url-pattern>6     </servlet-mapping>

The effect of both formulations is the same.

After adding such configuration information, the Tomcat server can parse and execute the *.JSPF file normally, as follows:

  

Workaround Two: Modify the Web. xml file of the Tomcat server to add a mapping to the extension *.JSPF file

Locate the Web. xml file for the Tomcat server as follows:

  

Find the servlet with the name JSP as follows:

1 <servlet> 2         <servlet-name>jsp</servlet-name> 3         <servlet-class> Org.apache.jasper.servlet.jspservlet</servlet-class> 4         <init-param> 5             <param-name>fork </param-name> 6             <param-value>false</param-value> 7         </init-param> 8         < Init-param> 9             <param-name>xpoweredby</param-name>10             <param-value>false</ Param-value>11         </init-param>12         <load-on-startup>3</load-on-startup>13 </ Servlet>

The corresponding servlet-mapping configuration is then found according to the servlet name, as follows:

1     <!--the mappings for the JSP servlet-->2     <servlet-mapping>3         <servlet-name>jsp</ Servlet-name>4         <url-pattern>*.jsp</url-pattern>5         <url-pattern>*.jspx</ Url-pattern>6     </servlet-mapping>

As you can see here, the servlet with the name JSP only supports *.jsp and *.jspx two extensions, so you can add one more <url-pattern>*.jspf</url-pattern> in this place, as follows:

1     <!--the mappings for the JSP servlet-->2     <servlet-mapping>3         <servlet-name>jsp</ Servlet-name>4         <url-pattern>*.jsp</url-pattern>5         <url-pattern>*.jspx</ Url-pattern>6         <url-pattern>*.jspf</url-pattern>7     </servlet-mapping>

After this configuration, the Tomcat server can parse and execute the *.JSPF file normally.

2.3. <jsp:forward> Label

The <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

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>2 <%--use <jsp:forward> Tags jump to forwarddemo02.jsp--%>3 <jsp:forward page= "/forwarddemo02.jsp"/>

forwarddemo02.jsp

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>2 

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> Label

When 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

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>2 

inc.jsp

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>2 

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

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>2 <%--use <jsp:forward> Tags jump to forwarddemo04.jsp--%>3 <%--use <jsp:param> label to pass parameters to forwarddemo04.jsp--%>4 <jsp:forward page= "/ forwarddemo04.jsp ">5     <jsp:param name=" ref1 "value=" Hello "/>6     <jsp:param name=" Ref2 "value=" GaCl "/>7 </jsp:forward>

forwarddemo04.jsp

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>2 

The results of the operation are as follows:

  

Javaweb Learning Summary (19)--jsp label

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.