一、三层架构
二、三层架构图
三、用户信息管理系统
利用MVC三层架构设计思想完成的一个用户信息管理系统,主要是让大家熟悉MVC三层架构。
1.项目功能
1 在pojo包中创建一个实体类User
package cn.pojo;
public class User {
private int uid;
private String uname;
private String pwd;
private String sex;
private int age;
private String birth;
public User() {
super();
}
public User(int uid, String uname, String pwd, String sex, int age, String birth) {
super();
this.uid = uid;
this.uname = uname;
this.pwd = pwd;
this.sex = sex;
this.age = age;
this.birth = birth;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
}
package cn.servlet;
import cn.pojo.User;
import cn.service.UserService;
import cn.service.UserServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
public class userServlet extends HttpServlet {
//获取service层对象
UserService us = new UserServiceImpl();
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//获取操做符
String oper = req.getParameter("oper");
if ("login".equals(oper)) {
//调用登录处理方法
checkUserLogin(req, resp);
} else if ("out".equals(oper)) {
//调用退出功能
userOut(req, resp);
} else if ("pwd".equals(oper)) {
//调用密码修改功能
userChangePwd(req, resp);
} else if ("show".equals(oper)) {
//调用显示所有用户功能
userShow(req, resp);
} else if("reg".equals(oper)){
//调用注册功能
userReg(req,resp);
}else{
return;
}
}
//注册用户功能
private void userReg(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//获取请求信息
String uname=req.getParameter("uname");
String pwd=req.getParameter("pwd");
String sex=req.getParameter("sex");
int age=req.getParameter("age")!=""?Integer.parseInt(req.getParameter("age")):0;
String birth=req.getParameter("birth");
/*//转日期格式
String[] bs=null;
if(birth!=""){
bs=birth.split("/");
birth=bs[0]+"-"+bs[1]+"-"+bs[2];//拼接
}*/
System.out.println(uname+":"+pwd+":"+sex+":"+age+":"+birth);
User u=new User(0,uname,pwd,sex,age,birth);//封装数据
//处理请求信息
//调用业务层处理
int index=us.userRegService(u);
//响应处理结果
if(index>0){
//获取session
HttpSession hs=req.getSession();
hs.setAttribute("reg", "true");
//重定向
resp.sendRedirect("login.jsp");
}
}
//显示所有用户信息
private void userShow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//处理请求
//调用service
List<User> lu=us.userShowService();
//判断
if(lu!=null){
//将查询的用户数据存储到request对象
req.setAttribute("lu",lu);
//请求转发
req.getRequestDispatcher("user/showUser.jsp").forward(req, resp);
return;
}
}
//登录处理
private void checkUserLogin(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
//获取请求信息
String uname=req.getParameter("uname");
String pwd=req.getParameter("pwd");
//处理请求信息
//校验
User u=us.checkUserLoginService(uname,pwd);
//System.out.println(u.getUid()+" "+u.getUname()+" "+u.getPwd()+" "+u.getSex()+" "+u.getAge()+" "+u.getBirth());
//响应处理结果
if(u!=null){
//获取session对象
HttpSession hs=req.getSession();
//将用户数据存储到session中
hs.setAttribute("user",u);
//重定向
resp.sendRedirect("main.jsp");
return;
}else {
//添加标识符到request中
req.setAttribute("flag",false);
//请求转发
req.getRequestDispatcher("login.jsp").forward(req, resp);
return;
}
}
//用户退出
private void userOut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//获取session对象
HttpSession hs=req.getSession();
//强制销毁session
hs.invalidate();
//重定向到登录页面
resp.sendRedirect("login.jsp");
}
//用户修改密码
private void userChangePwd(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//获取数据
String newPwd=req.getParameter("newPwd");
//从session中获取用户信息
User u=(User)req.getSession().getAttribute("user");
int uid=u.getUid();
//处理请求
//调用service处理
int index=us.userChangePwdService(newPwd,uid);
if(index>0){
//获取session对象
HttpSession hs=req.getSession();
hs.setAttribute("pwd","true");
//重定向到登录页面
resp.sendRedirect("login.jsp");
}
}
}
package cn.service;
import cn.pojo.User;
import java.util.List;
public interface UserService {
User checkUserLoginService(String uname,String pwd);//校验用户登录
int userChangePwdService(String newPwd, int uid);//修改用户密码
List<User> userShowService();//获取所有用户信息
int userRegService(User u);//用户注册
}
package cn.service;
import cn.dao.UserDao;
import cn.dao.UserDaoImpl;
import cn.pojo.User;
import java.util.List;
public class UserServiceImpl implements UserService{
//声明Dao层对象
UserDao ud=new UserDaoImpl();
@Override
public User checkUserLoginService(String uname, String pwd) {//用户登录
return ud.checkUserLoginDao(uname,pwd);
}
//修改密码
@Override
public int userChangePwdService(String newPwd, int uid) {
return ud.userChangePwdDao(newPwd,uid);
}
//获取所有用户信息
@Override
public List<User> userShowService() {
return ud.userShowDao();
}
//用户注册
@Override
public int userRegService(User u) {
return ud.userRegDao(u);
}
}
package cn.dao;
import cn.pojo.User;
import java.util.List;
public interface UserDao {
User checkUserLoginDao(String uname,String pwd);//根据用户名和密码查询用户信息
int userChangePwdDao(String newPwd, int uid);//修改密码
List<User> userShowDao();//查看所有用户
int userRegDao(User u);//用户注册
}
package cn.dao;
import cn.pojo.User;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class UserDaoImpl implements UserDao{
@Override//用户注册
public int userRegDao(User u) {
//声明jdbc对象
Connection conn=null;
PreparedStatement ps=null;
//声明变量
int index=-1;
try {
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获取链接
conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","2001223");
//创建sql语句
String sql="insert into t_user value(default,?,?,?,?,?)";
//创建sql命令对象
ps=conn.prepareStatement(sql);
//给占位符赋值
ps.setString(1,u.getUname());
ps.setString(2,u.getPwd());
ps.setString(3,u.getSex());
ps.setInt(4,u.getAge());
ps.setString(5,u.getBirth());
//执行sql
index=ps.executeUpdate();
}catch (Exception e){
e.getStackTrace();
}finally {
//关闭资源
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//返回结果
return index;
}
@Override
//获取所有用户信息
public List<User> userShowDao() {
//声明jdbc对象
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
//声明变量
List<User> lu=null;
try {
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获取链接
conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","2001223");
//创建sql语句
String sql="select * from t_user";
//创建sql命令对象
ps=conn.prepareStatement(sql);
//执行sql
rs=ps.executeQuery();
//给集合赋值
lu=new ArrayList<User>();
//遍历结果集
while (rs.next()){
//给变量赋值
User u=new User();
u.setUid(rs.getInt("uid"));
u.setUname(rs.getString("uname"));
u.setPwd(rs.getString("pwd"));
u.setSex(rs.getString("sex"));
u.setAge(rs.getInt("age"));
u.setBirth(rs.getString("birth"));
//将对象存储到集合中
lu.add(u);
}
}catch (Exception e){
e.getStackTrace();
}finally {
//关闭资源
try {
assert rs != null;
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return lu;
}
@Override
//根据用户id修改用户密码
public int userChangePwdDao(String newPwd, int uid) {
//声明jdbc对象
Connection conn=null;
PreparedStatement ps=null;
//创建变量
int index=-1;
try {
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获取链接
conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","2001223");
//创建sql语句
String sql="update t_user set pwd=? where uid=?";
//创建sql命令对象
ps=conn.prepareStatement(sql);
//给占位符赋值
ps.setString(1,newPwd);
ps.setInt(2,uid);
//执行sql
index=ps.executeUpdate();
}catch (Exception e) {
e.getStackTrace();
}finally {
//关闭资源
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//返回结果
return index;
}
@Override
public User checkUserLoginDao(String uname, String pwd){//根据用户名和密码查询用户信息
//声明jdbc对象
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
//声明变量
User u=null;
try {
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获取链接
conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","2001223");
//创建sql语句
String sql="select * from t_user where uname=? and pwd=?";
//创建sql命令对象
ps=conn.prepareStatement(sql);
//给占位符赋值
ps.setString(1,uname);
ps.setString(2,pwd);
//执行sql
rs=ps.executeQuery();
//遍历结果集
while (rs.next()){
u=new User();
u.setUid(rs.getInt("uid"));
u.setUname(rs.getString("uname"));
u.setPwd(rs.getString("pwd"));
u.setSex(rs.getString("sex"));
u.setAge(rs.getInt("age"));
u.setBirth(rs.getString("birth"));
}
}catch (Exception e){
e.getStackTrace();
}finally {
//关闭资源
try {
assert rs != null;
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//返回结果
return u;
}
}