MVC综合案例

MVC综合案例_第1张图片

 MVC综合案例_第2张图片

pojo--Dept

package com.qf.webpro.pojo;

import java.io.Serializable;

public class Dept implements Serializable {
    private int deptNo;
    private String dName;
    private String loc;

    public Dept(int deptNo, String dName, String loc) {
        this.deptNo = deptNo;
        this.dName = dName;
        this.loc = loc;
    }

    public Dept() {
    }

    public int getDeptNo() {
        return deptNo;
    }

    public void setDeptNo(int deptNo) {
        this.deptNo = deptNo;
    }

    public String getdName() {
        return dName;
    }

    public void setdName(String dName) {
        this.dName = dName;
    }

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }
}

 pojo--Users

package com.qf.webpro.pojo;

import java.io.Serializable;

public class Users implements Serializable {
    private String userId;
    private String pwd;
    private String realName;

    public Users(String userId, String pwd, String realName) {
        this.userId = userId;
        this.pwd = pwd;
        this.realName = realName;
    }

    public Users() {
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }
}

dao--DeptDao

package com.qf.webpro.dao;

import com.qf.webpro.pojo.Dept;

import java.sql.SQLException;
import java.util.List;

public interface DeptDao {
    //添加部门
    int addDept(Dept dept) throws SQLException;
    //显示所有,无条件查询
    List findDept() throws SQLException;
    //删除
    int deleteDeptByNo(int deptNo) throws SQLException;
    //修改
    int editDept(Dept dept) throws SQLException;
    //针对修改创建的查询  ---一次修改等于一次查询加一次添加
    Dept findDeptByNo(int deptNo) throws SQLException;
    //模糊查询
    List findDeptsByCondition(Dept dept) throws SQLException;
}

dao--UsersDao

//用来写接口

package com.qf.webpro.dao;

import com.qf.webpro.pojo.Users;

public interface UsersDao {
    public Users login(Users users);
}

dao--DeptDaoImpl

package com.qf.webpro.dao.impl;

import com.qf.webpro.dao.DeptDao;
import com.qf.webpro.pojo.Dept;
import com.qf.webpro.utils.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class DeptDaoImpl implements DeptDao {
private QueryRunner queryRunner = new QueryRunner(DBUtil.getDataSource());
//添加
    @Override
    public int addDept(Dept dept) throws SQLException {
        String sql = "insert into dept(deptno,dname,loc) values(?,?,?)";
        Object[] params = {dept.getDeptNo(),dept.getdName(),dept.getLoc()};
        int result = 0;
            result = queryRunner.update(sql,params);
        return result;
    }
//查询
    @Override
    public List findDept() throws SQLException {
        String sql = "select deptno,dname,loc from dept";
        List list = null;

            list = queryRunner.query(sql,new BeanListHandler(Dept.class));

        return list;
    }

    //删除
    @Override
    public int deleteDeptByNo(int deptNo) throws SQLException {
        String sql = "delete from dept where deptno=?";

        return queryRunner.update(sql,deptNo);
    }

    //修改
    @Override
    public int editDept(Dept dept) throws SQLException {
        String sql = "update dept set dname =?,loc=? where deptno=?";
        Object[] ary = {dept.getdName(),dept.getLoc(),dept.getDeptNo()};

        return queryRunner.update(sql,ary);
    }
    //针对修改创建的查询
    @Override
    public Dept findDeptByNo(int deptNo) throws SQLException {
        String sql = "select deptno,dname,loc from dept where deptno=?";
        //因为只修改了一条所以不用BeanList 用BeanHandler
        return queryRunner.query(sql,new BeanHandler(Dept.class),deptNo);
    }

    @Override
    public List findDeptsByCondition(Dept dept) throws SQLException {
        String sql = "select deptno,dname,loc from dept where dname like ? and loc like ?";

        return queryRunner.query(sql,new BeanListHandler(Dept.class),dept.getdName(),dept.getLoc());
    }
}

dao--UsersDaoImpl

//继承接口写方法
//dao层用来连接数据库写增删改查
package com.qf.webpro.dao.impl;

import com.qf.webpro.dao.UsersDao;
import com.qf.webpro.pojo.Users;
import com.qf.webpro.utils.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class UsersDaoImpl implements UsersDao {
    //接收连接池对象
    QueryRunner queryRunner = new QueryRunner(DBUtil.getDataSource());
    @Override
    public Users login(Users user) {
        String sql="select userid,pwd,realname From users where userId=?";

        //查询一条的话就是BeanHandler
        //查询所有的话就是BeanList

        Users u = null;
        try {
            u=queryRunner.query(sql,new BeanHandler(Users.class), user.getUserId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return u;
    }
}

service--DeptService

package com.qf.webpro.service;

import com.qf.webpro.pojo.Dept;

import java.sql.SQLException;
import java.util.List;

public interface DeptService {
    //添加
    boolean addDept (Dept dept) throws SQLException;
    //查询
    List findDept() throws SQLException;
    //删除
    boolean deleteDeptByNo(int deptNo) throws SQLException;
    //修改
    boolean editDept(Dept dept) throws SQLException;
    //针对于修改的查询
    Dept findDeptByNo(int deptNo) throws SQLException;
    //模糊查询
    List findDeptByCondition(Dept dept) throws SQLException;
}

service--UsersService

package com.qf.webpro.service;

import com.qf.webpro.pojo.Users;

public interface UserService {
    public Users Login(Users user);
}

service--DeptServiceImpl

package com.qf.webpro.service.impl;

import com.qf.webpro.dao.DeptDao;
import com.qf.webpro.dao.impl.DeptDaoImpl;
import com.qf.webpro.pojo.Dept;
import com.qf.webpro.service.DeptService;

import java.sql.SQLException;
import java.util.List;

public class DeptServiceImpl implements DeptService {
    private DeptDao deptDao = new DeptDaoImpl();
    //添加
    @Override
    public boolean addDept(Dept dept) throws SQLException {
        int result = deptDao.addDept(dept);
        if(result==1){
            return true;
        }else{
            return false;
        }

    }

    //查询
    @Override
    public List findDept() throws SQLException {
        return deptDao.findDept();
    }

    //删除
    @Override
    public boolean deleteDeptByNo(int deptNo) throws SQLException {
        int result=deptDao.deleteDeptByNo(deptNo);
        return result==1;
    }

    //修改
    @Override
    public boolean editDept(Dept dept) throws SQLException {
        int result = deptDao.editDept(dept);

        return result==1;
    }
    //针对于修改的查询
    @Override
    public Dept findDeptByNo(int deptNo) throws SQLException {
        return deptDao.findDeptByNo(deptNo);
    }
    
    //模糊查询
    @Override
    public List findDeptByCondition(Dept dept) throws SQLException {
        dept.setdName("%" + dept.getdName() + "%");
        dept.setLoc("%" + dept.getLoc() + "%");
        return deptDao.findDeptsByCondition(dept);
    }


}

service--UsersServiceImpl

package com.qf.webpro.service.impl;

import com.qf.webpro.dao.UsersDao;
import com.qf.webpro.dao.impl.UsersDaoImpl;
import com.qf.webpro.pojo.Users;
import com.qf.webpro.service.UserService;

public class UserServiceimpl implements UserService {
    UsersDao usersDao = new UsersDaoImpl();

    @Override
    public Users Login(Users user) {
        Users u = usersDao.login(user);
        //u是查到的账户
        if(u != null){
            //判断密码是否正确
            //前边是传值获取到的  后边是数据库查到的
            if(user.getPwd().equals(u.getPwd())){
                    return u;
            }else{
                return null;
            }
        }
        //为空的时候返回null
        return null;
    }
}

servlet--AddDeptServlet    添加

package com.qf.webpro.servlets;



import com.qf.webpro.pojo.Dept;
import com.qf.webpro.service.DeptService;
import com.qf.webpro.service.impl.DeptServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;


//添加部门
@WebServlet(name = "AddDeptServlet", value = "/ads", loadOnStartup=1)
public class AddDeptServlet extends HttpServlet {

    //表单的method是post时会调用doPost方法
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //使用参数request来获取
        //1、获取表单数据  得到的就是前端表单里的值
        //防止数据库传入的值出现乱码将其转换成utf-8
//        request.setCharacterEncoding("utf-8");
        int deptNo = Integer.parseInt(request.getParameter("deptNo"));
        String dName = request.getParameter("dName");
        String loc = request.getParameter("loc");
        Dept dept = new Dept(deptNo,dName,loc);
        //2、调用业务逻辑
        DeptService deptService = new DeptServiceImpl();
        try {
            boolean result = deptService.addDept(dept);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        //分发转向
        response.sendRedirect("deptList");


    }

    //表单的method是get时,会调用doGet方法
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

servlet--DeleteDeptServlet    删除

package com.qf.webpro.servlets;

import com.qf.webpro.service.DeptService;
import com.qf.webpro.service.impl.DeptServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

//删除的servlet
@WebServlet(name = "DeleteDeptServlet",value = "/deleteDeptServlet")
public class DeleteDeptServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //读取url传参数据
        //这个deptNo就是jsp里?后边的deptNo
        int deptNo = Integer.parseInt(request.getParameter("deptNo"));
        //调用业务逻辑
        DeptService deptService = new DeptServiceImpl();
        try {
            //调用删除的方法
            deptService.deleteDeptByNo(deptNo);
            //返回的是查询之后的FindDeptServlet里边的deptList
            response.sendRedirect("deptList");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}

servlet--DeleteDeptServlet    查询所有

package com.qf.webpro.servlets;

import com.qf.webpro.pojo.Dept;
import com.qf.webpro.service.DeptService;
import com.qf.webpro.service.impl.DeptServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

//查询的servlet

@WebServlet(name = "FindDeptServlet",value = "/deptList")
public class FindDeptServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、获取表单数据  因为是无条件查询,所以获取表单数据这一步可以省略

        //2、调用业务逻辑·
        DeptService deptService = new DeptServiceImpl();
        try {
            //得到集合
            List list = deptService.findDept();
            //把得到的集合使用request存储
            request.setAttribute("depts",list);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        //因为要把数据往页面里传是一次传值
        // 所以要转发跳转界面
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("deptlist.jsp");
        requestDispatcher.forward(request,response);
    }
}

servlet--QueryDeptServlet    模糊查询

package com.qf.webpro.servlets;

import com.qf.webpro.pojo.Dept;
import com.qf.webpro.service.DeptService;
import com.qf.webpro.service.impl.DeptServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

//迷糊查询
@WebServlet(name = "QueryDeptServlet",value = "/queryDept")
public class QueryDeptServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取表单数据
        Dept dept = new Dept();
        dept.setdName(request.getParameter("dName"));
        dept.setLoc(request.getParameter("loc"));
        //调整业务逻辑
        DeptService deptService = new DeptServiceImpl();
        try {
            //用集合因为查到多少条不确定
            List  list = deptService.findDeptByCondition(dept);
            request.setAttribute("depts",list);
            request.getRequestDispatcher("deptlist.jsp").forward(request,response);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

修改=添加+查询

servlet--UpdateDeptServlet    针对于修改的添加

package com.qf.webpro.servlets;

import com.qf.webpro.pojo.Dept;
import com.qf.webpro.service.DeptService;
import com.qf.webpro.service.impl.DeptServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

//修改的添加
@WebServlet(name = "UpdateDeptServlet",value = "/uds")
public class UpdateDeptServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取表单数据
        Dept dept = new Dept();
        //赋值隐藏域
        dept.setDeptNo(Integer.parseInt(request.getParameter("deptNo")));
        dept.setdName(request.getParameter("dName"));
        dept.setLoc(request.getParameter("loc"));
        //调整业务逻辑
        DeptService deptService = new DeptServiceImpl();
        try {
            deptService.editDept(dept);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //请求查询
        response.sendRedirect("deptList");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

servlet--QueryDeptServlet    针对于修改的查询

package com.qf.webpro.servlets;

import com.qf.webpro.pojo.Users;
import com.qf.webpro.service.UserService;
import com.qf.webpro.service.impl.UserServiceimpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

//针对于修改的查询
@WebServlet(value = "/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取表单数据
        String userId = request.getParameter("userId");
        String pwd = request.getParameter("pwd");
        Users user = new Users(userId,pwd,"");

        //调整业务逻辑
        //new对象为了调用方法
        UserService userService = new UserServiceimpl();
        //获取数据库得到的对象 u
       Users users1 =  userService.Login(user);

        //----------------dao pojo service 全走完了获取到结果传给了users

        //获取数据之后是空的时候
        //进行判断不同的点击之下出现不同的情况
        if(users1==null){
            //跳转回登陆页面
            //用重定向的方式进行跳转回登陆界面
            response.sendRedirect("Login.html");
        }else{
            //获取自动登录的那个数据 用String来接收
            String getResult = request.getParameter("isAutoLogin");
            //得到的值是否为空 怕出空指针 不点击就是空指针
            //可以写if-else 但是先是null后是on
            //因为不点击直接就是null点击是on 先是on就出错
            if(getResult != null){
                if(getResult.equals("on")){
//                    Cookie里的值是key-value的形式,
//                    key的值随意命名 value只能是String类型的,一般取id就可以
//                    id的值先添加到cookie里边再添加到客户端浏览器里边
//                    点击勾选然后登录之后把账号存进里边,每个人的账号(Id)是唯一的
//                    存进去就证明曾经已经登陆过,然后session读取的时候就只是读取到
//                    cookie里边有id就证明登陆过,那么就实现了免密登录
                    Cookie cookie = new Cookie("web",users1.getUserId());
                    //设置Cookie的有效期,默认是浏览器关闭就失效
                    //时间以秒为单位
                    cookie.setMaxAge(60*60);
                    //cookie.setPath("/");是存x储路径,一般不用写,默认
                    //将cookie赋的值添加到客户端里边
                    response.addCookie(cookie);
                }

            }
            //点不点击cookie  session都存值,存的是数据库里的值,存值的目的是在jsp里边进行比较 将数据库的与表单传的进行比较
                //使用session存储信息
            //1、获取session
            HttpSession session = request.getSession();
            //2、session存储用户信息
            //key-value value是Object类型
            session.setAttribute("userInfo",users1);
            //session默认有效期为30分钟
            session.setMaxInactiveInterval(60*60);
            System.out.println("登录的servlet中获取了session,sessionID=" + session.getId());
            response.sendRedirect("index.jsp");
        }

        //跳转页面  重定向
        //需要转发来跳转页面
//        RequestDispatcher requestDispatcher = request.getRequestDispatcher("index.jsp");
//        requestDispatcher.forward(request,response);
//        response.sendRedirect("index.jsp");

    }




    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

util--DBUtil    工具类

package com.qf.webpro.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.dbutils.DbUtils;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class DBUtil {
    //声明局部线程类:保存自己的连接
    //类map集合  key是线程id  value 是connection对象
    private static ThreadLocal threadLocal = new ThreadLocal();
    //声明连接池对象:提升获取连接的效率
    private static DruidDataSource dataSource;

    static {
        InputStream inputStream = DbUtils.class.getResourceAsStream("/db.properties");
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
            //创建连接池对象
            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        //到连接池中获取连接
        //第一步:先到局部线程类中获取自己原有的连接
        //第二步:判断是否获取到,如果获取到,则使用获取到的,如果没有获取到自己原来的连接,那么到连接池获取
        Connection connection = threadLocal.get();
        if (connection == null) {
            //没有获取到自己线程使用的connection对象

            try {
                //从连接池获取
                connection = dataSource.getConnection();
                //存入局部线程
                threadLocal.set(connection);
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return connection;
    }

    //返回连接池对象
    public static DataSource getDataSource() {
        return dataSource;
    }
}

 db.properties  用来给数据库连接的properties文件用于Util

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db2105pro
username=root
password=root

#连接池中初始化的数量
initialSize=10
#最大连接数
maxActive=30
#最小连接数量
minIdle=5
#最大等待时间
maxWa

filter--LoginFilter    登录的servlet

package com.qf.webpro.filters;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "LoginFilter",value = "/*")
public class LoginFilter implements Filter {
    //设置一个白名单:这里的界面不拦截,保证刚加载的时候会出现必要的界面
    //前者写的是页面,后者写的是action是login的
    String[] ary = {"Login.html","login",};

    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
//        //将其转换成子类类型 ----- 这步叫提升类型
          //因为子类有更多的方法供我们使用
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)resp;
        //获取网络请求
        //完整的网络请求  协议  域名  资源
//        System.out.println(request.getRequestURL());
        //资源
//        System.out.println(request.getRequestURI());
        //判断用户的网络请求是否在数组中
        //在白名单里边就不需要拦截,直接让他跳转
        //不在白名单里边就需要拦截让他登录
        String url=request.getRequestURI();
        boolean isPass = false;
        //循环遍历数组
        for(String str:ary){
            //indeexof就是找是否存在完全相同的值只要不一样就是-1
            //不等于-1说明找到了
            if(url.indexOf(str)!=-1){
                //设为true
                    isPass=true;
                    break;
            }
        }

        if(isPass){
            //用户请求的是登录或注册相关,应该放行
            chain.doFilter(request, response);
        }else{
            //获取session的值
            HttpSession session = request.getSession();
            Object obj = session.getAttribute("userInfo");
            if(obj == null){
                response.sendRedirect("Login.html");
            }else{
                chain.doFilter(request, response);
            }
        }

    }

    public void init(FilterConfig config) throws ServletException {

    }

}

filter--CharacterSetFilter   对于存数据进行转换成中文的servlet

package com.qf.webpro.filters;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
//设置编码格式
@WebFilter(filterName = "CharacterSetFilter",value = "/*")
public class CharacterSetFilter implements Filter {
    public void destroy() {
    }
    //当拦截到请求时,就会执行doFileter里的方法
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        //设置编码格式
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        //对请求放行,放行后,就到下一个过滤器
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}

web.xml    配置登录信息



    
    
        Login.html
    



adddept.html    点击添加部门后跳转的页面




    
    部门添加


     

部门编号:

部门名称:

部门位置:

deptlist.html    部门主页




    
    部门主页




deptlist.jsp    点击部门主页之后的页面

<%@ page import="java.util.List" %>
<%@ page import="com.qf.webpro.pojo.Dept" %><%--
  Created by IntelliJ IDEA.
  User: poison
  Date: 2022/3/12
  Time: 16:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    部门列表
    
    <%
        //得到获取的值
        Object object = request.getAttribute("depts");
        List list = null;
        if(object != null){
            list = (List) object;
        }
    %>
    


部门名称:      部门位置:            


<% for(Dept dept : list){ %> <% } %>
部门编号 部门名称 部门位置 操作
<%=dept.getDeptNo()%> <%=dept.getdName()%> <%=dept.getLoc()%> 删除 修改

editdept.jsp    选择修改时

<%@ page import="com.qf.webpro.pojo.Dept" %><%--
  Created by IntelliJ IDEA.
  User: poison
  Date: 2022/3/12
  Time: 18:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    部门修改页面
    <%
        //获取 request.setAttribute("dept",dept)中的value
        Object object = request.getAttribute("dept");
        Dept dept = (Dept)object;
    %>


<%-- value写值代表回显--%> <%-- <%=dept.getDeptNo()%>是因为不让编号进行编辑 但是这样就不能获取deptNo了 往后边的传值有影响-%> <%-- 是加一个隐藏域 让他不显示但是能获取--%>

部门编号:<%=dept.getDeptNo()%>

部门名称:

部门位置:

index.jsp    登陆界面    账号密码输入正确之后的界面

<%@ page import="com.qf.webpro.pojo.Users" %><%--
  Created by IntelliJ IDEA.
  User: poison
  Date: 2022/3/11
  Time: 8:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

  
    $Title$
<%
  //创建一个id 进行赋值
    String userId = "";

    //读取cookie  直接getCookies是浏览器Cookie里所有的东西 Cookie目录只有一个东西都放里边
    //这个项目所有的Cookie全部都得到
    Cookie[] ary = request.getCookies();
    //遍历数组查找名字叫web2105的cookie
    if(ary != null){
      for(Cookie cookie : ary){
        //判断的是key是否相等
        if(cookie.getName().equals("web")){
          //获取cookie的值
          userId = cookie.getValue();
          //将得到的值存进session里边

          //首先创建一个User new出来
          Users users = new Users();
          //然后给set进行赋值
          //传进去的值就是从Cookie里边得到的id 因为我的Cookie里边只有id
          users.setUserId(userId);

          //给session赋值
          //指定给那个session进行赋值
          //第一个指定的是session的名称 第二个转的是Users类型的users
          session.setAttribute("userInfo",users);
          break;
        }
      }

    }
    //然后通过session判断是否登录
  boolean isLogin = false;
    //读取session进行取值 取的是uesrInfo里的值
    //Object类型进行接收
  Object obj = session.getAttribute("userInfo");
    //判断是否为空  空证明是false证明数据库里没找到这个值 那就跳转到登陆界面
  isLogin =  obj==null?false:true;

%>
  
  
<%
  if(isLogin == false){
    //跳转到登陆界面
    response.sendRedirect("Login.html");
  }else{
    //强转
    //将取出的obj强转为Users类型

    Users user = (Users)obj;
%>

欢迎你:<%=user.getUserId()%>

<% } %> 部门主页

Login.jsp    输入账号密码的登陆界面




    
    Title



账户:

密码:

下次自动登录

 数据库

Dept

MVC综合案例_第3张图片

MVC综合案例_第4张图片

 Users

MVC综合案例_第5张图片

MVC综合案例_第6张图片

mvc程序结构图

MVC综合案例_第7张图片

修改原理

MVC综合案例_第8张图片

MVC综合案例_第9张图片

MVC综合案例_第10张图片

 MVC综合案例_第11张图片

你可能感兴趣的:(Synthesize,java,开发语言)