JSP Learning Notes

Source: Internet
Author: User
Tags add define connect odbc return string version 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 on 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 Validation Document

<?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>

Label Library version

<tlibversion>1.0</tlibversion>

JSP version of the tag library dependency

<jspversion>1.1</jspversion>

Tag Library Name

<shortname>ATMjsp</shortname>

For more information about the tag library

<info>a Tag library</info>

<tag>

Define the name of the label

<name>ATM</name>

Indicates that the label handler class

<tagclass>ATMTag</tagclass>

Definition of this label body

<body-content>JSP</body-content>

Additional information about this label and its functionality

<info>outputs of the ATM counter</info>

</tag>

</taglib>

3. Error page errorpage.jsp code:

<body>

<%@ page iserrorpage= "true"%>

<%=exception.getmessage ()%>

</body>

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" >

<table>

<tr><td></td><td><ATMjsp:ATM/></td></tr>

</table>



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.