Skip to content

Commit dc715a3

Browse files
izeyesnicoll
authored andcommitted
Update doc to align with TomcatEmbeddedServletContainerFactory renaming
Closes gh-12215
1 parent 8257fd9 commit dc715a3

File tree

2 files changed

+83
-31
lines changed

2 files changed

+83
-31
lines changed

spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,37 +1822,7 @@ depending on the web server in use. For Tomcat, the following configuration can
18221822

18231823
[source,java,indent=0]
18241824
----
1825-
@Bean
1826-
public TomcatEmbeddedServletContainerFactory servletContainerFactory() {
1827-
return new TomcatEmbeddedServletContainerFactory() {
1828-
@Override
1829-
protected void prepareContext(Host host,
1830-
ServletContextInitializer[] initializers) {
1831-
super.prepareContext(host, initializers);
1832-
StandardContext child = new StandardContext();
1833-
child.addLifecycleListener(new Tomcat.FixContextListener());
1834-
child.setPath("/cloudfoundryapplication");
1835-
ServletContainerInitializer initializer = getServletContextInitializer(getContextPath());
1836-
child.addServletContainerInitializer(initializer, Collections.emptySet());
1837-
child.setCrossContext(true);
1838-
host.addChild(child);
1839-
}
1840-
};
1841-
}
1842-
1843-
private ServletContainerInitializer getServletContextInitializer(String contextPath) {
1844-
return (c, context) -> {
1845-
Servlet servlet = new GenericServlet() {
1846-
@Override
1847-
public void service(ServletRequest req, ServletResponse res)
1848-
throws ServletException, IOException {
1849-
ServletContext context = req.getServletContext().getContext(contextPath);
1850-
context.getRequestDispatcher("/cloudfoundryapplication").forward(req, res);
1851-
}
1852-
};
1853-
context.addServlet("cloudfoundry", servlet).addMapping("/*");
1854-
};
1855-
}
1825+
include::{code-examples}/cloudfoundry/CloudFoundryCustomContextPathExample.java[tag=configuration]
18561826
----
18571827

18581828

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.cloudfoundry;
18+
19+
import java.io.IOException;
20+
import java.util.Collections;
21+
22+
import javax.servlet.GenericServlet;
23+
import javax.servlet.Servlet;
24+
import javax.servlet.ServletContainerInitializer;
25+
import javax.servlet.ServletContext;
26+
import javax.servlet.ServletException;
27+
import javax.servlet.ServletRequest;
28+
import javax.servlet.ServletResponse;
29+
30+
import org.apache.catalina.Host;
31+
import org.apache.catalina.core.StandardContext;
32+
import org.apache.catalina.startup.Tomcat;
33+
34+
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
35+
import org.springframework.boot.web.servlet.ServletContextInitializer;
36+
import org.springframework.context.annotation.Bean;
37+
38+
/**
39+
* Example configuration for custom context path in Cloud Foundry.
40+
*
41+
* @author Johnny Lim
42+
*/
43+
public class CloudFoundryCustomContextPathExample {
44+
45+
// tag::configuration[]
46+
@Bean
47+
public TomcatServletWebServerFactory servletWebServerFactory() {
48+
return new TomcatServletWebServerFactory() {
49+
50+
@Override
51+
protected void prepareContext(Host host, ServletContextInitializer[] initializers) {
52+
super.prepareContext(host, initializers);
53+
StandardContext child = new StandardContext();
54+
child.addLifecycleListener(new Tomcat.FixContextListener());
55+
child.setPath("/cloudfoundryapplication");
56+
ServletContainerInitializer initializer = getServletContextInitializer(getContextPath());
57+
child.addServletContainerInitializer(initializer, Collections.emptySet());
58+
child.setCrossContext(true);
59+
host.addChild(child);
60+
}
61+
62+
};
63+
}
64+
65+
private ServletContainerInitializer getServletContextInitializer(String contextPath) {
66+
return (c, context) -> {
67+
Servlet servlet = new GenericServlet() {
68+
69+
@Override
70+
public void service(ServletRequest req, ServletResponse res)
71+
throws ServletException, IOException {
72+
ServletContext context = req.getServletContext().getContext(contextPath);
73+
context.getRequestDispatcher("/cloudfoundryapplication").forward(req, res);
74+
}
75+
76+
};
77+
context.addServlet("cloudfoundry", servlet).addMapping("/*");
78+
};
79+
}
80+
// end::configuration[]
81+
82+
}

0 commit comments

Comments
 (0)