Java Development JSP custom tag

Source: Internet
Author: User
Tags tld

This article aims to correct some incorrect articles on the Internet!

Development steps:

First, create a class:

Inherit bodytagsupport

Then set the field generation Set Method

Class:

Package Org. pan. tags; import Java. io. ioexception; import Java. io. printwriter; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import javax. servlet. HTTP. httpsession; import javax. servlet. JSP. jspexception; import javax. servlet. JSP. tagext. bodytagsupport; public class sessionproperty extends bodytagsupport {Private Static final long serialversionuid = 1l; private string value; // The Public void setvalue (string value) {This. value = value;} Public String getvalue () {return value;} @ overridepublic int doendtag () throws jspexception {// todo auto-generated method stubreturn super. doendtag () ;}@ overridepublic int dostarttag () throws jspexception {system. out. println ("OK"); httpservletrequest request = (httpservletrequest) pagecontext. getrequest (); httpsession session = request. getsession (); object = session. getattribute (value); If (Object = NULL) return eval_page; If (Object instanceof string) {try {pagecontext. getout (). write (object. tostring ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} return Super. dostarttag ();}}

Note:

The output content must be:

pageContext.getOut().write(object.toString());

TLD Configuration:

Create a TLD file that needs to be placed in the WEB-INF

<?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>JSTL 1.1 core library</description><display-name>JSTL core</display-name><tlib-version>1.1</tlib-version><short-name>w</short-name><uri>http:/winter.baletu.com/</uri><tag><description>Obtained from the session attribute</description><name>property</name><tag-class>org.pan.tags.SessionProperty</tag-class><body-content>JSP</body-content><attribute><name>value</name><required>true</required><rtexprvalue>false</rtexprvalue></attribute></tag></taglib>
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.