JSP template Application Guide () _ MySQL

Source: Internet
Author: User
The execution Template discussed here will be executed under three custom tags: Template: insertTemplate: putTemplate: getinsert tag contains a Template, but before it is included, the put Tag stores some information-name, URI, and Boolean values (used to specify whether the content is included or directly displayed)-about the content contained in the template. In the execution Template
The template discussed here will be executed under three custom labels:

Template: insert

Template: put

Template: get

The insert tag contains a template, but before it is included, the put Tag stores some information-name, URI, and Boolean values (used to specify whether the content is included or directly displayed) -content contained in the template. The specified content is included (or displayed) in template: get, and the information will be accessed later.

Template: put stores the Bean in the request region (but not directly), because if the two templates use the same content name, a nested template overwrites the content in the encapsulation template.

To ensure that each template can only access its own information, template: insert retains a hashtable stack. Each insert starts a label to create a hashtable and put it into the stack. Encapsulate put labels to create beans and save them to the recently created hashtable. Then, access the bean in hashtable by The get tag in the contained template. Figure 4 shows how the stack is retained.


Figure 4. click to enlarge the parameters of the request region storage template (24 KB)

In figure 4, each template contains the footer footer.html and footer_2.html. If the bean is directly stored in the request area, step 5 in figure 4 overwrites the footer bean specified in step 2.

Template tag execution
Next, we will analyze the execution of the three template labels: insert, put, and get. Let's start with Figure 5. This chart shows the execution sequence of insert and put tag events when a template is used.


Figure 5. click to enlarge the execution sequence of put and insert tags (24 KB)

If a template stack does not exist, the insert start tag will be created and placed in the request area. Then a hashtable is created and put into the stack.

Each put start tag creates a PageParameter bean and stores it in the hashtable created by the encapsulated insert tag.

The insert end tag contains this template. This template uses the get tag to access the bean created by the put tag. After the template is processed, the hashtable created by the label starting with insert is cleared from the stack.

Figure 6 shows the sequence chart of template: get.


Figure 6. click to enlarge the get tag sequence chart (11 KB)

Template tag list
The tag handler is simple. In Example 3. a, the Insert label Class-tag handler is listed.

Example 3. a. InsertTag. java

Packagetags. templates;

Import java. util. Hashtable;

Import java. util. Stack;

Import javax. servlet. jsp. JspException;

Import javax. servlet. jsp. PageContext;

Import javax. servlet. jsp. tagext. TagSupport;

Public class InserttagextendstagSupport {

Private Stringtemplate;

Private Stack stack;

// Setter method fortemplate attribute

Public void setTemplate (Stringtemplate ){

This. template = template;

}

Public int doStartTag () throws JspException {

Stack = getStack (); // obtain a reference to thetemplate stack

Stack. push (new Hashtable (); // push new hashtable onto stack

Return EVAL_BODY_INCLUDE; // pass tagbody through unchanged

}

Public int doEndTag () throws JspException {

Try {

PageContext. include (template); // includetemplate

}

Catch (Exception ex) {// IOException or ServletException

Throw new JspException (ex. getMessage (); // recast exception

}

Stack. pop (); // pop hashtable off stack

Return EVAL_PAGE; // evaluate the rest of the page after the tag

}

// Taghandlers shoshould always implement release () because

// Handlers can be reused by the JSP container

Public void release (){

Template = null;

Stack = null;

}

Public Stack getStack (){

// Try to get stack from request scope

Stack s = (Stack) pageContext. get attribute (

"Template-stack ",

PageContext. REQUEST_SCOPE );

// If the stack's not present, create a new one and

// Put it into request scope

If (s = null ){

S = new Stack ();

PageContext. set attributes ("template-stack", s,

PageContext. REQUEST_SCOPE );

}

Return s;

}

}

Example 3. B lists the Put tag class and tag handler:

Example 3. B. PutTag. java

Packagetags. templates;

Import java. util. Hashtable;

Import java. util. Stack;

Import javax. servlet. jsp. JspException;

Import javax. servlet. jsp. tagext. TagSupport;

Import beans. templates. PageParameter;

Public class PuttagextendstagSupport {

Private String name, content, direct = "false ";

// Setter methods for Put tag attributes

Public void setName (String s) {name = s ;}

Public void setContent (String s) {content = s ;}

Public void setDirect (String s) {direct = s ;}

Public int doStartTag () throws JspException {

// Obtain a reference to enclosing insert tag

Inserttagparent = (InsertTag) getAncestor (

"Tags. templates. InsertTag ");

// Puttags must be enclosed in an insert tag

If (parent = null)

Throw new JspException ("PutTag. doStartTag ():" +

"No Inserttagancestor ");

// Gettemplate stack from insert tag

Stacktemplate_stack = parent. getStack ();

// Template stack shocould never be null

If (template_stack = null)

Throw new JspException ("PutTag: notemplate stack ");

// Peek at hashtable on the stack

Hashtable params = (Hashtable) template_stack.peek ();

// Hashtable shocould never be null either

If (params = null)

Throw new JspException ("PutTag: no hashtable ");

// Put a new PageParameter in the hashtable

Params. put (name, new PageParameter (content, direct ));

Return SKIP_BODY; // not interested in tagbody, if present

}

// Taghandlers shoshould always implement release () because

// Handlers can be reused by the JSP container

Public void release (){

Name = content = direct = null;

}

// Convenience method for finding ancestor names

// A specific class name

PrivatetagSupport getAncestor (String className)

Throws JspException {

Class klass = null; // can't name variable "class"

Try {

Klass = Class. forName (className );

}

Catch (ClassNotFoundException ex ){

Throw new JspException (ex. getMessage ());

}

Return (TagSupport) findAncestorWithClass (this, klass );

}

}

PutTag. doStarttag creates a PageParameter bean

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.