it167.com  设为主页
 收藏本站
 
  资讯:业界动态 | 软件动态 | 人物专栏 | 安全资讯 | 网络生活 | 电子商务 | 小游戏 | 视频 | 美女图片 | 音乐
  网络编程 | 网站运营 | 网页制作 | 图形图象 | 操作系统 | 媒体动画 | 软件教学 | 网络应用 | 邮件系统 | 网络安全 | 认证考试
asp | .net | php | jsp | Sql | java | Dreamweaver | FrontPages | Javascript | css | Coreldraw | photoshop | Flash | Coreldraw
当前位置: > 主页>网络编程>Java>JAVA开发技巧>java(j2ee)应用-简单自定义标签开发全过程----之一(Inber)
最新新闻

·机会与整合 边缘化互联
·TOM-Skype新增三大本地
·雅虎抢闸邮箱竞赛 网易
·新浪抢攻北京奥运
·洞悉网络口碑的掘金机会
·拆解网络病毒黑金交易
·木马下载器近期出现新变
·《互联网周刊》第17期文
·Web2.0是否催生自吹自擂
·三张宝宝裸照招来MSN封
热门新闻
·jsp 下实现防止重复在线
·一个必看的Java程序
·存储过程
·构建自己的基于Java的超
·构建自己的基于Java的超
·Web应用中的中文问题
·学习MySQL常用操作命令
·MySQL中修改密码及访问
·Hibernate 3新增XML关系
·java实现ftp下载文件
推荐新闻
 
 

java(j2ee)应用-简单自定义标签开发全过程----之一(Inber)  

作者:   来源:http://www.codesky.net/article/doc/200505/20050530   点击:   日期:2007-12-03

java(j2ee)应用-简单自定义标签开发全过程----之一(Inber)
关键字:不详

标签应用:
数据源
MyDataSource2
用户sa
密码(空)
数据库pubs 表:employee

说明:windows ODBC ---SQL  数据库pubs 表:employee

功能:检索job_lvl>=100的数据记录,在JSP页面中任意想调用的地方调用它。


1:标签处理程序: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:MyDataSource2","sa","");
      String strQuery = "select emp_id,fname,lname,job_lvl,hire_date from employee where

job_lvl>=100";
      state = con.createStatement();
      rs = state.executeQuery(strQuery);
      String emp_id=null;
      String fname=null;
      String lname=null;
      String job_lvl=null;
      String hire_date=null;     
     
out.write("<table width='677' 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='25%'>"+emp_id+"</td>");
 out.write("<td width='17%'>"+fname+"</td>");
 out.write("<td width='13%'>"+lname+"</td>");
 out.write("<td width='9%'>"+job_lvl+"</td>");
 out.write("<td width='36%'>"+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 to

JSP out");
  }
  return EVAL_PAGE;
 // return SKIP_PAGE;
 }
}
2.TLD文件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. JSP页面调用index.jsp
<%@ 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>以下的内容从库pubs检索表employee中job_lvl大于等于100的记录显示:</p>

<table width='677' height='21' border='1' bgcolor="#C14646">
  <tr>
    <td width="25%" >编号</td>
    <td width="17%">名 </td>
    <td width="13%">姓 </td>
    <td width="9%">job_lvl</td>
    <td width="36%">日期</td>
  </tr>
</table>
<a:EmployeeTag/>
aaa
</body>
</html>

4。TOMCAT部署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 except in compliance with the License.
  You may obtain a copy of the License at

      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>

好了,下面的最后一步你应该自已动手试试了,如有问题可以和我联系。
 



文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【论坛讨论

   相关文章:
·java实现精确的"四舍五入" ·Tomcat中通过JNDI配置访问数据库
·Hibernate 实现分页查询 (转) ·在STRUTS中如何使用 标签
·完全掌握java中的"包"机制 ·java类装载机制

   文章评论:(0条)
  
 请留名: 匿名评论   点击查看所有评论 网管论坛
 

  责任编辑:it167  声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。