[开发背景]
tomcat启动之后占用三个端口。我对此非常不理解,我决定在tomcat基础之上,开发一个新的tomcat,仅仅占用一个端口。
gitee地址:https://gitee.com/litongjava_admin/bill-tomcat
bill-tomcat开发
创建工程
pom.xml中添加依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.6</jdk.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- java编译插件 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
在src/main/resources创建webapps目录
在src/main/resources创建tomcat.properties
内容如下
tomcat.port=8080
进行编码
ConfigUtil.java
package com.litong.utils.file;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
/**
* @author litong 读取config.properties
*/
public class ConfigUtil {
private static String configFilePath = "tomcat.properties";
private static Properties prop = null;
private static InputStream ins = null;
static {
ins = ConfigUtil.class.getClassLoader().getResourceAsStream(configFilePath);
// 防止读取乱码
InputStreamReader insReader = null;
try {
insReader = new InputStreamReader(ins, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
prop = new Properties();
try {
prop.load(insReader);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* get value of key
*/
public static String getValue(String key) {
return prop.getProperty(key);
}
public static int getIntValue(String key) {
return Integer.parseInt(prop.getProperty(key));
}
/**
* 设置值,设置值后存盘
* @param key
* @param value
*/
public static void put(String key, String value) {
prop.put(key, value);
ConfigUtil.save();
}
public static void save() {
URL url = ConfigUtil.class.getClassLoader().getResource(configFilePath);
File file = new File(url.getFile());
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
prop.store(fileOutputStream, null);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* get properties
*/
public static Properties getProperties() {
return prop;
}
/**
*
* @return 配置文件名称
*/
public static String getConfigFile() {
return configFilePath;
}
public static void main(String[] args) {
// String value = ConfigUtil.getValue("prorject_id");
// System.out.println(value);
Properties prop = ConfigUtil.getProperties();
Set<Entry<Object, Object>> entrySet = prop.entrySet();