<datasources>
<datasource key=DataSource type=orgapachecommonsdbcpBasicDataSource>
<setproperty property=driverClassName value=commysqljdbcDriver />
<setproperty property=url value=jdbc:mysql://localhost:/struts_login/>
<setproperty property=username value=root/>
<setproperty property=password value=ihpp/>
</datasource>
</datasources>
配置了数据源后就可以在Action中访问数据源了
代码如下所示
//获取Servlet上下文对象
ServletContext ctx = servletgetServletContext();
//获得数据源
DataSource ds = (DataSource) ctxgetAttribute(DataSource);
//获取数据库的连接
Connection conn = dsgetConnection();
B修改Action
Action将取得数据源得到一个数据库的连接把页面传近来的用户名和密码通过jdbc与数据库进行对比如果存在转到successjsp并显示其名称否则到错误页面
具体实现代码如下所示
public class LoginAction extends Action {
public ActionForward execute(ActionMapping actionMapping
ActionForm actionForm HttpServletRequest request
HttpServletResponse response) {
LoginActionForm loginform = (LoginActionForm) actionForm;
String username = loginformgetUsername();
String userpass = loginformgetUserpass();
ServletContext ctx = servletgetServletContext();
DataSource ds = (DataSource) ctxgetAttribute(DataSource);
boolean flag = false;
[] [] [] []