Create your first custom jsp tag (jsp tag)

Source: Internet
Author: User

What is jsp (the preferred choice for SUN Enterprise Applications) tag pulling? If you know html and xml (standardization is getting closer and closer), you should know the tag. any tag-based language (such as html) must be between <and>. <titli> is a title tag. These html tags (tags) are usually used in Client browsers to display data in a unified format. similarly, we also use <and> in jsp (the preferred choice for SUN enterprise-level applications) and use them to do anything you want on the server. A subtle difference between jsp (preferred for SUN Enterprise applications) and html is that all jsp (preferred for SUN Enterprise Applications) labels must follow xml (standardization is getting closer and closer) tag syntax, that is, all the start tags (e.g. <star: firsttag>) must have an ending tag (e.g. <star: firsttag/> ).
Note that all jsp (preferred for SUN Enterprise Applications) Labels have a prefix, e.g. star in <star: firsttag/> tag. of course, just like html and xml (standardization is getting closer and closer), jsp (preferred for SUN Enterprise Applications) labels also have attributes (attributes) e.g. <star: firsttag attrib1 = "value1" attrib2 = "value2"/> two attributes are assigned with two values.
So there is no need to put existing labels. Why do we need to customize the pull?
Custom benefits: 1. jsp (the preferred choice for SUN Enterprise Applications) tag allows you to separate java (server) code from html (client) Code. This is very important when you are developing a large project, separate the server and client.
2. java code that can be easily reused by tags.
3. You can package a very useful custom tag library for end users.
4. tags is easy to maintain.
Oh, are you thinking about customizing jsp (the preferred choice for SUN Enterprise Applications) tags? The opposite is true. he is an easy task. just like writing a common java class, all we need to do is write a java class and then execute an interfaces (Interface) directly. ---- This is what we have to do in this article, or extended to a pre-defined java class, you can reload their methods if needed. it's as simple as this.
Then, you will have to write a new text Tag Library Descriptor (. TLD) file so that the packaged tag can be used.
Let's take a look at our FirstTag. java file. Create a New java program and save it in the/WEB-INF/classes/com/stardeveloper/tag/test/file. The Code is as follows:

Package com. stardeveloper. tag. test; import java. io. *; import javax. servlet. jsp (the first choice for SUN Enterprise applications ). *; import javax. servlet. jsp (the first choice for SUN Enterprise applications ). tagext. *; public class FirstTag implements Tag, Serializable {private PageContext pc = null; private Tag parent = null; private String name = null; public void setPageContext (PageContext p) {pc = p;} public void setParent (Tag t) {parent = t;} public Tag getParent () {return par Ent;} public void setName (String s) {name = s;} public String getName () {return name;} public int doStartTag () throws jsp (preferred for SUN Enterprise applications) exception {try {if (name! = Null) {pc. getOut (). write ("Hello" + name + "! ");} Else {pc. getOut (). write (" You didnt enter your name "); pc. getOut (). write (", what are you afraid? ") ;}} Catch (IOException e) {throw new jsp (preferred for SUN Enterprise Applications) TagException (" An IOException occurred. ");} return SKIP_BODY;} public int doEndTag () throws jsp (preferred for SUN Enterprise Applications) Exception {return EVAL_PAGE;} public void release () {pc = null; parent = null; name = null ;}}
Note: package com. stardeveloper. tag. test; indicates that the FirstTag class is packaged and stored in com. stardeveloper. tag. test.
2. introduce three packages (we use their methods and classes)
Import java. io. *; import javax. servlet. jsp (the first choice for SUN Enterprise applications ). *; import javax. servlet. jsp (the first choice for SUN Enterprise applications ). tagext. *; Note the Serializable and Tag interfaces,
For creating a jsp (preferred for SUN Enterprise Applications) tags class, Serializable is not a must, and Tag is the most important

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.