最近遇到个需求,引入的依赖中引入了Jetty9,jfinal-jetty的开发环境是jetty8。于是jar包冲突了。
![50736_20170905203816.png I_8@OW$B]PR0O0AL9A29BVV.png](/upload/img/share/0/50736_20170905203816.png)
这是jfinal-jetty的依赖
![50736_20170905203825.png 99W[CHJ[4WQ{)$BOIWY]G`6.png](/upload/img/share/0/50736_20170905203825.png)
这是我的依赖

于是乎在IDEA Main方法启动下就在这个地方出错了
参考了玛雅牛的博客之后 https://my.oschina.net/myaniu/blog/470050
/**
* 我有故事,你有酒么?
* JKhaled created by yunqisong@foxmail.com 2017/9/5
* FOR : JFinal IDEAJetty9Server With Jetty9
*/
public class IDEAJetty9Server implements IServer{
private String webAppDir;
private int port;
private String context;
private boolean running = false;
private Server server;
private WebAppContext webApp;
public IDEAServer(String webAppDir, int port, String context) {
if (webAppDir == null) {
throw new IllegalStateException("Invalid webAppDir of web server: " + webAppDir);
}
if (port < 0 || port > 65535) {
throw new IllegalArgumentException("Invalid port of web server: " + port);
}
if (StrKit.isBlank(context)) {
throw new IllegalStateException("Invalid context of web server: " + context);
}
this.webAppDir = webAppDir;
this.port = port;
this.context = context;
// this.scanIntervalSeconds = scanIntervalSeconds;
}
public void start() {
if (!running) {
try {
running = true;
doStart();
} catch (Exception e) {
System.err.println(e.getMessage());
LogKit.error(e.getMessage(), e);
}
}
}
public void stop() {
if (running) {
try {server.stop();} catch (Exception e) {LogKit.error(e.getMessage(), e);}
running = false;
}
}
private void doStart() {
if (!available(port)) {
throw new IllegalStateException("port: " + port + " already in use!");
}
deleteSessionData();
System.out.println("Starting JFinal " + Const.JFINAL_VERSION);
server = new Server();
HttpConfiguration http_config = new HttpConfiguration();
ServerConnector connector = new ServerConnector(server,new HttpConnectionFactory(http_config));
connector.setReuseAddress(true);
connector.setIdleTimeout(30000);
connector.setPort(port);
server.addConnector(connector);
webApp = new WebAppContext();
webApp.setThrowUnavailableOnStartupException(true); // 在启动过程中允许抛出异常终止启动并退出 JVM
webApp.setContextPath(context);
webApp.setResourceBase(webAppDir); // webApp.setWar(webAppDir);
webApp.setContextPath(context);
webApp.setMaxFormContentSize(81920000);
webApp.getInitParams().put("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
webApp.getInitParams().put("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "true");
webApp.getInitParams().put("org.eclipse.jetty.server.Request.maxFormContentSize", "-1");
persistSession(webApp);
server.setHandler(webApp);
try {
System.out.println("Starting web server on port: " + port);
server.start();
System.out.println("Starting Complete. Welcome To The JFinal World :)");
server.join();
} catch (Exception e) {
LogKit.error(e.getMessage(), e);
System.exit(100);
}
return;
}
private void deleteSessionData() {
try {
FileKit.delete(new File(getStoreDir()));
}
catch (Exception e) {
LogKit.logNothing(e);
}
}
private String getStoreDir() {
String storeDir = PathKit.getWebRootPath() + "/../../session_data" + context;
if ("\\".equals(File.separator)) {
storeDir = storeDir.replaceAll("/", "\\\\");
}
return storeDir;
}
private void persistSession(WebAppContext webApp) {
String storeDir = getStoreDir();
SessionManager sm = webApp.getSessionHandler().getSessionManager();
if (sm instanceof HashSessionManager) {
try {
((HashSessionManager)sm).setStoreDirectory(new File(storeDir));
} catch (IOException e) {
e.printStackTrace();
}
return ;
}
HashSessionManager hsm = new HashSessionManager();
try {
hsm.setStoreDirectory(new File(storeDir));
} catch (IOException e) {
e.printStackTrace();
}
SessionHandler sh = new SessionHandler();
sh.setSessionManager(hsm);
webApp.setSessionHandler(sh);
}
private static boolean available(int port) {
if (port <= 0) {
throw new IllegalArgumentException("Invalid start port: " + port);
}
ServerSocket ss = null;
DatagramSocket ds = null;
try {
ss = new ServerSocket(port);
ss.setReuseAddress(true);
ds = new DatagramSocket(port);
ds.setReuseAddress(true);
return true;
} catch (IOException e) {
LogKit.logNothing(e);
} finally {
if (ds != null) {
ds.close();
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
// should not be thrown, just detect port available.
LogKit.logNothing(e);
}
}
}
return false;
}
}然后 main方法中启动
new IDEAJetty9Server ("src/main/webapp",80,"/").start();希望能够在大家有需要的时候帮助到大家,(虽然大部分代码都是copy波总的),玛雅牛给出Eclipse的实现,这里是一个IDEA简单的实现。欢迎大家讨论
另外,类名我建议改成 Jetty9Server,区分版本更好