功能实现:
login.jsp写登陆界面
提交数据给servlet,servlet接收数据并将数据封装到一个实体类中
根据提交的数据到mysql中查找
如果找到就算登陆成功,跳转到成功页面,否则失败跳转到登陆页面
最后加了一个过滤器,只有登陆成功的才能跳转到后台成功页面
效果:
一、先看项目结构
二、每一部分代码
1、login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h3><font color="red">${msg }</font></h3>
<form action="${pageContext.request.contextPath }/ServletDemo" method="post">
<table>
<tr>
<td>UserName</td>
<!-- 这里踩过大坑,未定义输入表单的name,而是直接values="username"....弄了四五天....大哭啊-->
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>PassWord</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit"/></td>
</tr>
</table>
</form>
</body>
</html>
2、定义一个servlet接收数据 ServletDemo.java
package net.cszxx.control;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;
import net.cszxx.domain.User;
import net.cszxx.model.UserModel;
public class ServletDemo extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = new User();
user.setUsername(username);
user.setPassword(password);
UserModel userModel = new UserModel();
User existUser = userModel.login(user);
if (existUser != null) {
request.getSession().setAttribute("existUser", existUser);
response.sendRedirect(request.getContextPath() + "/jsp/success.jsp");
} else {
request.setAttribute("msg", "Error,pls regain to load.");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
3、定义一个模型层,将数据到mysql中进行查找
package net.cszxx.model;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import net.cszxx.domain.User;
import net.cszxx.utils.JDBCUtils;
public class UserModel {
public User login(User user) throws SQLException {
QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
String sql = "select * from user1 where username = ?";
User existUser = queryRunner.query(sql, new BeanHandler<User>(User.class),user.getUsername());
return existUser;
}
}
4、封装数据的实体类 User
package net.cszxx.domain;
public class User {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
}
5、过滤器PrevilegeFilter
package net.cszxx.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import net.cszxx.domain.User;
public class PrevilegeFilter implements Filter{
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)request;
User existUser = (User) req.getSession().getAttribute("existUser");
if(existUser == null) {
req.setAttribute("msg", "Error,pls reload.");
req.getRequestDispatcher("/login.jsp").forward(req, response);
}else {
chain.doFilter(req, response);
}
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
}
6、工具类
package net.cszxx.utils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JDBCUtils {
public static final ComboPooledDataSource dataSource = new ComboPooledDataSource();
/*
* 获取连接的方法
*/
public static Connection getConnection() throws Exception{
return dataSource.getConnection();
}
/*
* 获得连接池
*/
public static DataSource getDataSource() {
return dataSource;
}
/*
* 释放资源
*/
public static void release(ResultSet rs,Statement stmt,Connection conn) {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
public static void release(Statement stmt,Connection conn) {
if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}
7、c3p0-config.xml配置
<?xml version="1.0" encoding="utf-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///web05</property>
<property name="user">root</property>
<property name="password">12345678</property>
</default-config>
</c3p0-config>
8、成功页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>success!</h1>
</body>
</html>
9、数据库
create database web05;
use web05;
create table user1(
id int primary key auto_increment,
username varchar(20),
password varchar(20)
);
insert into user values(null,'aaa','123456');