JavaWeb项目搭建准备工作----------狂神说Java

项目搭建准备工作

一、搭建一个Maven项目

二、配置Tomcat

三、测试项目是否可以跑起来

四、导入jar包

             jsp、Servlet、mysql-connection、jstl、standard

五、创建项目包结构

    pojo、servlet、service、util、Filter等

六、编写实体类

ORM映射:表--类映射

七、编写公共基础类

1、数据库配置文件

driver  =com.mysql.cj.jdbc.Driver
url     =jdbc:mysql://localhost:3306/mydb1?serverTimezone=UTC
username=root
password=123456

2、读取文件

        Properties properties = new Properties();
        //通过类加载器读取相应的资源
        InputStream is =              BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            properties.load(is);
​
        } catch (IOException e) {
            e.printStackTrace();
        }
        String driver=properties.getProperty("driver");
        String url = properties.getProperty("url");
        String username = properties.getProperty("username");
        String password = properties.getProperty("password");

3、编写公共类

package com.llf.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 {
        Properties properties = new Properties();
        //通过类加载器读取相应的资源
        InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            properties.load(is);
​
        } catch (IOException e) {
            e.printStackTrace();
        }
        String driver=properties.getProperty("driver");
        String url = properties.getProperty("url");
        String username = properties.getProperty("username");
        String 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 executeQuery( Connection connection,String sql, Object[] parms,ResultSet resultSet,
                                          PreparedStatement preparedStatement ) throws SQLException {
        preparedStatement = connection.prepareStatement(sql);
        // 因为占位符是从1开始的 所以我们使I的值从1开始
        for (int i = 1; i <parms.length; i++) {
            preparedStatement.setObject(i,parms[i]);
        }
​
        resultSet = preparedStatement.executeQuery();
        return resultSet;
​
​
    }
    //编写更新公共类
    public static int executeUpdate( Connection connection,String sql, Object[] parms,ResultSet resultSet,
                                     PreparedStatement preparedStatement ) throws SQLException {
        preparedStatement = connection.prepareStatement(sql);
        // 因为占位符是从1开始的 所以我们使I的值从1开始
        for (int i = 1; i <parms.length; i++) {
            preparedStatement.setObject(i,parms[i]);
        }
​
        int updateRows = preparedStatement.executeUpdate();
        return updateRows;
​
​
    }
    //关闭连接 释放资源
    public   static  boolean closeResourse (Connection connection,ResultSet resultSet,PreparedStatement preparedStatement) {
        boolean flag = true;
        if (resultSet != null) {
            try {
                resultSet.close();
                //GC垃圾清理  回收
                resultSet = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
             //失败了就返回false
                flag = false;
            }
​
        }
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
                //GC垃圾清理  回收
                preparedStatement = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                //失败了就返回false
                flag = false;
            }
​
        }
        if (connection != null) {
            try {
                connection.close();
                //GC垃圾清理  回收
                connection = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                //失败了就返回false
                flag = false;
            }
​
        }
        return flag;
​
    }
}
​

 

4、编写字符编码过滤器

package com.llf.filter;
​
import javax.servlet.*;
import java.io.IOException;
​
public class CharacterEncodingFilter implements Filter {
    @Override
    // web服务器开启  过滤器初始化
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("初始化");
    }
​
    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
        req.setCharacterEncoding("Utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        System.out.println("过滤前");
       // 它的作用相当于保证程序可以继续运行
        filterChain.doFilter(req,resp);
        System.out.println("过滤后");
    }
​
    @Override
    // web服务器关闭 过滤器销毁
    public void destroy() {
        System.out.println("已销毁");
    }
}

 

八、导入静态资源

导入项目所需要的各种css文件、图片等静态资源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值