Create your first custom JSP tag (JSP tag)

Source: Internet
Author: User
Tags html tags interface client
js| create what is JSP tag pull? If you know Html,xml, then 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 commonly used in client browsers, the role is to unify the format for displaying data. Similarly, in the JSP we also use ' < ' and ' > ', use them, in the server can do anything you want to. A subtle difference in JSP and HTML, That is, all JSP tags must follow the syntax of XML tags, that is to say, all the start tag (e.g <star:firsttag>) in the JSP must have an end tag (e.g. <star:firsttag/>).
Also note that all JSP tags have a prefix, e.g. ' star ' in <star:firsttag/> tag. Of course, like HTML and XML, JSP tags have attributes (attributes) e.g. <star: Firsttag attrib1= "value1" attrib2= "value2"/> has two attributes and is assigned two values.
So, there are ready-made labels, why do you want to customize the pull?
Benefits of customization: 1. JSP tag allows you to separate the Java (server) code from the HTML (client) code, which is important when you are developing a large project that separates the server from the client.
2.tags Java code that is easy to reuse.
3. You can package a very useful custom tag library and use it for end customers.
4.tags is easy to maintain.
Oh, you are thinking of customizing JSP tags is difficult? Just the opposite. He is a very easy thing to do. Like writing an ordinary Java class, all we have to do is write a Java class and execute a interfaces (interface) directly,----that's what we're going to do in this article, or extended to predefined Java classes, and you can overload their methods if you need to. As simple as this.
Well, you'll have to write a new text Tag Library descriptor (. TLD) file so that you can use the packaged tag.
All right, look at our Firsttag.java file. Create a new Java program and save it in a/web-inf/classes/com/stardeveloper/tag/test/file with the following code:

Package com.stardeveloper.tag.test;

Import java.io.*;
Import javax.servlet.jsp.*;
Import javax.servlet.jsp.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 to parent;
}

public void SetName (String s) {
name = S;
}

Public String GetName () {
return name;
}

public int doStartTag () throws Jspexception {
try {

if (name!= null) {
Pc.getout (). Write ("Hello" + name + "!");
} else {
Pc.getout (). Write ("You didn ' t enter your name");
Pc.getout (). Write (", what are You afraid of");
}

catch (IOException e) {
throw new Jsptagexception ("an ioexception occurred.");
}
return skip_body;
}

public int Doendtag () throws Jspexception {
return eval_page;
}

public void release () {
PC = NULL;
parent = NULL;
name = NULL;
}
Note: Package com.stardeveloper.tag.test indicates that Firsttag class is packaged and placed in Com.stardeveloper.tag.test. 2. Introduction of three packages (we use the methods and classes in them) Import java.io.*;
Import javax.servlet.jsp.*;
Import javax.servlet.jsp.tagext.* Note that these two senses interface serializable and tag, for the creation of a JSP tags class serializable is not necessary, 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.