package Jetty;
import org.eclipse.jetty.server.NetworkTrafficServerConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
//import org.eclipse.jetty.server.NetworkTrafficServerConnector;
//import org.eclipse.jetty.server.Server;
//import org.eclipse.jetty.webapp.WebAppContext;
public class JettyStart {
/* (1) get请求 编码默认使用 UTF-8 编码,
* 可以通过 request.setAttribute(“org.eclipse.jetty.server.Request.queryEncoding”,”GBK”) 设定查询参数的编码,
* 如果不设定则默认读取系统属性 -Dorg.eclipse.jetty.util.URI.charset=GBK 的编码,如果系统属性也没有设定,则默认为 UTF-8 。
* (2)POST 参数默认使用 Content-type 中的 Charset 编码,如果 Charset 没有,则默认使用 UTF-8 编码,
* 当然可以在使用之前使用 request.set CharacterEncoding 设定编码。
*/
public static void main(String[] args) throws Exception {
//新建一个server,设置端口号
Server server = new Server(8888);
//想向server中添加一二Connector,可单独设置IP和端口
NetworkTrafficServerConnector connector = new NetworkTrafficServerConnector(server);
connector.setHost("localhost");
connector.setPort(7777);
server.addConnector(connector);
//设置jetty的工作目录。(取得一个context环境,并添加到server中)
new JettyStart();
String currentClassPath = JettyStart.class.getResource("/").getPath();
// String webPath = currentClassPath.substring(0, currentClassPath.indexOf("com.spring.web")) + "com.spring.web/src/main/webapp";
String webPath = currentClassPath.substring(0, currentClassPath.indexOf("com.spring.web")) + "com.spring.web";
WebAppContext context = new WebAppContext(webPath, "/");
server.setHandler(context);
//设置XX东西,哎,不深入了解
server.setStopAtShutdown(true);
server.start();
server.join();
}
}
POM.XML
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.3.8.v20160314</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>9.3.8.v20160314</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.3.8.v20160314</version>
</dependency>