完成一个IDEA web项目(一)项目搭建准备工作
2021-02-13 08:19
标签:pack dea 释放 字符 字符编码 网站 lan import character servlet,jsp,mysql,jstl依赖 去这个网站找:https://mvnrepository.com/ 项目结构(忽略我的jsp和html文件) 数据库配置文件 (我的编码全是gbk) 编写数据库的公共类BaseDao 编写字符编码过滤器 js,css等 CharacterEncodingFilter 完成一个IDEA web项目(一)项目搭建准备工作 标签:pack dea 释放 字符 字符编码 网站 lan import character 原文地址:https://www.cnblogs.com/musecho/p/13021554.html一、 项目搭建准备工作
1. 创建maven web项目,配置Tomcat
2. 测试项目是否跑得起来
3. 导入项目中会遇到的jar包(maven依赖)
4. 创建项目包结构
5. 连接数据库
6. 编写实体类
7. 编写基础公共类
driver = com.mysql.jdbc.Driver
url= jdbc:mysql://localhost:3306/TutorSelectSys?useUnicode=true&characterEncoding=gbk
user = root
password = root
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
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. 导入静态资源
9. 编写字符编码过滤器(哪步写都行)
@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() {
}
}
上一篇:css常用选择器
下一篇:js封装cookie
文章标题:完成一个IDEA web项目(一)项目搭建准备工作
文章链接:http://soscw.com/index.php/essay/54801.html