完成一个IDEA web项目(一)项目搭建准备工作

2021-02-13 08:19

阅读:393

标签:pack   dea   释放   字符   字符编码   网站   lan   import   character   

一、 项目搭建准备工作


1. 创建maven web项目,配置Tomcat

  • 具体见博文

2. 测试项目是否跑得起来


3. 导入项目中会遇到的jar包(maven依赖)

  • servlet,jsp,mysql,jstl依赖

    去这个网站找:https://mvnrepository.com/

    javax.servlet
            servlet-api
            2.5javax.servlet.jsp
            javax.servlet.jsp-api
            2.3.3mysql
            mysql-connector-java
            5.1.47javax.servlet.jsp.jstl
            jstl
            1.2org.apache.taglibs
            taglibs-standard-impl
            1.2.5

4. 创建项目包结构

  • 项目结构(忽略我的jsp和html文件)

    技术图片


5. 连接数据库


6. 编写实体类


7. 编写基础公共类

  1. 数据库配置文件

    (我的编码全是gbk)

    driver = com.mysql.jdbc.Driver
    url= jdbc:mysql://localhost:3306/TutorSelectSys?useUnicode=true&characterEncoding=gbk
    user = root
    password = root
    
  2. 编写数据库的公共类BaseDao

    package 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 user;
        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();
            }
    
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");
        }
    
        //获取数据库的链接
        public static Connection getConnection() {
            Connection conn = null;
            try {
                Class.forName(driver);
                conn = DriverManager.getConnection(url, user, password);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return conn;
        }
    
        //查询公共方法
        public static ResultSet executeQuery(Connection conn, String sql, Object[] params, PreparedStatement ps, ResultSet rs) throws SQLException {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i 

  3. 编写字符编码过滤器

    package filter;
    
    import javax.servlet.*;
    import javax.servlet.annotation.WebFilter;
    import java.io.IOException;
    
    /**
     * 字符编码过滤器
     */
    @WebFilter("/*")
    public class CharacterEncodingFilter implements Filter {
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            servletRequest.setCharacterEncoding("gbk");
            servletResponse.setCharacterEncoding("gbk");
    
            filterChain.doFilter(servletRequest,servletResponse);
        }
    
        @Override
        public void destroy() {
    
        }
    }
    

8. 导入静态资源

js,css等


9. 编写字符编码过滤器(哪步写都行)

  • CharacterEncodingFilter

    @WebFilter("/*")
    public class CharacterEncodingFilter implements Filter {
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            servletRequest.setCharacterEncoding("gbk");
            servletResponse.setCharacterEncoding("gbk");
    
            filterChain.doFilter(servletRequest,servletResponse);
        }
    
        @Override
        public void destroy() {
    
        }
    }
    

完成一个IDEA web项目(一)项目搭建准备工作

标签:pack   dea   释放   字符   字符编码   网站   lan   import   character   

原文地址:https://www.cnblogs.com/musecho/p/13021554.html

上一篇:css常用选择器

下一篇:js封装cookie


评论


亲,登录后才可以留言!