标签:pack dea 释放 字符 字符编码 网站 lan import character
servlet,jsp,mysql,jstl依赖
去这个网站找:https://mvnrepository.com/
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- JSTL表达式的依赖 -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- standard标签库 -->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency>
</dependencies>
项目结构(忽略我的jsp和html文件)
数据库配置文件
(我的编码全是gbk)
driver = com.mysql.jdbc.Driver
url= jdbc:mysql://localhost:3306/TutorSelectSys?useUnicode=true&characterEncoding=gbk
user = root
password = root
编写数据库的公共类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 < params.length; i++) {
//注意:setObject占位符从1开始,数组从0开始
ps.setObject(i + 1, params[i]);
}
rs = ps.executeQuery();
return rs;
}
//增删改公共方法
public static int executeUpdate(Connection conn, String sql, Object[] params, PreparedStatement ps) throws SQLException {
ps = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
//注意:setObject占位符从1开始,数组从0开始
ps.setObject(i + 1, params[i]);
}
int updateRows = ps.executeUpdate();
return updateRows;
}
//释放资源
public static boolean closeResource(Connection conn, PreparedStatement ps, ResultSet rs) {
boolean flag = true;
if (rs != null) {
try {
rs.close();
//GC回收
rs = null;
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
if (ps != null) {
try {
ps.close();
//GC回收
ps = null;
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
if (conn != null) {
try {
conn.close();
//GC回收
conn = null;
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
return flag;
}
}
编写字符编码过滤器
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() {
}
}
js,css等
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() {
}
}
标签:pack dea 释放 字符 字符编码 网站 lan import character
原文地址:https://www.cnblogs.com/musecho/p/13021554.html