JSP Learning Notes (ix)-----JSP Tag Library

Source: Internet
Author: User
Tags add define connect odbc return string tld
js| notes

The 1.JSP Tag library (also called a custom library) can be viewed as a set of methods that generate xml-based scripts, which are supported by JavaBeans. Conceptually, a tag library is a very simple and reusable code construct. It can greatly simplify the code you enter into various entry boxes in the page (such as code: <input type= "text" name= "taxpayername" value = ", <select name=" Collectionitemcode " Class= "Required" style= "width:200" ... ) and make it structured.
2. The tag library (tag libraries) provides a simple way to build reusable blocks of code. But unlike COM and Java EE, you don't have to have any extra skills to build a tag library: If you write a JSP page, you create a tag library. Finally, the tag library also improves the maintainability of Web applications. This improvement in maintainability is demonstrated by the ease with which xml-based customizable interfaces can be implemented on JSP pages.
3. A simple label is composed of the following elements:
⑴javabeans: To get Java and raw object-oriented benefits, reusable code should be placed in a separate code container. These JavaBeans are not part of the tag library. But it's the basic block of code your code base uses to perform related tasks.
⑵ Label Processing: This is the true core of the tag library. A label processor will refer to whatever resources it needs (your JavaBeans) and access to all the information (PageContext objects) of your JSP page. The JSP page also transmits all the label properties that have been set and the contents of the label body on the JSP page to the label processor. After the label processor has finished processing, it will be sent back to your JSP page for processing.
Description of the ⑶ tag library (TLD file): This is a simple XML file that records the properties, information, and location of the label processor. The JSP container uses this file to learn where and how to invoke a tag library.
Web.xml file for ⑷ website: This is the initialization file for your site, in which you define the custom tags used in the site and which TLD file describes each custom label.
⑸ distribution file (a war or jar file): If you want to reuse a custom label, you need a way to transfer it from one project to another. Packaging a tag library as a jar file is a simple and effective way to do this.
⑹ in your JSP file as a tag library statement: Very simple, if you want to use the tag, as long as the page declaration can be, then, you can use it anywhere on the JSP page.
Here is an example
1. First write the label processing program, Atmtag.java
Use tag handlers to define the work of customer tags
Import javax.servlet.jsp.*;
Driver for import Tag class
Import javax.servlet.jsp.tagext.*;
Import java.io.*;
Import java.sql.*;
Import java.math.*;
Import java.util.*;
Implementing TagSupport or Bodytagsupport interfaces
public class Atmtag extends TagSupport
{
Connection connect = null;
Statement state = null;
ResultSet result = null;
Public Atmtag () throws ClassNotFoundException
{
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
}
The method of the abstract class tag, the initialization of the tag handler
public int doStartTag () throws Jsptagexception
{
Instructs the JSP engine to process the body contents of the label
return eval_body_include;
}
Perform a label task
public int Doendtag () throws Jsptagexception
{
Try
{
Write the output to the JSP page
JspWriter out=pagecontext.getout ();
Connect=drivermanager.getconnection ("Jdbc:odbc:test", "sa", "");

String strquery = "Select ccounter_id, caddress,mcashbalance from Counter where mcashbalance<=10000;";
System.out.println ("Query:" +strquery);
State = Connect.createstatement ();
ResultSet result = State.executequery (strquery);
String Sid=null;
String Saddress=null;
String Sbalance=null;
Vector v=new vector ();
while (Result.next ())
{
SId = result.getstring (1);
Saddress = result.getstring (2);
Sbalance = result.getstring (3);
V.add (SID);
V.add (saddress);
V.add (sbalance);
V.add (";");
}
for (int i=0;i<v.size (); i++)
{
String str= (String) v.elementat (i);
Pagecontext.getout (). write (str);
}
}catch (Exception IoE)
{
System.out.println ("Error in Getting Results" +ioe.tostring ());
}
Indicates that the rest of the JSP page is evaluated
return eval_page;
}
}

2. Code to write TLD files:
//XML验证文档
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
//标签库版本
<tlibversion>1.0</tlibversion>
//标签库依赖的JSP版本
<jspversion>1.1</jspversion>
//标签库名
<shortname>ATMjsp</shortname>
//关于标签库的详细信息
<info>A Tag library</info>
<tag>
//定义标签的名
<name>ATM</name>
//指出标签处理程序类
<tagclass>ATMTag</tagclass>
//此标签体的定义
<body-content>JSP</body-content>
//关于此标签与其功能的附加信息
<info>Outputs of the ATM counter</info>
</tag>
</taglib>

3. Error page errorpage.jsp code:

<html>
<body>
<%@ page isErrorPage="true" %>
<%=exception.getMessage() %>
</body>
</html>

4. Writing JSP files: atm.jsp
<%@ page import="ATMTag" %>
<%@ taglib uri="ATMjsp.tld" prefix="ATMjsp" %>
<%@ page errorPage="errorpage.jsp" %>
<!DocType HTML Public "-//W3C//DTD HTML 4.0 Translational//EN ">
<html>
<h3 align=center>ATM counters that needs Cash</h3>
<table>
<tr><td></td><td><ATMjsp:ATM/></td></tr>
</table>
</html>




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.