JavaWeb25.2【综合案例:properties & util & filter & pom】
druid.properties

1 driverClassName=com.mysql.jdbc.Driver 2 url=jdbc:mysql://localhost:3306/hm_travel 3 username=root 4 password=root 5 initialSize=5 6 maxActive=10 7 maxWait=3000
jedis.properties

1 host=127.0.0.1 2 port=6379 3 maxTotal=50 4 maxIdle=10
CharchaterFilter

1 package cn.haifei.travel.web.filter; 2 3 import javax.servlet.*; 4 import javax.servlet.annotation.WebFilter; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 import java.io.IOException; 8 9 /** 10 * 解决全站乱码问题,处理所有的请求 11 */ 12 @WebFilter("/*") 13 public class CharchaterFilter implements Filter { 14 @Override 15 public void init(FilterConfig filterConfig) throws ServletException { 16 17 } 18 19 @Override 20 public void doFilter(ServletRequest req, ServletResponse rep, FilterChain filterChain) throws IOException, ServletException { 21 //将父接口转为子接口 22 HttpServletRequest request = (HttpServletRequest) req; 23 HttpServletResponse response = (HttpServletResponse) rep; 24 //获取请求方法 25 String method = request.getMethod(); 26 //解决post请求中文数据乱码问题 27 if(method.equalsIgnoreCase("post")){ 28 request.setCharacterEncoding("utf-8"); 29 } 30 //处理响应乱码 31 response.setContentType("text/html;charset=utf-8"); 32 filterChain.doFilter(request,response); 33 } 34 35 @Override 36 public void destroy() { 37 38 } 39 }
JDBCUtils

1 package cn.haifei.travel.util; 2 3 import com.alibaba.druid.pool.DruidDataSourceFactory; 4 5 import javax.sql.DataSource; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.sql.Connection; 9 import java.sql.ResultSet; 10 import java.sql.SQLException; 11 import java.sql.Statement; 12 import java.util.Properties; 13 14 /* 15 1. 声明静态数据源成员变量 16 2. 创建连接池对象 17 3. 定义公有的得到数据源的方法 18 4. 定义得到连接对象的方法 19 5. 定义关闭资源的方法 20 */ 21 public class JDBCUtils { 22 // 1. 声明静态数据源成员变量 23 private static DataSource ds; 24 25 // 2. 创建连接池对象 26 static { 27 // 加载配置文件中的数据 28 InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"); 29 Properties pp = new Properties(); 30 try { 31 pp.load(is); 32 // 创建连接池,使用配置文件中的参数 33 ds = DruidDataSourceFactory.createDataSource(pp); 34 } catch (IOException e) { 35 e.printStackTrace(); 36 } catch (Exception e) { 37 e.printStackTrace(); 38 } 39 } 40 41 // 3. 定义公有的得到数据源的方法 42 public static DataSource getDataSource() { 43 return ds; 44 } 45 46 // 4. 定义得到连接对象的方法 47 public static Connection getConnection() throws SQLException { 48 return ds.getConnection(); 49 } 50 51 // 5.定义关闭资源的方法 52 public static void close(Connection conn, Statement stmt, ResultSet rs) { 53 if (rs != null) { 54 try { 55 rs.close(); 56 } catch (SQLException e) {} 57 } 58 59 if (stmt != null) { 60 try { 61 stmt.close(); 62 } catch (SQLException e) {} 63 } 64 65 if (conn != null) { 66 try { 67 conn.close(); 68 } catch (SQLException e) {} 69 } 70 } 71 72 // 6.重载关闭方法 73 public static void close(Connection conn, Statement stmt) { 74 close(conn, stmt, null); 75 } 76 }
JedisUtil

1 package cn.haifei.travel.util; 2 3 import redis.clients.jedis.Jedis; 4 import redis.clients.jedis.JedisPool; 5 import redis.clients.jedis.JedisPoolConfig; 6 7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.util.Properties; 10 11 /** 12 * Jedis工具类 13 */ 14 public final class JedisUtil { 15 private static JedisPool jedisPool; 16 17 static { 18 //读取配置文件 19 InputStream is = JedisPool.class.getClassLoader().getResourceAsStream("jedis.properties"); 20 //创建Properties对象 21 Properties pro = new Properties(); 22 //关联文件 23 try { 24 pro.load(is); 25 } catch (IOException e) { 26 e.printStackTrace(); 27 } 28 //获取数据,设置到JedisPoolConfig中 29 JedisPoolConfig config = new JedisPoolConfig(); 30 config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal"))); 31 config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle"))); 32 33 //初始化JedisPool 34 jedisPool = new JedisPool(config, pro.getProperty("host"), Integer.parseInt(pro.getProperty("port"))); 35 36 37 } 38 39 40 /** 41 * 获取连接方法 42 */ 43 public static Jedis getJedis() { 44 return jedisPool.getResource(); 45 } 46 47 /** 48 * 关闭Jedis 49 */ 50 public static void close(Jedis jedis) { 51 if (jedis != null) { 52 jedis.close(); 53 } 54 } 55 }
MailUtils

1 package cn.haifei.travel.util; 2 3 import javax.mail.*; 4 import javax.mail.internet.InternetAddress; 5 import javax.mail.internet.MimeMessage; 6 import java.util.Properties; 7 8 /** 9 * 发邮件工具类 10 */ 11 public final class MailUtils { 12 private static final String USER = "yub4by@qq.com"; // 发件人称号,同邮箱地址 13 private static final String PASSWORD = ""; // 如果是qq邮箱可以使户端授权码,或者登录密码 14 15 /** 16 * 17 * @param to 收件人邮箱 18 * @param text 邮件正文 19 * @param title 标题 20 */ 21 /* 发送验证信息的邮件 */ 22 public static boolean sendMail(String to, String text, String title){ 23 try { 24 final Properties props = new Properties(); 25 props.put("mail.smtp.auth", "true"); 26 props.put("mail.smtp.host", "smtp.qq.com"); 27 28 // 发件人的账号 29 props.put("mail.user", USER); 30 //发件人的密码 31 props.put("mail.password", PASSWORD); 32 33 // 构建授权信息,用于进行SMTP进行身份验证 34 Authenticator authenticator = new Authenticator() { 35 @Override 36 protected PasswordAuthentication getPasswordAuthentication() { 37 // 用户名、密码 38 String userName = props.getProperty("mail.user"); 39 String password = props.getProperty("mail.password"); 40 return new PasswordAuthentication(userName, password); 41 } 42 }; 43 // 使用环境属性和授权信息,创建邮件会话 44 Session mailSession = Session.getInstance(props, authenticator); 45 // 创建邮件消息 46 MimeMessage message = new MimeMessage(mailSession); 47 // 设置发件人 48 String username = props.getProperty("mail.user"); 49 InternetAddress form = new InternetAddress(username); 50 message.setFrom(form); 51 52 // 设置收件人 53 InternetAddress toAddress = new InternetAddress(to); 54 message.setRecipient(Message.RecipientType.TO, toAddress); 55 56 // 设置邮件标题 57 message.setSubject(title); 58 59 // 设置邮件的内容体 60 message.setContent(text, "text/html;charset=UTF-8"); 61 // 发送邮件 62 Transport.send(message); 63 return true; 64 }catch (Exception e){ 65 e.printStackTrace(); 66 } 67 return false; 68 } 69 70 public static void main(String[] args) throws Exception { // 做测试用 71 MailUtils.sendMail("1196006753@qq.com","你好,这是一封测试邮件,无需回复。","测试邮件"); 72 System.out.println("发送成功"); 73 } 74 75 }
Md5Util

1 package cn.haifei.travel.util; 2 3 import java.security.MessageDigest; 4 5 /** 6 * 写一个MD5算法,运行结果与MySQL的md5()函数相同 7 * 将明文密码转成MD5密码 8 * 123456->e10adc3949ba59abbe56e057f20f883e 9 */ 10 public final class Md5Util { 11 12 private Md5Util(){} 13 14 /** 15 * 将明文密码转成MD5密码 16 */ 17 public static String encodeByMd5(String password) throws Exception{ 18 //Java中MessageDigest类封装了MD5和SHA算法,今天我们只要MD5算法 19 MessageDigest md5 = MessageDigest.getInstance("MD5"); 20 //调用MD5算法,即返回16个byte类型的值 21 byte[] byteArray = md5.digest(password.getBytes()); 22 //注意:MessageDigest只能将String转成byte[],接下来的事情,由我们程序员来完成 23 return byteArrayToHexString(byteArray); 24 } 25 26 /** 27 * 将byte[]转在16进制字符串 28 */ 29 private static String byteArrayToHexString(byte[] byteArray) { 30 StringBuffer sb = new StringBuffer(); 31 //遍历 32 for(byte b : byteArray){//16次 33 //取出每一个byte类型,进行转换 34 String hex = byteToHexString(b); 35 //将转换后的值放入StringBuffer中 36 sb.append(hex); 37 } 38 return sb.toString(); 39 } 40 41 /** 42 * 将byte转在16进制字符串 43 */ 44 private static String byteToHexString(byte b) {//-31转成e1,10转成0a,。。。 45 //将byte类型赋给int类型 46 int n = b; 47 //如果n是负数 48 if(n < 0){ 49 //转正数 50 //-31的16进制数,等价于求225的16进制数 51 n = 256 + n; 52 } 53 //商(14),数组的下标 54 int d1 = n / 16; 55 //余(1),数组的下标 56 int d2 = n % 16; 57 //通过下标取值 58 return hex[d1] + hex[d2]; 59 } 60 61 private static String[] hex = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}; 62 63 /** 64 * 测试 65 */ 66 public static void main(String[] args) throws Exception{ 67 String password = "123456"; 68 String passwordMD5 = Md5Util.encodeByMd5(password); 69 System.out.println(password); 70 System.out.println(passwordMD5); 71 } 72 }
UuidUtil

1 package cn.haifei.travel.util; 2 3 import java.util.UUID; 4 5 /** 6 * 产生UUID随机字符串工具类 7 */ 8 public final class UuidUtil { 9 10 private UuidUtil(){} 11 12 public static String getUuid(){ 13 return UUID.randomUUID().toString().replace("-",""); 14 } 15 16 /** 17 * 测试 18 */ 19 public static void main(String[] args) { 20 System.out.println(UuidUtil.getUuid()); 21 System.out.println(UuidUtil.getUuid()); 22 System.out.println(UuidUtil.getUuid()); 23 System.out.println(UuidUtil.getUuid()); 24 } 25 }
pom.xml

1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 8 <groupId>cn.haifei</groupId> 9 <artifactId>travel</artifactId> 10 <version>1.0-SNAPSHOT</version> 11 <packaging>war</packaging> 12 13 14 <dependencies> 15 <dependency> 16 <groupId>junit</groupId> 17 <artifactId>junit</artifactId> 18 <version>3.8.1</version> 19 <scope>test</scope> 20 </dependency> 21 <!--servlet--> 22 <dependency> 23 <groupId>javax.servlet</groupId> 24 <artifactId>javax.servlet-api</artifactId> 25 <version>3.1.0</version> 26 <scope>provided</scope> 27 </dependency> 28 <!--mysql驱动--> 29 <dependency> 30 <groupId>mysql</groupId> 31 <artifactId>mysql-connector-java</artifactId> 32 <version>5.1.26</version> 33 <scope>compile</scope> 34 </dependency> 35 <!--druid连接池--> 36 <dependency> 37 <groupId>com.alibaba</groupId> 38 <artifactId>druid</artifactId> 39 <version>1.0.9</version> 40 </dependency> 41 <!--jdbcTemplate--> 42 <dependency> 43 <groupId>org.springframework</groupId> 44 <artifactId>spring-core</artifactId> 45 <version>4.1.2.RELEASE</version> 46 <scope>compile</scope> 47 </dependency> 48 <dependency> 49 <groupId>org.springframework</groupId> 50 <artifactId>spring-jdbc</artifactId> 51 <version>4.1.2.RELEASE</version> 52 <scope>compile</scope> 53 </dependency> 54 <dependency> 55 <groupId>org.springframework</groupId> 56 <artifactId>spring-tx</artifactId> 57 <version>4.1.2.RELEASE</version> 58 <scope>compile</scope> 59 </dependency> 60 <dependency> 61 <groupId>org.springframework</groupId> 62 <artifactId>spring-beans</artifactId> 63 <version>4.1.2.RELEASE</version> 64 <scope>compile</scope> 65 </dependency> 66 <dependency> 67 <groupId>commons-logging</groupId> 68 <artifactId>commons-logging</artifactId> 69 <version>1.1.1</version> 70 <scope>compile</scope> 71 </dependency> 72 <!--beanUtils--> 73 <dependency> 74 <groupId>commons-beanutils</groupId> 75 <artifactId>commons-beanutils</artifactId> 76 <version>1.9.2</version> 77 <scope>compile</scope> 78 </dependency> 79 <!--jackson--> 80 <dependency> 81 <groupId>com.fasterxml.jackson.core</groupId> 82 <artifactId>jackson-databind</artifactId> 83 <version>2.3.3</version> 84 </dependency> 85 <dependency> 86 <groupId>com.fasterxml.jackson.core</groupId> 87 <artifactId>jackson-core</artifactId> 88 <version>2.3.3</version> 89 </dependency> 90 <dependency> 91 <groupId>com.fasterxml.jackson.core</groupId> 92 <artifactId>jackson-annotations</artifactId> 93 <version>2.3.3</version> 94 </dependency> 95 <!--javaMail--> 96 <dependency> 97 <groupId>javax.mail</groupId> 98 <artifactId>javax.mail-api</artifactId> 99 <version>1.5.6</version> 100 </dependency> 101 <dependency> 102 <groupId>com.sun.mail</groupId> 103 <artifactId>javax.mail</artifactId> 104 <version>1.5.3</version> 105 </dependency> 106 <!--jedis--> 107 <dependency> 108 <groupId>redis.clients</groupId> 109 <artifactId>jedis</artifactId> 110 <version>2.7.0</version> 111 </dependency> 112 </dependencies> 113 114 115 <build> 116 <!--maven插件--> 117 <plugins> 118 <!--jdk编译插件--> 119 <plugin> 120 <groupId>org.apache.maven.plugins</groupId> 121 <artifactId>maven-compiler-plugin</artifactId> 122 <configuration> 123 <source>1.8</source> 124 <target>1.8</target> 125 <encoding>utf-8</encoding> 126 </configuration> 127 </plugin> 128 <!--tomcat插件--> 129 <plugin> 130 <groupId>org.apache.tomcat.maven</groupId> 131 <!-- tomcat7的插件, 不同tomcat版本这个也不一样 --> 132 <artifactId>tomcat7-maven-plugin</artifactId> 133 <version>2.1</version> 134 <configuration> 135 <!-- 通过maven tomcat7:run运行项目时,访问项目的端口号 --> 136 <port>80</port> 137 <!-- 项目访问路径/虚拟目录--> 138 <path>/travel</path> 139 </configuration> 140 </plugin> 141 </plugins> 142 </build> 143 144 145 </project>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!