SoFunction
Updated on 2025-04-14

JSP Custom Tags Page 3/3


13. How to load static text when web application starts:

1. Create a subclass that inherits the HttpServlet class, and set the load-on-startup property when configuring this Servlet:

someclass

somepackage.SomeClass1

2. Create a class in the init() method of this Servlet

3. Get the ServletContext object of the current web application

4. Read the properties file in the WEB-INF directory into the input stream InputStream:

InputStream in = ("WEB-INF/");

5. Load the input stream into the property object

(in);

6. Save the attribute object to

("attributeName",ps);

14. How to create a tag processing class:

1. Introduce the required resources:

import .*;

import .*;

import .*;

import .*;

2. Inherit the TagSupport class and override the doStartTag()/doEndTag() method

3. Get the object from the ServletContext object

4. Get the property value corresponding to the key from the Properties object

5. Process the obtained attributes accordingly and output the results

15. Create a Tag Library Descriptor:

1. The tag library description file, referred to as TLD, uses XML file format to define the user's tag library. Elements in TLD files can be divided into 3 categories:

A.: Tag library elements

B.: Label elements

C.: Label attribute elements

2. The tag library element is used to set the relevant information of the tag library. Its common attributes are:

: Specify the default prefix name of Tag Library (prefix)

: Set the Tag Library's unique access indicator

3. The tag element is used to define a tag. Its common attributes are:

: Set the name of the tag

: Set the Tag processing class

: Set the body content of the tag

1).empty: means there is no body in the tag

2).JSP: JSP program code can be added to the body of the tag

3).tagdependent: means that the content in the tag is processed by the tag itself

4. The tag attribute element is used to define the attributes of the tag. Its common attributes are:

: Attribute name

: Whether the attribute is required, default to false

: Whether the attribute value can be a request-time expression, that is, an expression similar to < %=…% >

16. Use tags in web applications:

1. If a custom JSP tag is used in a web application, an element must be added to the file, which is used to declare the tag library where the referenced tag is located.

/sometaglib

/WEB-INF/

2.: Set the unique identifier of the Tag Library, and the Tag Library will be referenced in the web application.

3.: Specify the location of the TLD file corresponding to the Tag Library

4. The <% @ taglib% > directive needs to be added to the JSP file to declare a reference to the tag library. For example:

<% @ taglib prefix = “somePrefix” uri = "/someuri" %>

It represents the prefix when referring to the tag library's tag in a JSP web page. URI is used to specify the identifier of the Tag Library, which must be consistent with the properties in it.

Previous page123Read the full text