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>