SoFunction
Updated on 2025-04-05

Example analysis of the usage of JSP C tags

This article describes the usage of JSP's C tag. Share it for your reference, as follows:

Core Tag Library

It is a core library in JSTL that provides common support for daily tasks such as displaying and setting variables, reusing a set of projects, testing conditions, and other operations (such as importing and redirecting web content). Core tags can be divided into 4 types according to their functions:

1 Variable maintenance:

(1) <c:set>: Set variable values ​​and object properties. The syntax is as follows:

Copy the codeThe code is as follows:
<c:set value="value" var="variable name" scope="variable scope" target="object name" property="object property name"></c:set>

There are two ways to set each. To sum up, there are 4 forms of <c:set>, as shown below:

a. Set JSP variables using tag attributes

Copy the codeThe code is as follows:
<c:set value="value" var="variable name" scope="scope"/>

b. Setting JSP variables with marker

Copy the codeThe code is as follows:
<c:set var="variable name" scope="scope">tagged content</c:set>

c. Set object properties using tag properties

Copy the codeThe code is as follows:
<c:set value="variable name" target="Object name" property="Object property name"/>

d. Set object properties using marker

Copy the codeThe code is as follows:
<c:set target="Object Name" property="Scope">Tag content</set>

(2) <c:remove>: Delete variables within the specified scope. The syntax is as follows:

Copy the codeThe code is as follows:
<c:remove var="variable name" scope="scope"/>

2 Process control: divided into conditional labels and iterative labels.

Conditional tag: <c:if> <c:choose> <c:when> <c:otherwise>

(1) <c:if>: The same method as the use of if statements in Java language, but the function of else cannot be implemented.

The <c:if> tag has two syntax forms, which are distinguished by whether there is or not.
No tag body:

Copy the codeThe code is as follows:
<c:if test="test condition" var="variable name" [scope="scope"]/>

Tag body:
&lt;c:if test="Testing Conditions" var="Variable Name" [scope="Scope"]&gt;
  Tag body
&lt;/c:if&gt;

<c:if> with marker

Copy the codeThe code is as follows:
<c:if test="${!=0}">Welcome</c:if>

(2)<c:choose> <c:when> <c:otherwise>
<c:when> <c:otherwise> cannot be used alone, but can only be used as a child tag of <c:choose>. These three tags are combined to realize the function of switch statements in Java. The syntax is as follows:

  &lt;c:choose&gt;
  &lt;c:when test="${=='guest'}"&gt;
    Tag body1
  &lt;/c:when&gt;
  &lt;c:when test="${=='vip'}"&gt;
    Tag body2
  &lt;/c:when&gt;  
  &lt;c:otherwise&gt;
    Tag body3
  &lt;/c:otherwise&gt; 
  &lt;/c:choose&gt;

Iteration tag: <c:forEach> <c:forTokens>

(1)<c:forEach>: used to traverse an object collection.

&lt;c:forEach var="Variable Name" items="gather" varStatus="Traveling state name"
    begin="begin" end="end" step="step" &gt;
  Tag body
&lt;/c:forEach&gt;

(2) <c:forTokens>: is used to traverse a string, and each traversal result returns a word in the string.

&lt;c:forTokens items="String" delims="Districting symbol" var="Variable Name"
 varStatus="Traveling state name" begin="begin" end="end" step="sep"&gt;
  Tag body
&lt;/c:forTokens&gt;

3 URL management

(1) <c:url>: used to encode URL addresses.

Tag body:

&lt;c:url value="URL" context="path" var="Variable Name" scope="Scope"&gt;
   Tag body
&lt;/c:url&gt;

The following code:

&lt;c:url value="http://localhost:8080/el/" var="NewURL"&gt;
   &lt;c:param name="name" value="zero"/&gt;
   &lt;c:param name="age" value="28"/&gt;
&lt;/c:url&gt;
&lt;a href="${NewURL}"&gt;Click me&lt;/a&gt;

The generated URL: http://localhost:8080/el/?name=zero&age=28

Tagless body: mainly used to edit context URLs.

Copy the codeThe code is as follows:
<c:url value="URL" context="path" var="variable name" scope="scope"/>

The following code:

Copy the codeThe code is as follows:
<c:url value="/">Login</c:url>

If the current path is el, the output is: /el/

(2) <c:import>: Introduce URL resources (can be resources on remote program sites) to the current JSP page. The Include directive and include actions cannot introduce resources other than the web program to the JSP page, and the imported resources must be located in the current web program.

Syntax introduced with String objects:

&lt;c:import url="address" context="Context Path" var="Variable Name"
  scope="Scope" charEncoding="Character Set"&gt;
    Tag body use&lt;c:param&gt;
&lt;/c:import&gt;

The following code: Introduce external resources into the current JSP page.

&lt;c:import url="http://" var="myurl" charEncoding="gb2312"&gt;
&lt;/c:import&gt;
&lt;a href="${myurl }"&gt;address&lt;/a&gt;

Syntax for importing as Reader object:

&lt;c:import url="address" context="Context Path" varReader="Variable Name"
  scope="Scope" charEncoding="Character Set"&gt;
    Use other action elements in the tag body
&lt;/c:import&gt;

(3)<c:redirect>: used for HTTP redirection.

No tag body:

Copy the codeThe code is as follows:
<c:redirect url="address" context="context path"/>

Tag body:

&lt;c:redirect url="address" context="Context Path"&gt;
&lt;c:param/&gt;Label
&lt;/c:redirect&gt;

(4) <c:param>: can only be embedded in <c:url>, <c:import>, and <c:redirect> tags as child elements. This tag is mainly used to set the parameters to be passed in the URL.

No tag body:

Copy the codeThe code is as follows:
<c:param name="name" value="value"/>

Tag body:

&lt;c:param name="name" value="value" &gt;
Tag body
&lt;/c:param&gt;

4 Other tags: <c:out>, <c:catch>.

(1) <c:out>: Display variable content in JSP page.

No tag body:

Copy the codeThe code is as follows:
<c:out value="value" escapeXml="{true|false}" default="default"/>

Tag body:

&lt;c:out value="value" escapeXml="{true|false}" default="default value"&gt;
Tag body
&lt;/c:out&gt;

in:

default: Used to specify the value that should be output when the value is null.
escapeXml: Used to set whether to escape the characters "<", ">", "&", "'", "", "", and these characters.
escapeXml defaults to true, indicating that the conversion has occurred.
"<"Convert to "&lt"
">"Convert to "&gt"
Convert "&" to "&amp"
"'"Convert to "&#039"
"""Convert to "&#034"
(2) <c:catch>: used to handle JSP page errors.

If a JSP page error occurs, you can turn it on the error handling page by setting the page directive properties. The <c:catch> tag is a supplement to this error handling. Its handling method is to embed JSP code snippets that may occur in the marker body, and then use the var attribute to receive exceptions thrown by the marker body.

&lt;c:catch var="Variable Name"&gt;
   Nested Actions
&lt;/c:catch&gt;

How to use:

&lt;c:catch var="myexception"&gt;
   Nested Actions
&lt;/c:catch&gt;
&lt;c:if test="${myexception!=null}"&gt;
  content
&lt;/c:if&gt;

The final discussion on version issues

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="/xml/ns/j2ee"
xmlns:xsi="http:///2001/XMLSchema-instance"
xsi:schemaLocation="/xml/ns/j2ee version="2.4">

The 2.4 version needs to be and, and you also need to pay attention to version issues. Different versions cannot be compatible. ,
A JSP exception occurs when the application is deployed and runs, which occurs when using the JSTL library: According to TLD or attribute directive in tag file, attribute value does not accept any expressions, which may be because the JSP2.0 version is used and the alternative version (RT library) of the JSTL core library is not used. There are two ways to deal with it:

1. If you do not want to use web-app_2_4.xsd and jstl1.1, you can modify it in the following two ways

1). Modify.

Copy the codeThe code is as follows:
<web-app xmlns="/xml/ns/j2ee" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/xml/ns/j2ee ; version="2.4">

Change to version 2.3

Copy the codeThe code is as follows:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "/dtd/web-app_2_3.dtd">
<web-app>

2). Use JSTL core RT library

There are two taglib directives in the JSTL core library, where the RT library depends on the traditional JSP request attribute values, rather than on EL to implement it (called the EL library. JSP2.0 will support EL)

Use <%@ taglib uri=/jstl/core prefix="c"%> in JSP is OK in version 2.3, but not in 2.4. This is caused by version incompatibility. The version of servlet will be introduced later.

Just put

Copy the codeThe code is as follows:
<%@ taglib uri="/jstl/core" prefix="c"%>

Change to

Copy the codeThe code is as follows:
<%@ taglib uri=/jstl/core_rt prefix="c"%>

2: If you want to use jstl1.1 (recommended), follow the modification, it is very simple.

There are differences between 1.0 and 1.1 by jstl. It is recommended that you use EL to be in the 1.1 version.
Using jstl1.1 only requires
1.0 is

Copy the codeThe code is as follows:
<%@ taglib uri="/jstl/core" prefix="c" %>

Change to:
Copy the codeThe code is as follows:
<%@ taglib uri="/jsp/jstl/core" prefix="c" %>

I hope this article will be helpful to everyone's JSP programming.