jetty嵌入式使用
所属分类 jetty
浏览量 1484
自定义处理器handler
servlet
webapp
7.6.20.v20160902
pom依赖 组件
javax.servlet:servlet-api:2.5
org.eclipse.jetty
jetty-server
jetty-servlet
jetty-webapp
jetty-jsp
自定义handler
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
public class HelloWorld extends AbstractHandler {
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
System.out.println("target="+target);
response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
baseRequest.setHandled(true);
response.getWriter().println("hello world,"+new Date());
}
public static void main(String[] args) throws Exception {
int port = 8080;
System.out.println("port="+port);
Server server = new Server(port);
server.setHandler(new HelloWorld());
server.start();
server.join();
}
}
可自定义handler , 实现 restfull 框架
servlet
import java.io.IOException;
import java.util.Date;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
public class HelloServletMain {
public static void main(String[] args) throws Exception {
int port = 8080;
Server server = createServer(port);
server.start();
server.join();
}
public static Server createServer(int port) {
Server server = new Server(port);
// 引入 jetty-servlet
ServletHandler handler = new ServletHandler();
server.setHandler(handler);
handler.addServletWithMapping(HelloServlet.class, "/*");
return server;
}
@SuppressWarnings("serial")
public static class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
response.getWriter().println("Hello Servlet,"+new Date());
}
}
}
webapp
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
public class HelloAppMain {
public static Server createServer(int port) {
Server server = new Server(port);
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
// File warFile = JettyDistribution.resolve("demo-base/webapps/async-rest.war").toFile();
// webapp.setWar(warFile.getAbsolutePath());
webapp.setResourceBase("/Users/dugang/work/xdemo/webapp/");
server.setHandler(webapp);
return server;
}
public static void main(String[] args) throws Exception {
int port = 8080;
Server server = createServer(port);
server.start();
server.dumpStdErr();
server.join();
}
}
org.eclipse.jetty.webapp.WebAppClassLoader
public class WebAppClassLoader extends URLClassLoader
可能的错误
JSP support not configured 添加依赖 jetty-jsp
java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.isAsyncStarted()
jetty版本太高,servlet-api版本太低 , 可以先使用低版本的jetty
完整代码
https://gitee.com/dyyx/xdemo/tree/master/src/main/java/dyyx/jetty
Embedding Jetty
https://www.eclipse.org/jetty/documentation/current/embedding-jetty.html
上一篇
下一篇
java内存溢出
java restful 框架
类加载器中findClass与loadClass的区别
URLClassLoader简介
netty配置参数
netty参数及相关代码