我尝试使用jetty和javax-rs创建其他服务器。 我指的是一些网站的内容。
我只是有一些问题。 1.如果我想制作它,我是否必须运行tomcat或者只是使用Jetty?
我想知道下面两个不同代码的合并。
我的服务器主要用于使用websocket(它被引用到这个网站http://amilamanoj.blogspot.kr/2013/06/secure-websockets-with-jetty.html)
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.util.resource.FileResource;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.websocket.server.WebSocketHandler;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
import java.util.ArrayList;
import java.util.List;
public class WebSocketServer {
private Server server;
private String host;
private int port;
private Resource keyStoreResource;
private String keyStorePassword;
private String keyManagerPassword;
private List<Handler> webSocketHandlerList = new ArrayList<>();
public static void main(String[] args) throws Exception {
WebSocketServer webSocketServer = new WebSocketServer();
webSocketServer.setHost("localhost");
webSocketServer.setPort(8443);
webSocketServer.setKeyStoreResource(new FileResource(WebSocketServer.class.getResource("/keystore.jks")));
webSocketServer.setKeyStorePassword("password");
webSocketServer.setKeyManagerPassword("password");
webSocketServer.addWebSocket(MyWebSocket.class, "/");
webSocketServer.initialize();
webSocketServer.start();
}
public void initialize() {
server = new Server();
// connector configuration
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStoreResource(keyStoreResource);
sslContextFactory.setKeyStorePassword(keyStorePassword);
sslContextFactory.setKeyManagerPassword(keyManagerPassword);
SslConnectionFactory sslConnectionFactory = new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString());
HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory(new HttpConfiguration());
ServerConnector sslConnector = new ServerConnector(server, sslConnectionFactory, httpConnectionFactory);
sslConnector.setHost(host);
sslConnector.setPort(port);
server.addConnector(sslConnector);
// handler configuration
HandlerCollection handlerCollection = new HandlerCollection();
handlerCollection.setHandlers(webSocketHandlerList.toArray(new Handler[0]));
server.setHandler(handlerCollection);
}
public void addWebSocket(final Class<?> webSocket, String pathSpec) {
WebSocketHandler wsHandler = new WebSocketHandler() {
@Override
public void configure(WebSocketServletFactory webSocketServletFactory) {
webSocketServletFactory.register(webSocket);
}
};
ContextHandler wsContextHandler = new ContextHandler();
wsContextHandler.setHandler(wsHandler);
wsContextHandler.setContextPath(pathSpec); // this context path doesn't work ftm
webSocketHandlerList.add(wsHandler);
}
public void start() throws Exception {
server.start();
server.join();
}
public void stop() throws Exception {
server.stop();
server.join();
}
public void setHost(String host) {
this.host = host;
}
public void setPort(int port) {
this.port = port;
}
public void setKeyStoreResource(Resource keyStoreResource) {
this.keyStoreResource = keyStoreResource;
}
public void setKeyStorePassword(String keyStorePassword) {
this.keyStorePassword = keyStorePassword;
}
public void setKeyManagerPassword(String keyManagerPassword) {
this.keyManagerPassword = keyManagerPassword;
}
}
这是运行Rest Server的配置。
package example;
import org.eclipse.jetty.server.Server;4
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
public class RestServer {
public static void main(String[] args) throws Exception {
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
Server jettyServer = new Server(8080);
jettyServer.setHandler(context);
ServletHolder jerseyServlet = context.addServlet(
org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
jerseyServlet.setInitParameter(
"jersey.config.server.provider.classnames",
Calculator.class.getCanonicalName());
try {
jettyServer.start();
jettyServer.join();
} finally {
jettyServer.destroy();
}
}
}