JAVA-WEB-based jsp tag Creation

Source: Internet
Author: User
Tags ip number

Create a simple jsp tag today !! Get your own IP Address
First, create a class that implements the Tag interface and inherit the TagSupport class.
Then rewrite the doStartTag method in it, and write here to get your own id;
Because the request and out methods must be used
HttpServletRequest request = (HttpServletRequest) pageContext. getRequest (); // get it through pageContext
JspWriter out = pageContext. getOut ();
String id = request. getRemoteAddr (); // obtain your own ip number;
Out. print (ip); // here or there is an exception, you can handle it.
 
In this way, this class is complete. Next, create a tld file to describe the tag processing class.
(Put in the WEB-INF folder)
(C: \ apache-tomcat-6.0.18 \ webapps \ examples \ WEB-INF \ jsp2)
A template is required for writing a file, which needs to be searched in the preceding address,
<? Xml version = "1.0" encoding = "UTF-8"?>
<Taglib xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
Version = "2.0">
<Description> A tag library exercising SimpleTag handlers. </description> // describes the file.
<Tlib-version> 1.0 </tlib-version> // version
<Short-name> hbsi </short-name> // The prefix value of the jsp tag.
<Uri> http://www.software8.co </uri> // The uri value of the jsp tag
<Tag>
<Name> viewIP </name> // name the tag implementation class and wait for the call.
<Tag-class> com. hbsi. web. tag. ViewIPTag </tag-class> // location of the tag implementation class
<Body-content> empty </body-content> // The content of <viewIp> is empty </viewIp>
</Tag>
</Taglib> www.2cto.com
This allows you to write a jsp file.
First, write the command to write the tag created by yourself.
<% Taglib uri = "http://www.software8.co" prefix = "hbsi" %>
<Body>
Your IP address is: <viewIP/>
</Body>

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.