实现内嵌tomcat

内嵌tomcat的实现:

首先根据外部tomcat的配置和启动过程我们可以得到以下条件:

web.xml:

1、配置Servlet

2、配置请求拦截

3、配置请求转发

... ...

tomcat一些"默认"配置:

1、端口号

2、将项目添加到webapps目录下

... ...

由上面一些条件再通过tomcat的API就能实现一个简单的内嵌tomcat例子:

(为集成到其它项目中去,此例子创建的是jar包)

项目结构:


实现内嵌tomcat_第1张图片

maven依赖:

                
		
			org.apache.tomcat.embed
			tomcat-embed-websocket
			8.5.11
		
		
		
			javax.servlet
			javax.servlet-api
			3.1.0
		
		
		
			javax.servlet.jsp
			javax.servlet.jsp-api
			2.3.1
		
		
		
			javax.servlet
			jstl
			1.2
		
		
                        net.sf.json-lib
                        json-lib
                        2.4
                        jdk15
                

创建一个内嵌tomcat类:

package com.bin.inside;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.catalina.Context;
import org.apache.catalina.Host;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;

import com.bin.servlet.Servlet;

public class InsideTomcat {

	// tomcat的端口号
	private static String port;
	// tomcat的字符编码集
	private static String code;
	// 拦截请求路径
	private static String hinderURL;
	// 请求转发路径
	private static String shiftURL;
	// tomcat对象
	private static Tomcat tomcat;
	// servlet对象
	private static HttpServlet servlet;

	InsideTomcat() {
		try {
			init();
		} catch (LifecycleException | ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 启动这个内嵌tomcat容器
	public static void run() throws LifecycleException {
		InsideTomcat insideTomcat = new InsideTomcat();
		// 启动tomcat
		insideTomcat.tomcat.start();
		// 保持tomcat的启动状态
		insideTomcat.tomcat.getServer().await();
	}

	// 初始化tomcat容器
	public void init() throws LifecycleException, ServletException {
		// 执行init方法时加载默认属性值
                // 为了演示这里写了死值进去
		port = "8080";
		code = "UTF-8";
		hinderURL = "/";
		shiftURL = "/";
		tomcat = new Tomcat();
		// HttpServlet对象是你现有的自定义的Servlet容器
		servlet = new Servlet();

		// 创建连接器
		Connector conn = tomcat.getConnector();
		conn.setPort(Integer.valueOf(port));
		conn.setURIEncoding(code);

		// 设置Host
		Host host = tomcat.getHost();
		host.setAppBase("webapps");

		// 获取目录绝对路径
		String classPath = System.getProperty("user.dir");

		// 配置tomcat上下文
		Context context = tomcat.addContext(host, hinderURL, classPath);

		// 配置请求拦截转发
		Wrapper wrapper = tomcat.addServlet(shiftURL, "Servlet", servlet);
		wrapper.addMapping(shiftURL);

	}

	/*
	 * 以下Setters和Getters方法提供外部访问或灵活设置
	 */
	public static String getPort() {
		return port;
	}
	public static void setPort(String port) {
		InsideTomcat.port = port;
	}
	public static String getCode() {
		return code;
	}
	public static void setCode(String code) {
		InsideTomcat.code = code;
	}
	public static String getHinderURL() {
		return hinderURL;
	}
	public static void setHinderURL(String hinderURL) {
		InsideTomcat.hinderURL = hinderURL;
	}
	public static String getShiftURL() {
		return shiftURL;
	}
	public static void setShiftURL(String shiftURL) {
		InsideTomcat.shiftURL = shiftURL;
	}
	public static Tomcat getTomcat() {
		return tomcat;
	}
	public static void setTomcat(Tomcat tomcat) {
		InsideTomcat.tomcat = tomcat;
	}
	public static HttpServlet getServlet() {
		return servlet;
	}
	public static void setServlet(HttpServlet servlet) {
		InsideTomcat.servlet = servlet;
	}
}

创建一个Servlet类,继承HttpServlet:

package com.bin.servlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Servlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void init() {
		System.out.println("执行了init方法");
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("进入doGet方法,请求路径是:" + request.getRequestURI());
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		doGet(request, response);
	}
}

测试类:

package com.bin.main;

import org.apache.catalina.LifecycleException;
import com.bin.inside.InsideTomcat;

public class Main {

	public static void main(String[] args) throws LifecycleException {
		// TODO Auto-generated method stub
		InsideTomcat.run();
	}

}

运行结果:

实现内嵌tomcat_第2张图片

你可能感兴趣的:(javaWeb)