How to configure AJP connector in the embedded tomcat?

3.9k views Asked by At

I need to configure AJP connector in embedded tomcat https://tomcat.apache.org/tomcat-8.0-doc/config/ajp.html Equals to the following configuration:

<Connector port="8009" protocol="AJP/1.3" redirectPort="8080" />

How to do it?

1

There are 1 answers

0
ares On BEST ANSWER
// initialize tomcat with default nio connector
Tomcat tomcat = new Tomcat();
tomcat.setPort(8080);
tomcat.getHost().setAppBase(".");
tomcat.addWebapp("/", ".");

// create an AJP connector
Connector ajpConnector = new Connector("AJP/1.3");
ajpConnector.setPort(8009);
ajpConnector.setRedirectPort(8080);
ajpConnector.setSecure(false);

// add connector to tomcat
tomcat.getService().addConnector(ajpConnector);
tomcat.start();
tomcat.getServer().await();

From the startup logs:

Dec 26, 2016 10:45:26 AM org.apache.catalina.core.StandardContext setPath
WARNING: A context path must either be an empty string or start with a '/' and do not end with a '/'. The path [/] does not meet these criteria and has been changed to []
Dec 26, 2016 10:45:27 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Dec 26, 2016 10:45:27 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Dec 26, 2016 10:45:27 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Dec 26, 2016 10:45:27 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Dec 26, 2016 10:45:27 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
Dec 26, 2016 10:45:27 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.28
Dec 26, 2016 10:45:27 AM org.apache.catalina.startup.ContextConfig getDefaultWebXmlFragment
INFO: No global web.xml found
Dec 26, 2016 10:45:28 AM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Dec 26, 2016 10:45:28 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Dec 26, 2016 10:45:28 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]

http-nio is listening on port 8080 and ajp-nio on port 8009.