JSP自定义标签实例---从数据库读取数据放在下拉列表中
Myselect.java
package com.xiefei.mytag; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.BodyContent; import javax.servlet.jsp.tagext.BodyTagSupport; import com.xiefei.db.DBConn; import com.xiefei.vo.Users; public class MySelect extends BodyTagSupport { @Override public void setBodyContent(BodyContent b) { super.setBodyContent(b); } @Override public int doStartTag() throws JspException { try { List list = getUserList(); JspWriter out = pageContext.getOut(); out.print(" "+list.get(i).getName()); out.print(""); } catch (IOException e) { System.out.println("发生错误!"); e.printStackTrace(); } return SKIP_BODY; } @Override public int doEndTag() throws JspException { return EVAL_PAGE; } /** * 返回用户列表 * * @return */ public List getUserList() { List list = new ArrayList(); DBConn dbconn = new DBConn(); Connection conn = dbconn.getConnection(); String mysql = "select name from users"; PreparedStatement statement = null; ResultSet rs = null; try { statement = conn.prepareStatement(mysql); rs = statement.executeQuery(); while (rs.next()) { Users user = new Users(); user.setName(rs.getString(1)); list.add(user); } } catch (SQLException e) { e.printStackTrace(); return null; } finally { try { rs.close(); statement.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return list; } }
DBconn.java
package com.xiefei.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConn {
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 得到数据库连接
* @return 一个数据库连接
*/
public Connection getConnection(){
Connection conn=null;
try{
conn=DriverManager.getConnection("jdbc:mysql://localhost/userinformation","root","123");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
Users.java
package com.xiefei.vo;
public class Users {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
myselect.tld
<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<!-- 描述 -->
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>myselect</short-name>
<tag>
<name>myselect</name>
<tag-class>com.xiefei.mytag.MySelect</tag-class>
<body-content>jsp</body-content>
</tag>
</taglib>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib uri="WEB-INF/myselect.tld" prefix="m"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
引用相当简单