Java (J2EE) applications-the entire process of simple custom tag development-one (inber)-Java sleepless nights

Source: Internet
Author: User
Tags tld

Tag application:
Data Source
Myperformance2
User sa
Password (null)
Database pubs table: employee

Description: windows ODBC --- SQL database pubs table: employee

Function: retrieve the data record of job_lvl> = 100 and call it wherever you want to call it on the JSP page.

1: Tag handler: EmployeeTag. java

Package net. inber;

Import javax. servlet. jsp .*;
Import javax. servlet. jsp. tagext .*;

Import java. util. Hashtable;
Import java. io. Writer;
Import java. io. IOException;
Import java. util. Date;

Import java. SQL .*;
Import java. math .*;
Import java. util .*;

Public class EmployeeTag extends TagSupport
{

Connection con = null;
Statement state = null;
Resultset rs = NULL;


Public employeetag () throws classnotfoundexception
{
Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");
}

Public int dostarttag () throws jsptagexception {

Return eval_body_include;
}

Public int doendtag () throws jsptagexception
{
String datestring = new date (). tostring ();
Try
{
Pagecontext. getout (). Write ("");

Try {
JspWriter out = pageContext. getOut ();
Con = DriverManager. getConnection ("jdbc: odbc: myperformance2", "sa ","");
String strQuery = "select emp_id, fname, lname, job_lvl, hire_date from employee where

Job_lvl & gt; = 100 ";
State = con. createStatement ();
Rs = state.exe cuteQuery (strQuery );
String emp_id = null;
String fname = null;
String lname = null;
String job_lvl = null;
String hire_date = null;

Out. Write ("<Table width = '000000' Height = '22' border = '1'> ");
While (Rs. Next ())
{
Emp_id = Rs. getstring (1 );
Fname = Rs. getstring (2 );
Lname = Rs. getstring (3 );
Job_lvl = Rs. getstring (4 );
Hire_date = Rs. getstring (5 );
Out. Write ("<tr> ");
Out. Write ("<TD width = '000000'>" + emp_id + "</TD> ");
Out. Write ("<TD width = '000000'>" + fname + "</TD> ");
Out. Write ("<TD width = '000000'>" + lname + "</TD> ");
Out. Write ("<TD width = '9% '>" + job_lvl + "</TD> ");
Out. Write ("<TD width = '000000'>" + hire_date + "</TD> ");
Out. Write ("</tr> ");
}
Out. write ("</table> ");

}
Catch (Exception e)
{
// Throw new JspTagException ("Fatal error: connect error! ");
}


}
Catch (IOException ex)
{
Throw new JspTagException ("Fatal error: hello tag conld not write

JSP out ");
}
Return EVAL_PAGE;
// Return SKIP_PAGE;
}
}
2. TLD file EmployeeTag. tld
<? Xml version = "1.0" encoding = "ISO-8859-1"?>

<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 web-jsptaglibrary_2_0.xsd"
Version = "2.0">

<Description> A tag library exercising SimpleTag handlers. </description>
<Tlib-version> 1.0 </tlib-version>
<Description>
A simple tab library for the examples
</Description>

<Tag>
<Description> select db pubs tb employees </description>
<Name> EmployeeTag </name>
<Tag-class> net. inber. EmployeeTag </tag-class>
<Body-content> jsp </body-content>
</Tag>
 
</Taglib>
3. Call index. JSP on the jsp page
<% @ Taglib uri = "/EmployeeTag" prefix = "a" %>
<% @ Page import = "net. inber. *" %>
<% @ Page contentType = "text/html; charset = gb2312" language = "java" %>
<Html>
<Head>
<Title> first cumstomed tag </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<Body>
<P> the following content is displayed for records whose job_lvl is greater than or equal to 100 in the database pubs retrieval table employee: </p>

<Table width = '000000' height = '21' border = '1' bgcolor = "# C14646">
<Tr>
<Td width = "25%"> NO. </td>
<Td width = "17%"> name </td>
<Td width = "13%"> last name </td>
<Td width = "9%"> job_lvl </td>
<Td width = "36%"> date </td>
</Tr>
</Table>
<A: EmployeeTag/>
Aaa
</Body>
</Html>

4. TOMCAT deployment web. xml

<? Xml version = "1.0" encoding = "ISO-8859-1"?>
<! --
Copyright 2004 The Apache Software Foundation

Licensed under the Apache License, Version 2.0 (the "License ");
You may not use this file before t in compliance with the License.
You may obtain a copy of the License

Http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
Distributed under the license is distributed on an "as is" basis,
Without warranties or conditions of any kind, either express or implied.
See the license for the specific language governing permissions and
Limitations under the license.
-->

<Web-app 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

-App_2_4.xsd"
Version = "2.4">

<Display-Name> welcome to Tomcat </display-Name>
<Description>
Welcome to Tomcat
</Description>

<! -- JSPC servlet mappings start -->

<Taglib>
<Taglib-uri>/EmployeeTag </taglib-uri>
<Taglib-location>/WEB-INF/tlds/EmployeeTag. tld </taglib-location>
</Taglib>

<! -- JSPC servlet mappings end -->

</Web-app>

Well, you should try the next step. If you have any questions, contact me.

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.