java(j2ee)應用-簡單自訂標籤開發全過程—-之一(Inber)——JAVA夜未眠

來源:互聯網
上載者:User

標籤應用:
資料來源
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>

好了,下面的最後一步你應該自已動手試試了,如有問題可以和我聯絡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.