全部代码如下:
bean:
package myeshopbean;
import java.io.*;
import java.util.*;
import java.beans.*;
import java.sql.*;
import javax.servlet.http.*;
import myeshopbean.AppConstants;
import myutils.*;
public class ConnectionManager implements AppConstants
{
/**The debug boolean =default is false */
private boolean debug = false;
protected Connection con;
protected String driver;
protected String passwd;
protected String url;
protected String user;
protected DebugWriter writer;
PropertyChangeSupport pcs;
public ConnectionManager()
{
pcs = new PropertyChangeSupport(this);
writer = new DebugWriter();
writer.setDebugFileName("c://debug.log");
}
public void setDriverClass(String s)
{
driver = s;
writer.writeDebug( "DriverClass: " + s );
try
{
Class.forName( driver ).newInstance();
}
catch(Exception ex)
{
pcs.firePropertyChange(driverClass, null, ex);
if (debug)
{
writer.writeDebug("Error setting driver: " + ex);
}
}
}
public void setConnectionUrl(String s)
{
url = s;
}
public void setDebug(String b)
{
debug = b.equals("true");
}
public void addPropertyChangeListener (PropertyChangeListener l)
{
pcs.addPropertyChangeListener( l );
}
public void removePropertyChangeListener(PropertyChangeListener l)
{
pcs.removePropertyChangeListener( l );
}
public void processRequest (HttpServletRequest req)
{
user = req.getParameter(userId);
passwd = req.getParameter(password);
if (debug)
{
if(user == null || user.equals("") )
{
writer.writeDebug("Empty user id parameter");
}
if (passwd == null || passwd.equals("") )
{
writer.writeDebug("Empty password parameter");
}
}
}
public void logon()
{
try
{
con = DriverManager.getConnection( url, user, passwd );
pcs.firePropertyChange(connection, null, con);
if (debug)
{
writer.writeDebug("Connection succeded! URL: " + url
+ " Driver: " + driver + " User: " + user + " Pwd: " + passwd);
}
}
catch(Exception ex)
{
pcs.firePropertyChange( conError, null, ex);
if (debug)
{
writer.writeDebug("Connection failed! URL: " + url
+ " Driver: " + driver + " User: " + user + " Pwd: "
+ passwd + ex.getMessage());
}
}
}
}
测试页面:try1.jsp
调用地址:http://localhost:8080/myjsp/myEshop/try1.jsp?userid=esdata&passwd=eshop123456
jsp内容如下:
<%@ page contentType="text/html;
charset=gb2312" language="java" import="java.sql.*" errorPage="error.jsp" %>
<jsp:useBean id="mySqlCon" scope="page" class="myeshopbean.ConnectionManager"/>
<%
mySqlCon.setDebug("true");
mySqlCon.processRequest(request);
mySqlCon.setDriverClass("com.mysql.jdbc.Driver");
mySqlCon.setConnectionUrl(" jdbc:mysql://localhost/eshop?user=esdata&password=eshop123456");
mySqlCon.logon();
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=gb2312">
<title>无标题文档</title>
</head>
<p>测试页面!</p>
<body>
</body>
</html>