嵌入式Tomcat简单地尝试下
2019-07-13 08:49发布
生成海报
package com.xiuye.server;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
public class SmallTomcat1 {
@SuppressWarnings("deprecation")
public static void main(String[] args) {
Tomcat tomcat = new Tomcat();
HttpServlet servlet = new HttpServlet() {
/**
*
*/
private static final long serialVersionUID = 5639776356541557752L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.getWriter().write("Hello,I am embeded tomcat!
");
}
};
Context ctx = tomcat.addContext("/hello", null);
Tomcat.addServlet(ctx, "/servlet", servlet);
ctx.addServletMapping("/servlet", "/servlet");
try {
tomcat.init();
tomcat.start();
tomcat.getServer().await();
} catch (LifecycleException e) {
e.printStackTrace();
}
}
}
package com.xiuye.server;
import javax.servlet.ServletException;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
public class SmallTomcat2 {
public static void main(String[] args) {
Tomcat tomcat = new Tomcat();
try {
tomcat.addWebapp("/sample", "webapps\showcase-5.1");
tomcat.init();
tomcat.start();
tomcat.getServer().await();
} catch (ServletException e) {
e.printStackTrace();
} catch (LifecycleException e) {
e.printStackTrace();
}
}
}
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮