嵌入式Tomcat简单地尝试下

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!

\r\n"); } }; 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();
		}
	}

}

嵌入式Tomcat简单地尝试下_第1张图片


嵌入式Tomcat简单地尝试下_第2张图片





你可能感兴趣的:(Java,JavaEE,Error,Solution,Configuration)