使用JDBC、spring框架、servlet实现一个简单的用户登录功能。
一、mySql数据库
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(40) DEFAULT NULL,
`pass` varchar(15) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
二、连接数据库(jdbc+spring)
(1)c3p0-config.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!-- 指定名称的配置 -->
<named-config name="oa">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/animal</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="maxPoolSize">100</property>
<property name="initialPoolSize">20</property>
<property name="minPoolSize">10</property>
<property name="acquireIncrement">5</property>
</named-config>
<!-- 缺省的配置 -->
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/animal</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="maxPoolSize">100</property>
<property name="initialPoolSize">20</property>
<property name="minPoolSize">10</property>
<property name="acquireIncrement">5</property>
</default-config>
</c3p0-config>
(2)工具类:
package blank.util;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DBConn {
private static ComboPooledDataSource cpds=new ComboPooledDataSource("oa");
private DBConn(){}
public static DataSource getDataSource(){
return cpds;
}
public static Connection getConn() throws SQLException{
return cpds.getConnection();
}
public static void release(Connection conn){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn=null;
}
}
}
三、创建对应实体
blank.domain包中建立User.java
package blank.domain;
public class User {
private Integer id;
private String name;
private String pass;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(Integer id, String name, String pass) {
super();
this.id = id;
this.name = name;
this.pass = pass;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", pass=" + pass + "]";
}
}
四、数据库操作接口
package blank.dao;
import blank.domain.User;
public interface UserDao {
User getUser(String name, String pass);
}
五、数据库操作的具体方法实现
package blank.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import blank.dao.UserDao;
import blank.domain.User;
import blank.util.DBConn;
public class UserDaoImpl implements UserDao{
private JdbcTemplate jdbcTemplate=new JdbcTemplate(DBConn.getDataSource());
@Override
public User getUser(String name, String pass) {
String sql="select id , name, pass from user where name=? and pass=?";
List<User> users=jdbcTemplate.query(sql, new Object[]{name,pass}, new RowMapper<User>(){
@Override
public User mapRow(ResultSet rs, int num) throws SQLException {
User u=new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
u.setPass(rs.getString("pass"));
return u;
}});
User user=null;
if(users!=null&&users.size()>0){
user=users.get(0);
}
return user;
}
}
说明:此处查询时不可使用单个个体查询的方式,否则当用户名不存在时会报错!
六、登录的jsp界面
(1)login.jsp:
<body>
<div style="text-align: center;">
${user}
<!-- ./login.do -->
<form action="./login.do" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pass"/></td>
</tr>
<tr style="text-align: center;">
<td colspan="2"><input type="submit" value="登录"/></td>
</tr>
</table>
</form>
</div>
</body>
(2)sc.jsp:
<body>
欢迎登录
</body>
七、servlet控制层
package blank.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import blank.dao.PetDao;
import blank.dao.UserDao;
import blank.dao.impl.PetDaoImpl;
import blank.dao.impl.UserDaoImpl;
import blank.domain.Pet;
import blank.domain.User;
public class LoginServlet extends HttpServlet {
private UserDao userDao=new UserDaoImpl();
private PetDao petDao=new PetDaoImpl();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name=request.getParameter("name");
String pass=request.getParameter("pass");
User user=userDao.getUser(name, pass);
if(user!=null){
request.setAttribute("user", user);
List<Pet> pets=petDao.getObjects();
request.setAttribute("pets", pets);
request.getRequestDispatcher("/sc.jsp").forward(request, response);
}else{
request.setAttribute("user", "用户名或密码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
八、结果展示
进入页面显示如下:
输入正确的用户名和密码后跳转至另一页面,显示如下:
输入错误的用户名或密码后留在登录页面不跳转,结果如下: