SMBMS(一)
项目搭建准备
-
搭建一个maven web项目
-
配置Tomcat
-
测试项目是否能够正常运行
-
导入项目中所需的jar包:
jsp、Servlet、mysql驱动、jstl、stand…
-
创建项目包结构
-
编写实体类 : ORM映射:表-类映射
-
编写基础公共类
1.数据库配置文件 【db.properties】
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=utf-8
username = root
password = *******
2.编写数据库的公共类
package com.ma.dao;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
//操作数据库的公共类
public class BaseDao {
private static String driver;
private static String url;
private static String username;
private static String password;
//静态代码块,类加载的时候就初始化了
static {
final Properties properties = new Properties();
//通过类加载器读取对应的资源
final InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
}
//获取数据库的链接
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url,username,password);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
//编写查询实体类
public static ResultSet execute (Connection connection, String sql, Object[] params, ResultSet resultSet, PreparedStatement preparedStatement) throws SQLException {
preparedStatement = connection.prepareStatement(sql);
for (int i = 0;i < params.length; i++) {
// setObject ,占位符从1开始,但是我们的数组是从0开始
preparedStatement.setObject(i + 1,params);
}
resultSet = preparedStatement.executeQuery(sql);
return resultSet;
}
//编写增删改公共方法
public static int execute(Connection connection,String sql,Object[] params,PreparedStatement preparedStatement) throws SQLException {
preparedStatement = connection.prepareStatement(sql);
for (int i = 0;i < params.length; i++) {
// setObject ,占位符从1开始,但是我们的数组是从0开始
preparedStatement.setObject(i + 1,params);
}
int updateRows = preparedStatement.executeUpdate();
return updateRows;
}
//释放资源
public static boolean closeResource(Connection connection , PreparedStatement preparedStatement, ResultSet resultSet) {
boolean flag = true;
if (resultSet != null) {
try {
resultSet.close();
//GC回收
resultSet = null;
} catch ( SQLException e) {
e.printStackTrace();
flag = false ;
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
//GC回收
preparedStatement = null;
} catch ( SQLException e) {
e.printStackTrace();
flag = false ;
}
}
if (connection != null) {
try {
connection.close();
//GC回收
connection = null;
} catch ( SQLException e) {
e.printStackTrace();
flag = false ;
}
}
return flag;
}
}
3.编写字符编码过滤器
package com.ma.filter;
import javax.servlet.Fliter;
import javax.servlet.*;
import java.io.IOException;
public class CharacterEncodingFilter implements Filter {
//初始化:web服务器启动的时候,就已经初始化了,随时等待过滤对象出现
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//解决中文乱码
servletRequest.setCharacterEncoding("UTF-8");
servletResponse.setCharacterEncoding("utf-8");
servletResponse.setContentType("text/html;charset=utf-8");
filterChain.doFilter(servletRequest,servletResponse); //让我们的请求继续走,如果不写,程序到这里会被拦截停止!
}
//销毁:web服务器关闭的时候,过滤会销毁
@Override
public void destroy() {
}
}
4.在web.xml中注册过滤器
<!-- 字符编码过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.ma.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- 导入静态资源:图片音频等
登录功能实现
- 编写前端页面【login.jsp】
- 设置首页【欢迎页面】
<!-- 设置欢迎页面 -->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
- 编写dao层 :登录用户登录的接口
import com.ma.pojo.User;
import java.sql.Connection;
import java.sql.SQLException;
//interface :面向接口编程
public interface UserDao {
//得到要登录的用户
public User getLoginUser(Connection connection, String loginName) throws SQLException;
}
- 编写到dao接口的实现类
package com.ma.dao.user;
import com.ma.dao.BaseDao;
import com.ma.pojo.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDaoImpl implements UserDao{
@Override
public User getLoginUser(Connection connection, String loginName) throws SQLException {
//得到想要登录的用户
PreparedStatement pstm = null;
ResultSet rs = null;
User user = null;
if (connection != null) {
//通过sql查询语句得到该用户的信息
String sql = "select * from t_user where loginName=?";
Object[] params = {loginName};
rs = BaseDao.execute(connection,pstm,rs,sql,params);
if (rs.next()) {
user = new User(); //封装到一个用户对象里面
user.setId(rs.getInt("id"));
user.setLoginName(rs.getString("loginName"));
user.setLoginPwd(rs.getString("loginPwd"));
user.setRealName(rs.getString("realName"));
}
BaseDao.closeResource(null,pstm,rs);
}
return user; //最后将这个用户返回给调用这个用户的人
}
}
- 业务层接口
public interface UserService {
//用户登录
public User login(String loginName, String loginPwd);
}
- 业务层实现类
public class UserServiceImpl implements UserService{
//业务层都会调用dao层,故需引入Dao层
private UserDao userDao;
public UserServiceImpl() {
userDao = new UserDaoImpl();
//userDao被实例化 可用容器完成
}
public User login(String loginName, String loginPwd) {
Connection connection = null;
User user = null;
try {
connection = BaseDao.getConnection();
//通过业务层调用对应的具体的数据库操作
user = userDao.getLoginUser(connection,loginName);
} catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.closeResource(connection,null,null);
}
return user;
}
- 编写Servlet
public class LoginServlet extends HttpServlet {
//Servlet:控制层,调用业务层
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("LoginServlet-start");
//获取用户名和密码
final String loginName = req.getParameter("loginName");
final String loginPwd = req.getParameter("loginPwd");
//与数据库中密码进行对比,调用业务层
final UserServiceImpl userService = new UserServiceImpl();
final User user = userService.login(loginName, loginPwd); //这里已经吧登录的人给查出来了
if (user != null) { //如果查有此人,可以登录【此处业务层中少了一密码判断】
//将用户的信息放到Session中;则该用户的信息在任意网站都可查到,除非该用户注销
req.getSession().setAttribute(Constants.USER_SESSION,user);
//跳转到主页【重定向】
resp.sendRedirect("//"); //主页为.jsp 暂时未写
}else { //未查到此人,无法登录
//转发回登录页面,并给出提示:用户名或密码错误
req.setAttribute("error", "用户名或密码错误");
req.getRequestDispatcher("login.jsp").forward(req,resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
- 注册Servlet
- 测试访问,确保以上功能成功
SMBMS(二)
登录功能优化
注销功能:移除Session,返回登录页面
(1)编写注销Servlet;
public class LogoutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//移除用户的Constants.USER_SESSION
req.getSession().removeAttribute(Constants.USER_SESSION);
resp.sendRedirect(req.getContextPath() + "/login.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
(2)注册xml;
登录拦截优化
只能通过用户名密码登录进入主页,退出后不能通过url路径进入
编写过滤器、注册xml
public class SysFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
final HttpServletRequest request = (HttpServletRequest) req;
final HttpServletResponse response = (HttpServletResponse) resp;
//过滤器,从Session中获取用户
final User user = (User) request.getSession().getAttribute(Constants.USER_SESSION);
if (user == null) { //已经被移除或注销,或者未登录
response.sendRedirect("/error.jsp");
} else {
filterChain.doFilter(req,resp);
}
}
@Override
public void destroy() {
}
}
SMBMS(三)
密码修改实现
项目架构思路:
前端 --请求–> Servlet --调用–> Service --调用–> Dao ----> 数据库
先理清项目功能和架构
网站如何进行访问
-
输入一个域名;回车
-
检查本机的C:\Windows\System32\drivers\etc\hosts配置文件下有没有这个域名映射;
(1)有:直接返回对应的IP地址,这个地址中,有我们需要访问的web程序,可以直接访问;
(2)没有:去DNS服务器找,找到的话就返回,找不到就返回找不到; -
可以配置一下环境变量(可选性)