Home > Java > javaTutorial > What is the use of jsp custom tags

What is the use of jsp custom tags

(*-*)浩
Release: 2020-09-16 10:12:42
Original
3820 people have browsed it

Custom JSP tags are usually for reuse. For example, the same function does not need to be written in the same code on different pages to facilitate the maintenance of page code; custom tags are user-defined JSP language elements that create custom The syntax for defining tags is "".

What is the use of jsp custom tags

Custom tags are user-defined JSP language elements. When a JSP page contains a custom tag that is converted into a servlet, the tag is converted into operations on an object called a tag handler, which are operations that the Web container calls when the servlet is executed. Custom JSP tags are usually for reuse. For example, the same function does not need to be written in the same code on different pages, which facilitates the maintenance of page code.

Recommended courses: java tutorial.

JSP tag extension allows you to create new tags and insert them directly into a JSP page. Simple Tag Handlers were introduced in the JSP 2.0 specification to write these custom tags.

You can inherit the SimpleTagSupport class and override the doTag() method to develop the simplest custom tag.

Create the "Hello" tag

Next, we want to create a custom tag called , the tag format is:

<span style='font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;'><ex:Hello /></span>    <br>
Copy after login

To create custom JSP tags, you must first create the Java class that handles the tags. So, let us create a HelloTag class as follows:

package com.runoob;

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;

public class HelloTag extends SimpleTagSupport {

 public void doTag() throws JspException, IOException {
   JspWriter out = getJspContext().getOut();
   out.println("Hello Custom Tag!");
 }
}
Copy after login

The following code overrides the doTag() method, which uses the getJspContext() method to obtain the current JspContext object and replace "Hello Custom Tag!" is passed to the JspWriter object.

Compile the above class and copy it to the environment variable CLASSPATH directory. Finally, create the following tag library: webapps\ROOT\WEB-INF\custom.tld.

<taglib>
 <tlib-version>1.0</tlib-version>
 <jsp-version>2.0</jsp-version>
 <short-name>Example TLD</short-name>
 <tag>
   <name>Hello</name>
   <tag-class>com.runoob.HelloTag</tag-class>
   <body-content>empty</body-content>
 </tag>
</taglib>
Copy after login

Next, we can use the Hello tag in the JSP file:

<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%><html> <head>   <title>A sample custom tag</title> </head> <body>   <ex:Hello/> </body></html>
Copy after login

The output result of the above program is:

Hello Custom Tag!
Copy after login

The above is the detailed content of What is the use of jsp custom tags. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
jsp
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template