⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectiontag.java

📁 Servlet与JSP核心编程原码,适合刚做软件开发的程序员参考!
💻 JAVA
字号:
package examples;import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*;import java.io.*;import java.sql.*;/** * Example: A connection/password/userid/query set of tags. * All JDBC code is just to show things. * * This is the runtime representation of a connection tag * * This example is made intentionally extra complex to show how to do * some things * * ConnectionTag supports two programming paradigms.  * * (A) define an id to use later down. * <connection id="con01" ref="connection.xml"> *  <password>.... *  <userid>.... * </connection>  * ... now the connection object is defined via "id" * * (B) implicitly define a connection that is available within the start/end tag * <connection ref="connection.xml"> * <password>....</password> * <userid>....</userid> * <query....> </query> * </connection> */public class ConnectionTag extends TagSupport {    /**     * Create a new ConnectionTag     */    public ConnectionTag() {	super();    }    /**     * Attributes for this action:     *   id (inherited)     *   ref     */    /**     * ref is a reference to some connection data.     * setter and getter methods.     */    public void setRef(String s) {	this.ref = s;    }    public String getRef() {	return this.ref;    }    /**     * Process the start tag.     * Extract and store away any needed TagData.     *     * @return evaluate the body     */    public int doStartTag() throws JspTagException {	ref = getRef();	if (ref == null) {	  throw new JspTagException("missing ref attribute");	}	return EVAL_BODY_INCLUDE;    }    // No need to do anything before or after body evaluation    /**     * Process the end tag     *     * Define an object into the PageContect if ID is given     */    public int doEndTag() throws JspTagException {	String tagId = getId();	if (tagId != null) {	    if (userId == null) {	      		throw new JspTagException("userId has not been defined yet");	    }	    if (password == null) {		throw new JspTagException("password has not been defined yet");	    }	    if (connection == null) {		getConnection();	    }	    pageContext.setAttribute(tagId, connection);	}	return EVAL_PAGE;    }    /**     * Get the SQL connection Object.     * A connection object requires having defined both the userid and the password     * previous to its use.  Trying to get the connection object before the data is     * available will produce an error     */    public Connection getConnection() throws JspTagException {	if (userId == null) {	    throw new JspTagException("userId has not been defined yet");	}	if (password == null) {	    throw new JspTagException("password has not been defined yet");	}	// TODO -- Debugging information?  Line number?	try {	    // get the connection object	    connection = DriverManager.getConnection(ref,						     userId,						     password);	} catch (SQLException ex) {	    // TODO -- revise this	    connection = null;	    throw new JspTagException("some error!");	}	return connection;    }    /**     * Release data     */    public void release() {	ref = null;	super.release();    }    // Methods that are used by the enclosing tags    // The methods could be package private if we wanted to    public void setUserId(String uid) {	userId = uid;    }    public void setPassword(String pass) {	password = pass;    }    // For completeness    public String getUserId() { return userId; }    public String getPassword() { return password; }    // class data    private Connection connection;// the sql connection object    private String password;	// password for the connection    private String userId;	// userid for the connection    private String ref;		// the ref attribute; assumed to be some URL spec}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -