Embed Tomcat8简单示例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mn960mn/article/details/52596359

一:项目依赖

<dependency>
	<groupId>org.apache.tomcat.embed</groupId>
	<artifactId>tomcat-embed-core</artifactId>
	<version>8.5.5</version>
</dependency>
<dependency>
	<groupId>org.apache.tomcat.embed</groupId>
	<artifactId>tomcat-embed-el</artifactId>
	<version>8.5.5</version>
</dependency>
<dependency>
	<groupId>org.apache.tomcat.embed</groupId>
	<artifactId>tomcat-embed-jasper</artifactId>
	<version>8.5.5</version>
</dependency>

二:示例代码

servlet

package com.pp.server.tomcat;

import java.io.IOException;

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

public class HomeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("request scheme: " + req.getScheme());
		resp.getWriter().print("hello tomcat");
	}
}

http示例

package com.pp.server.tomcat;

import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.Tomcat.FixContextListener;

public class EmbedTomcatHttp {
	static final int port = 9080;
	static final String docBase = "e:/tmp/tomcat";
	public static void main(String[] args) throws Exception {
		Tomcat tomcat = new Tomcat();
        tomcat.setPort(port);
        tomcat.setBaseDir(docBase);
        tomcat.getHost().setAutoDeploy(false);
        
        String contextPath = "/book";
        StandardContext context = new StandardContext();
        context.setPath(contextPath);
        context.addLifecycleListener(new FixContextListener());
        tomcat.getHost().addChild(context);
        
        tomcat.addServlet(contextPath, "homeServlet", new HomeServlet());
        context.addServletMappingDecoded("/home", "homeServlet");
        tomcat.start(); 
        tomcat.getServer().await();
	}
}

访问地址:http://127.0.0.1:9080/book/home


https示例

package com.pp.server.tomcat;

import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.Tomcat.FixContextListener;
import org.apache.coyote.http11.Http11NioProtocol;

public class EmbedTomcatHttps {
	
	public static final String DEFAULT_PROTOCOL = "org.apache.coyote.http11.Http11NioProtocol";
	static final String docBase = "e:/tmp/tomcat";
	static final int port = 9080;
	static final int ports = 9090;
	public static void main(String[] args) throws Exception {
		Tomcat tomcat = new Tomcat();
        tomcat.setPort(port);
        tomcat.setBaseDir(docBase);
        
        Connector connector = new Connector(DEFAULT_PROTOCOL);
        connector.setPort(ports);
        
        Http11NioProtocol protocol = (Http11NioProtocol)connector.getProtocolHandler();
        protocol.setKeystorePass("123456");
        protocol.setKeystoreFile("e:/tmp/ssl/boot.keystore");
        protocol.setKeyAlias("mykey");
        protocol.setSSLEnabled(true);
        
        tomcat.getService().addConnector(connector);
        tomcat.getHost().setAutoDeploy(false);
        
        String contextPath = "/book";
        StandardContext context = new StandardContext();
        context.setPath(contextPath);
        context.addLifecycleListener(new FixContextListener());
        tomcat.getHost().addChild(context);
        
        tomcat.addServlet(contextPath, "homeServlet", new HomeServlet());
        context.addServletMappingDecoded("/home", "homeServlet");
        tomcat.start(); 
        tomcat.getServer().await();
	}
}

访问地址:

http://127.0.0.1:9080/book/home

https://127.0.0.1:9090/book/home

注意,在生成密钥的时候要用RSA加密,要不然只能在IE上打开,chrome(版本 53.0.2785.116)会禁止打开



展开阅读全文

没有更多推荐了,返回首页