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
}
Servlet与JSP核心编程原码.zip_law5x6
版权申诉
100 浏览量
2022-09-24
12:34:15
上传
评论
收藏 10KB ZIP 举报
小贝德罗
- 粉丝: 71
- 资源: 1万+
最新资源
- 基于STC12C5A60S2单片机+LCD12864屏实现贪吃蛇源码程序软件源码KEIL C51工程文件.zip
- Java资源 javaeasycms-v2.0.zip
- 基于Qt和opencv图像格式处理工具源代码
- tomcat安装及配置教程.zip
- python,爬取电商平台手机评论并保存为excel,用python进行数据分析并展示图片
- matlab对直接序列扩频和直接序列码分多址进行仿真实验源代码
- 六一儿童节祝福网页源代码
- 端午节活动吃豆人游戏源代码
- python爬虫案例findtrip-master.zip
- 对要使用到的开源组件进行组合整理, 搭建一个简单的框架,方便后续的开发, 其中包括MVP
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈