我们之前3月一直在做spring全家桶范围的项目测试和开发,4月我们将缓存,java多线程,安全框架进行了系列的学习,对于这个项目测试尽管工作量是一个人完成的小型学习与练习式测试,但是我们对于基本的jdbc的API实现的数据层操作与javaweb的Servlet配置与写法也更加熟悉,同时做完这个javaweb的项目开发,我也深刻的理解到自从spring框架来临的时候,广大程序员们精神上的解放。

开发步骤:

1、导入需要的依赖

2、手写一个封装好的JDBC工具包,让我们能够直接调用相应的api实现数据库操作

3、设计实体类

4、设计dao层与其实现类

5、设计各个业务的servlet类

6、配置servlet和filter过滤器解决前后端数据流动期间的中文乱码问题

1、导入需要的依赖

          javax.servlet      servlet-api      2.5              javax.servlet.jsp      jsp-api      2.2              com.mchange      c3p0      0.9.5.2              javax.servlet      jstl      1.2              mysql      mysql-connector-java      8.0.28              org.projectlombok      lombok      1.18.24    

2、手写一个封装好的JDBC工具包,让我们能够直接调用相应的api实现数据库操作

public class JDBC {   static String driver = "com.mysql.cj.jdbc.Driver";   static String url = "jdbc:mysql://localhost:3306/test";   static String username = "root";   static String password = "123456";   static Connection connection = null;/*链接数据库方法*/    public static void init() throws Exception{        ComboPooledDataSource dataSource = new ComboPooledDataSource();        dataSource.setDriverClass(driver);        dataSource.setJdbcUrl(url);        dataSource.setUser(username);        dataSource.setPassword(password);        connection = dataSource.getConnection();    }/*实现查询方法*/    public static ResultSet select(String sql) throws Exception{        PreparedStatement ps = connection.prepareStatement(sql);        ResultSet resultSet = ps.executeQuery(sql);        return resultSet;    }/*实现插入修改删除等方法*   这么设置的原因是在动态的statement对象方法里*   修改的方法也可以执行删除与增加操作,他们的性质差不多,可以通过组合产生一样的效果*/    public static  int update(String sql) throws Exception{        int i = 0;        PreparedStatement ps = connection.prepareStatement(sql);        i = ps.executeUpdate();        return i;    }/*关闭资源方法*/    public static  void shutCon()throws Exception{        connection.close();    }}

3、设计实体类

@Datapublic class User {    private String name;    private int age;}

4、设计dao层与其实现类

/*定义方法接口*/public interface UserDao {    List findAll() throws Exception;    Boolean login(String name,int age) throws Exception;    Boolean delete(String name) throws Exception;    Boolean save(User user) throws Exception;}public class UserDaoImpl implements UserDao{    public List findAll() throws Exception {        JDBC.init();        ResultSet resultSet = JDBC.select("select * from user");        List userList = (List) resultSet;        JDBC.shutCon();        return userList;    }    public Boolean login(String name, int age) throws Exception {        JDBC.init();        ResultSet resultSet = JDBC.select("select * from user where name='" + name + "' and age='" + age + "'");        JDBC.shutCon();        return resultSet==null ? false : true;    }    public Boolean delete(String name) throws Exception {        JDBC.init();        int i = JDBC.update("delete from user where name='" + name + "' ");        JDBC.shutCon();        return i==0?false:true;    }    public Boolean save(User user) throws Exception {        JDBC.init();        int i = JDBC.update("insert into user values('" + user.getName() + "','" + user.getAge() + "')");        JDBC.shutCon();        return i==0?false:true;    }}

5、设计各个业务的servlet类

登录:

public class login extends HttpServlet {    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        doPost(req,resp);    }    @SneakyThrows    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        String name = req.getParameter("name");        int age = Integer.parseInt(req.getParameter("age"));        UserDao userDao = new UserDaoImpl();        if(userDao.login(name,age)){            /*转发成功页面*/            req.getRequestDispatcher("/success.jsp").forward(req,resp);        }else {            /*登不上一直留在这*/            resp.sendRedirect("index.jsp");        }    }}

注册:

public class save extends HttpServlet {    @Override    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        doPost(request,response);    }    @SneakyThrows    @Override    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        User user = new User();        user.setName(request.getParameter("name"));        user.setAge(Integer.parseInt(request.getParameter("age")));        UserDao userDao = new UserDaoImpl();        Boolean save = userDao.save(user);        if (save){            request.getRequestDispatcher("/login.jsp").forward(request,response);        }else {            response.sendRedirect("error.jsp");        }    }}

删除:

public class delete extends HttpServlet {    @Override    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    }    @SneakyThrows    @Override    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        String name = request.getParameter("name");        UserDao userDao = new UserDaoImpl();        Boolean delete = userDao.delete(name);        if (delete){            request.getRequestDispatcher("/List.jsp").forward(request,response);        }else{            response.sendRedirect("delete.jsp");        }    }}

展示全部:

public class allList extends HttpServlet {    @Override    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        doPost(request,response);    }    @SneakyThrows    @Override    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        UserDao userDao = new UserDaoImpl();        List userList = userDao.findAll();        request.setAttribute("userList",userList);    }}

6、配置servlet和filter过滤器解决前后端数据流动期间的中文乱码问题

web.xml文件配置:

         filter        com.javaweb.untils.filter                filter        /*                allList        com.javaweb.web.allList                allList        /allList                delete        com.javaweb.web.delete                delete        /delete                save        com.javaweb.web.save                save        /save                login        com.javaweb.web.login                login        /login    

乱码解决:配置filter类

public class filter implements Filter {    public void destroy() {        System.out.println("过滤器销毁");    }    public void init(FilterConfig filterConfig) throws ServletException {        System.out.println("过滤器初始化");    }    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {        //1.解决response输出流的乱码问题        req.setCharacterEncoding("utf-8");        //2.解决post请求中的中文信息乱码问题        //2.1 设置获取流的编码格式        resp.setCharacterEncoding("utf-8");        //2.2 告知浏览器我们输出的文件类型以及编码格式        resp.setContentType("text/html;utf-8");        chain.doFilter(req, resp);    }}

至于前端页面大家自己按照我的api配吧!这个月努力学习数据结构与算法以及我没有学完的jvm。感觉工作压力挺大的,因为我不知道到底要学到什么程度才能参加工作,虽然java方面开发以及解决问题已经熟练了,但是还是觉得不够,还需要沉淀。