08-08
01
JSP自定义标签实例---从数据库读取数据放在下拉列表中
作者:Java伴侣 日期:2008-08-01
Myselect.java
DBconn.java
Users.java
myselect.tld
index.jsp
index.jsp
复制内容到剪贴板 程序代码
view plaincopy to clipboardprint?
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<USERS></USERS> list = getUserList();
JspWriter out = pageContext.getOut();
out.print("<Select>"); for (int i = 0; i < list.size(); i++) { out.print("<OPTION selected>"+list.get(i).getName()); out.print("</OPTION>"); } out.print("</Select>");
} catch (IOException e) {
System.out.println("发生错误!");
e.printStackTrace();
}
return SKIP_BODY;
}
@Override
public int doEndTag() throws JspException {
return EVAL_PAGE;
}
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<USERS></USERS> list = getUserList();
JspWriter out = pageContext.getOut();
out.print("<Select>"); for (int i = 0; i < list.size(); i++) { out.print("<OPTION selected>"+list.get(i).getName()); out.print("</OPTION>"); } out.print("</Select>");
} catch (IOException e) {
System.out.println("发生错误!");
e.printStackTrace();
}
return SKIP_BODY;
}
@Override
public int doEndTag() throws JspException {
return EVAL_PAGE;
}
复制内容到剪贴板 程序代码
/**
* 返回用户列表
*
* @return
*/
public List<USERS></USERS> getUserList() {
List<USERS></USERS> 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;
}
}
* 返回用户列表
*
* @return
*/
public List<USERS></USERS> getUserList() {
List<USERS></USERS> 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;
}
}
复制内容到剪贴板 程序代码
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(""); for (int i = 0; i < list.size(); i++) { out.print(""+list.get(i).getName()); out.print(""); } 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;
}
}
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(""); for (int i = 0; i < list.size(); i++) { out.print(""+list.get(i).getName()); out.print(""); } 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
复制内容到剪贴板 程序代码
DBconn.javaview plaincopy to clipboardprint?
package com.xiefei.db;
package com.xiefei.db;view plaincopy to clipboardprint?
import java.sql.Connection;<BR>import java.sql.DriverManager;<BR>import java.sql.SQLException;
import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;view plaincopy to clipboardprint?
public class DBConn {<BR> <BR> static{<BR> try {<BR> Class.forName("com.mysql.jdbc.Driver");<BR> } catch (ClassNotFoundException e) {<BR> e.printStackTrace();<BR> }<BR> }<BR> /**<BR> * 得到数据库连接<BR> * @return 一个数据库连接<BR> */<BR> public Connection getConnection(){<BR> Connection conn=null;<BR> try{<BR> conn=DriverManager.getConnection("jdbc:mysql://localhost/userinformation","root","123");<BR> } catch (SQLException e) {<BR> e.printStackTrace();<BR> }<BR> return conn;<BR> }<BR> <BR>}<BR>
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; } }
package com.xiefei.db;
package com.xiefei.db;view plaincopy to clipboardprint?
import java.sql.Connection;<BR>import java.sql.DriverManager;<BR>import java.sql.SQLException;
import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;view plaincopy to clipboardprint?
public class DBConn {<BR> <BR> static{<BR> try {<BR> Class.forName("com.mysql.jdbc.Driver");<BR> } catch (ClassNotFoundException e) {<BR> e.printStackTrace();<BR> }<BR> }<BR> /**<BR> * 得到数据库连接<BR> * @return 一个数据库连接<BR> */<BR> public Connection getConnection(){<BR> Connection conn=null;<BR> try{<BR> conn=DriverManager.getConnection("jdbc:mysql://localhost/userinformation","root","123");<BR> } catch (SQLException e) {<BR> e.printStackTrace();<BR> }<BR> return conn;<BR> }<BR> <BR>}<BR>
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
复制内容到剪贴板 程序代码
Users.javaview plaincopy to clipboardprint?
package com.xiefei.vo;
package com.xiefei.vo;view plaincopy to clipboardprint?
public class Users {<BR> private int id;<BR> private String name;
public class Users { private int id; private String name;view plaincopy to clipboardprint?
public int getId() {<BR> return id;<BR> }
public int getId() { return id; }view plaincopy to clipboardprint?
public void setId(int id) {<BR> this.id = id;<BR> }
public void setId(int id) { this.id = id; }view plaincopy to clipboardprint?
public String getName() {<BR> return name;<BR> }
public String getName() { return name; }view plaincopy to clipboardprint?
public void setName(String name) {<BR> this.name = name;<BR> }<BR>}<BR>
public void setName(String name) { this.name = name; }}
package com.xiefei.vo;
package com.xiefei.vo;view plaincopy to clipboardprint?
public class Users {<BR> private int id;<BR> private String name;
public class Users { private int id; private String name;view plaincopy to clipboardprint?
public int getId() {<BR> return id;<BR> }
public int getId() { return id; }view plaincopy to clipboardprint?
public void setId(int id) {<BR> this.id = id;<BR> }
public void setId(int id) { this.id = id; }view plaincopy to clipboardprint?
public String getName() {<BR> return name;<BR> }
public String getName() { return name; }view plaincopy to clipboardprint?
public void setName(String name) {<BR> this.name = name;<BR> }<BR>}<BR>
public void setName(String name) { this.name = name; }}
myselect.tld
复制内容到剪贴板 程序代码
myselect.tldview plaincopy to clipboardprint?
<?xml version="1.0" encoding="GBK" ?><BR><!DOCTYPE taglib<BR> PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"<BR> "<A href="http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd</A>"><BR><!-- 描述 --><BR><taglib><BR> <tlib-version>1.0</tlib-version><BR> <jsp-version>1.2</jsp-version><BR> <short-name>myselect</short-name>
<?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>view plaincopy to clipboardprint?
<tag><BR> <name>myselect</name><BR> <tag-class>com.xiefei.mytag.MySelect</tag-class><BR> <body-content>jsp</body-content><BR> </tag>
<tag> <name>myselect</name> <tag-class>com.xiefei.mytag.MySelect</tag-class> <body-content>jsp</body-content> </tag>view plaincopy to clipboardprint?
</taglib>
</taglib>
<?xml version="1.0" encoding="GBK" ?><BR><!DOCTYPE taglib<BR> PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"<BR> "<A href="http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd</A>"><BR><!-- 描述 --><BR><taglib><BR> <tlib-version>1.0</tlib-version><BR> <jsp-version>1.2</jsp-version><BR> <short-name>myselect</short-name>
<?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>view plaincopy to clipboardprint?
<tag><BR> <name>myselect</name><BR> <tag-class>com.xiefei.mytag.MySelect</tag-class><BR> <body-content>jsp</body-content><BR> </tag>
<tag> <name>myselect</name> <tag-class>com.xiefei.mytag.MySelect</tag-class> <body-content>jsp</body-content> </tag>view plaincopy to clipboardprint?
</taglib>
</taglib>
index.jsp
index.jsp
复制内容到剪贴板 程序代码
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%><BR><%@ taglib uri="WEB-INF/myselect.tld" prefix="m"%><BR><%<BR>String path = request.getContextPath();<BR>String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";<BR>%>
<%@ 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+"/";%>view plaincopy to clipboardprint?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><BR><html><BR> <head><BR> <base href="<%=basePath%>"><BR> <BR> <title>My JSP 'index.jsp' starting page</title><BR> </head><BR> <BR> <body>
<!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> <m:myselect/>
</body>
</html>
<%@ 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+"/";%>view plaincopy to clipboardprint?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><BR><html><BR> <head><BR> <base href="<%=basePath%>"><BR> <BR> <title>My JSP 'index.jsp' starting page</title><BR> </head><BR> <BR> <body>
<!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> <m:myselect/>
</body>
</html>
评论: 0 | 引用: 0 | 查看次数: 983
发表评论