Java Development jsp custom tag

Source: Internet
Author: User

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:


[Java] 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 attribute to be obtained.

Public void setValue (String value ){
This. value = value;
}

Public String getValue (){
Return value;
}
 
@ Override
Public int doEndTag () throws JspException {
// TODO Auto-generated method stub
Return super. doEndTag ();
}
 
@ Override
Public int doStartTag () throws JspException {

System. out. println ("OK ");
HttpServletRequest request = (HttpServletRequest) pageContext. getRequest ();
HttpSession session = request. getSession ();

Object 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 block
E. printStackTrace ();
}
}

Return super. doStartTag ();
}
 
}

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 attribute to be obtained.
 
Public void setValue (String value ){
This. value = value;
}
 
Public String getValue (){
Return value;
}

@ Override
Public int doEndTag () throws JspException {
// TODO Auto-generated method stub
Return super. doEndTag ();
}

@ Override
Public int doStartTag () throws JspException {
 
System. out. println ("OK ");
HttpServletRequest request = (HttpServletRequest) pageContext. getRequest ();
HttpSession session = request. getSession ();

Object 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 block
E. printStackTrace ();
}
}

Return super. doStartTag ();
}

}
Note:

The output content must be:

[Java] pageContext. getOut (). write (object. toString ());

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


Tld Configuration:

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

 

 

[Html] <? 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.