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

本文介绍了使用Java、JSP、Tomcat和XML实现自定义标签检索数据库记录的方法。通过创建标签处理程序EmployeeTag.java、TLD文件EmployeeTag.tld,在JSP页面index.jsp中调用标签,并在TOMCAT的web.xml中部署,可检索数据库pubs中表employee里job_lvl>=100的数据记录。

标签应用:
数据源
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>

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

本课程详细讲解了以下内容:    1.jsp环境搭建及入门、虚拟路径和虚拟主机、JSP执行流程    2.使用Eclipse快速开发JSP、编码问题、JSP页面元素以及request对象、使用request对象实现注册示例    3.请求方式的编码问题、response、请求转发和重定向、cookie、session执行机制、session共享问题     4.session与cookie问题及application、cookie补充说明及四种范围对象作用域     5.JDBC原理及使用Statement访问数据库、使用JDBC切换数据库以及PreparedStatement的使用、Statement与PreparedStatement的区别     6.JDBC调用存储过程和存储函数、JDBC处理大文本CLOB及二进制BLOB类型数据     7.JSP访问数据库、JavaBean(封装数据和封装业务逻辑)     8.MVC模式与Servlet执行流程、Servlet25与Servlet30的使用、ServletAPI详解与源码分析     9.MVC案例、三层架构详解、乱码问题以及三层代码流程解析、完善Service和Dao、完善View、优化用户体验、优化三层(加入接口和DBUtil)    1 0.Web调试及bug修复、分页SQL(Oracle、MySQL、SQLSERVER)     11.分页业务逻辑层和数据访问层Service、Dao、分页表示层Jsp、Servlet     12.文件上传及注意问题、控制文件上传类型和大小、下载、各浏览器下载乱码问题     13.EL表达式语法、点操作符和中括号操作符、EL运算、隐式对象、JSTL基础及set、out、remove     14.过滤器、过滤器通配符、过滤器链、监听器     15.session绑定解绑、钝化活化     16.以及Ajax的各种应用     17. Idea环境下的Java Web开发
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值