Label application: Data source MyDataSource2 User SA Password (empty) Database PUBS table: Employee
Description: Windows ODBC --- SQL Database PUBS Table: Employee
Function: Retrieve data records of JOB_LVL> = 100, call it in place in 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: MyDataSource2", "SA", ""); String strQuery = "SELECT EMP_ID, FNAME, LNAME, JOB_LVL, HIRE_DATE AROM EMPLOYE 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 ("
"); 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 (""); out.write (" " EMP_ID " TD> "); Out.write (" "); Out.write (" LNAME " TD>"); Out.write (" " 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 Contond Not Write To
JSP out ");} return evAl_page; // Return Skip_page;}} 2.TLD file Employeetag.TLD XML Version =" 1.0 "encoding =" ISO-8859-1 "?>
a tag library adricising simpletag handlers. description> 1.0 tlib-version> a Simple Tab Library for the Examples Description>
Select DB Pubs Tb Employees Description> Employeetag name> net.inber.employeetag tag-class> jsp body-content> JSP body-content > Tag> taglib> 3. JSP page call index.jsp <% @ taglib URI = "/ Employeetag" prefix = "a"%> <% @ Page Import = "Net.inber. *"%> < % @ Page ContentType = "Text / HTML; Charset = GB2312" Language = "Java"%> first cumstomed tag title> head> The following content from the library PUBS search table EMPLOYEE JOB_LVL is greater than or equal to 100 record: p>
Name td> Listing TD> Date TD> TR > table> AAA body> html>
4. Tomcat Deployment Web.xml
XML Version = "1.0" Encoding = "ISO-8859-1">
http://www.apache.org/license/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 .-->
Welcome to Tomcat display-name> Welcome to Tomcat description>
/ Employeetag taglib-uri> /web-inf/tlds/employeetag.tld taglib-location> taglib>
web-app>
Ok, the last step below you should try it with your hands, if you have any questions, you can contact me.
转载请注明原文地址:https://www.9cbs.com/read-124942.html