利用struts自带的数据连接池连接数据库
zhanggok () 2004-03-28 01:44:39 在 Java / 框架、开源 提问
这是我从网上看到是但有一点不明白
dataSource=getDataSource(request);
这里的request是HttpServletRequest request
吗.这样就以得到数据源吗?不明白
struts自带的数据连接池在struts-config.xml中可以设置
本人的设置如下:
<data-sources>
<data-source type="org.apache.commons.dbcp.BasicDataSource">
<set-property
property="description"
value="Airline DB Connection config"/>
<set-property
property="driverClassName"
value="com.mysql.jdbc.Driver"/>
<set-property
property="maxCount"
value="10"/>
<set-property
property="password"
value=""/>
<set-property
property="url"
value="jdbc:mysql://localhost/airline"/>
<set-property
property="username"
value="root"/>
<set-property
property="maxWait"
value="5000" />
<set-property
property="defaultAutoCommit"
value="false" />
<set-property
property="defaultReadOnly"
value="false" />
</data-source>
</data-sources>
这些基本的,我想大家都应该看得懂吧
然后就是来验证我们的数据库连接了
这里用一个login来测试
第一个是LoginForm
package com.airline;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public final class LoginForm extends ActionForm {
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
password = null;
userName = null;
}
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if ((userName==null) || (userName.length() < 1))
errors.add("userName", new ActionError("error.missing.userName"));
if ((password==null) || (password.length() < 1))
errors.add("password", new ActionError("error.missing.password"));
return errors;
}
}
这个java就是给将来我们要提交的数据一个容器,就像“装水的玻璃瓶”一样,这是一个“瓶子”
接下来的就是LoginAction
package com.airline;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;
import javax.sql.DataSource;
public final class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String userName = ((LoginForm) form).getUserName();
String password = ((LoginForm) form).getPassword();
DataSource dataSource;
Connection con=null;
Statement stmt;
ResultSet rs;
String usn="",pwd="",result="";
try{
dataSource=getDataSource(request);
con=dataSource.getConnection();
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery("select * from login where username='"+userName+"'");
while(rs.next()){
usn=rs.getString(2);
pwd=rs.getString(3);
}
}catch(SQLException sqle){
getServlet().log("Connection.process",sqle);
}finally{
try{
con.close();
}catch(SQLException e){
getServlet().log("Connection.close",e);
}
}
if (userName!=null && password!=null &&
password.equals(pwd)) {
HttpSession session = request.getSession();
session.setAttribute("loggedIn", "1");
return mapping.findForward("success");
}
else {
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("error.login.failed"));
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
}
}
}
这个java就是来具体处理我们刚才放在"瓶子"里的东西,它就会连接到MYSQL数据库里去查找相应的数据,然后验证一下。
接下来,我们就要在struts-config.xml中对刚才的两个java作一下设置了
首先是在<Action-mapping>中加入如下的代码
<action path="/login"
type="com.airline.LoginAction"
name="loginForm"
scope="request"
input="/login.jsp">
<forward name="success" path="/mainMenu.jsp"/>
</action>
然后在<form-bean>中加入
<form-bean name="loginForm" type="com.airline.LoginForm"/>
最后再来做一下jsp页面
login.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html:html locale="true">
<head>
<title>Login Page</title>
<html:base/>
</head>
<body>
<html:errors/>
Please enter your user name and password
<br>
<html:form action="/login" focus="userName">
<table>
<tr>
<td>User Name:</td>
<td><html:text property="userName"/>
</tr>
<tr>
<td>Password:</td>
<td><html:password property="password" redisplay="false"/>
</tr>
<tr>
<td colspan=2 align=right><html:submit property="submit" value="Login"/></td>
</tr>
</table>
</html:form>
</body>
</html:html>
mainMenu.jsp
<%
if (session.getAttribute("loggedIn")==null) {
%>
<jsp:forward page="login.jsp"/>
<%
}
else {
%>
<html>
<head>
<title>Main Menu</title>
</head>
<body>
<b>Main Menu</b>
<br><a href=logout.do>Log Out</a>
<br><a href=viewSecret.do>View Company Secrets</a>
</body>
</html>
<%
}
%>
在classes下找到application.properties
加入如下代码:
errors.header=<b>Error(s)!!!</b><ul>
errors.footer=</ul><hr>
error.missing.userName=<li>A user name was not found.</li>
error.missing.password=<li>A password was not found.</li>
error.login.failed=<li>Incorrect user name and/or password.</li>
这些是用来显示出错信息的。
好了,打开Tomcat服务器,来测试一下吧