parent
b80265392c
commit
72d5bfca82
1
pom.xml
1
pom.xml
@ -109,6 +109,7 @@
|
|||||||
<module>spring-autowire</module>
|
<module>spring-autowire</module>
|
||||||
<module>spring-batch</module>
|
<module>spring-batch</module>
|
||||||
<module>spring-boot</module>
|
<module>spring-boot</module>
|
||||||
|
<module>spring-boot-servlet</module>
|
||||||
<module>spring-cloud-data-flow</module>
|
<module>spring-cloud-data-flow</module>
|
||||||
<module>spring-cloud</module>
|
<module>spring-cloud</module>
|
||||||
<module>spring-core</module>
|
<module>spring-core</module>
|
||||||
|
4
spring-boot-servlet/.gitignore
vendored
Normal file
4
spring-boot-servlet/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
/target/
|
||||||
|
.settings/
|
||||||
|
.classpath
|
||||||
|
.project
|
2
spring-boot-servlet/README.md
Normal file
2
spring-boot-servlet/README.md
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
###Relevant Articles:
|
||||||
|
- [How to Register a Servlet in a Java Web Application](http://www.baeldung.com/how-to-register-a-servlet-in-a-java-web-application/)
|
55
spring-boot-servlet/pom.xml
Normal file
55
spring-boot-servlet/pom.xml
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>spring-boot-servlet</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>war</packaging>
|
||||||
|
<name>spring-boot-servlet</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-dependencies</artifactId>
|
||||||
|
<version>1.5.1.RELEASE</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- Spring Boot Dependencies -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<!-- Embedded EmbeddedTomcatExample Dependencies -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.tomcat.embed</groupId>
|
||||||
|
<artifactId>tomcat-embed-core</artifactId>
|
||||||
|
<version>${tomcat.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.tomcat.embed</groupId>
|
||||||
|
<artifactId>tomcat-embed-jasper</artifactId>
|
||||||
|
<version>${tomcat.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
<tomcat.version>8.5.11</tomcat.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
2
spring-boot-servlet/src/main/java/META-INF/MANIFEST.MF
Normal file
2
spring-boot-servlet/src/main/java/META-INF/MANIFEST.MF
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
Manifest-Version: 1.0
|
||||||
|
Main-Class: com.baeldung.ApplicationMain
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||||
|
import org.springframework.boot.web.support.SpringBootServletInitializer;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class ApplicationMain extends SpringBootServletInitializer {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(ApplicationMain.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
|
||||||
|
return application.sources(ApplicationMain.class);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.baeldung.configuration;
|
||||||
|
|
||||||
|
import org.springframework.web.WebApplicationInitializer;
|
||||||
|
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||||
|
import org.springframework.web.context.support.XmlWebApplicationContext;
|
||||||
|
import org.springframework.web.servlet.DispatcherServlet;
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.ServletRegistration;
|
||||||
|
|
||||||
|
public class WebAppInitializer implements WebApplicationInitializer {
|
||||||
|
|
||||||
|
public void onStartup(ServletContext container) throws ServletException {
|
||||||
|
|
||||||
|
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
|
||||||
|
ctx.register(WebMvcConfigure.class);
|
||||||
|
ctx.setServletContext(container);
|
||||||
|
|
||||||
|
ServletRegistration.Dynamic servletOne = container.addServlet("SpringProgrammaticDispatcherServlet", new DispatcherServlet(ctx));
|
||||||
|
servletOne.setLoadOnStartup(1);
|
||||||
|
servletOne.addMapping("/");
|
||||||
|
|
||||||
|
XmlWebApplicationContext xctx = new XmlWebApplicationContext();
|
||||||
|
xctx.setConfigLocation("/WEB-INF/context.xml");
|
||||||
|
xctx.setServletContext(container);
|
||||||
|
|
||||||
|
ServletRegistration.Dynamic servletTwo = container.addServlet("SpringProgrammaticXMLDispatcherServlet", new DispatcherServlet(xctx));
|
||||||
|
servletTwo.setLoadOnStartup(1);
|
||||||
|
servletTwo.addMapping("/");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.baeldung.configuration;
|
||||||
|
|
||||||
|
import org.springframework.boot.web.support.ErrorPageFilter;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.ViewResolver;
|
||||||
|
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
|
||||||
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||||
|
import org.springframework.web.servlet.resource.PathResourceResolver;
|
||||||
|
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebMvcConfigure extends WebMvcConfigurerAdapter {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ViewResolver getViewResolver() {
|
||||||
|
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
|
||||||
|
resolver.setPrefix("/WEB-INF/");
|
||||||
|
resolver.setSuffix(".jsp");
|
||||||
|
return resolver;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
|
||||||
|
configurer.enable();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||||
|
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(3600).resourceChain(true).addResolver(new PathResourceResolver());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ErrorPageFilter errorPageFilter() {
|
||||||
|
return new ErrorPageFilter();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.props;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public final class Constants {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
PropertySourcesLoader psl;
|
||||||
|
|
||||||
|
public static final String breakLine = System.getProperty("line.separator");
|
||||||
|
private static final PropertyLoader pl = new PropertyLoader();
|
||||||
|
private static final Properties mainProps = pl.getProperties("custom.properties");
|
||||||
|
public static final String DISPATCHER_SERVLET_NAME = mainProps.getProperty("dispatcher.servlet.name");
|
||||||
|
public static final String DISPATCHER_SERVLET_MAPPING = mainProps.getProperty("dispatcher.servlet.mapping");
|
||||||
|
private final String EXAMPLE_SERVLET_NAME = psl.getProperty("example.servlet.name");
|
||||||
|
private final String EXAMPLE_SERVLET_MAPPING = psl.getProperty("example.servlet.mapping");
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.props;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class PropertyLoader {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(PropertyLoader.class);
|
||||||
|
|
||||||
|
public Properties getProperties(String file) {
|
||||||
|
Properties prop = new Properties();
|
||||||
|
InputStream input = null;
|
||||||
|
try {
|
||||||
|
input = getClass().getResourceAsStream(file);
|
||||||
|
prop.load(input);
|
||||||
|
if (input != null) {
|
||||||
|
input.close();
|
||||||
|
}
|
||||||
|
} catch (IOException ex) {
|
||||||
|
log.error("IOException: " + ex);
|
||||||
|
}
|
||||||
|
return prop;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.baeldung.props;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
import org.springframework.core.env.ConfigurableEnvironment;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ComponentScan(basePackages = { "com.baeldung.*" })
|
||||||
|
@PropertySource("classpath:custom.properties") public class PropertySourcesLoader {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(PropertySourcesLoader.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
ConfigurableEnvironment env;
|
||||||
|
|
||||||
|
public String getProperty(String key) {
|
||||||
|
return env.getProperty(key);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.baeldung.servlets;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
|
public class GenericCustomServlet extends HttpServlet {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
|
response.setContentType("text/html");
|
||||||
|
PrintWriter out = response.getWriter();
|
||||||
|
out.println("<p>Hello World</p>");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.servlets.javaee;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.annotation.WebServlet;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@WebServlet(name = "AnnotationServlet",
|
||||||
|
description = "Example Servlet Using Annotations",
|
||||||
|
urlPatterns = { "/annotationservlet" })
|
||||||
|
public class AnnotationServlet extends HttpServlet {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
|
request.getRequestDispatcher("/annotationservlet.jsp").forward(request, response);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.servlets.javaee;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
|
public class EEWebXmlServlet extends HttpServlet {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
|
response.setContentType("text/html");
|
||||||
|
PrintWriter out = response.getWriter();
|
||||||
|
out.println("<p>Hello World</p>");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.baeldung.servlets.springboot;
|
||||||
|
|
||||||
|
import com.baeldung.servlets.GenericCustomServlet;
|
||||||
|
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class SpringRegistrationBeanServlet {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ServletRegistrationBean genericCustomServlet() {
|
||||||
|
ServletRegistrationBean bean = new ServletRegistrationBean(new GenericCustomServlet(), "/springregistrationbeanservlet/*");
|
||||||
|
bean.setLoadOnStartup(1);
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung.servlets.springboot.embedded;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
|
||||||
|
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class EmbeddedTomcatExample {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public EmbeddedServletContainerFactory servletContainer() {
|
||||||
|
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
|
||||||
|
return tomcat;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
#Server Configuration
|
||||||
|
#server.port=8080
|
||||||
|
#server.context-path=/javabootdata
|
||||||
|
#Resource Handling
|
||||||
|
#spring.resources.static-locations=classpath:/WEB-INF/resources
|
||||||
|
#spring.mvc.view.prefix=/WEB-INF/
|
||||||
|
#spring.mvc.view.suffix=.jsp
|
||||||
|
#spring.resources.cache-period=3600
|
||||||
|
servlet.name=dispatcherExample
|
||||||
|
servlet.mapping=/dispatcherExampleURL
|
4
spring-boot-servlet/src/main/resources/custom.properties
Normal file
4
spring-boot-servlet/src/main/resources/custom.properties
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
dispatcher.servlet.name=dispatcherExample
|
||||||
|
dispatcher.servlet.mapping=/dispatcherExampleURL
|
||||||
|
example.servlet.name=dispatcherExample
|
||||||
|
example.servlet.mapping=/dispatcherExampleURL
|
12
spring-boot-servlet/src/main/webapp/WEB-INF/context.xml
Normal file
12
spring-boot-servlet/src/main/webapp/WEB-INF/context.xml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:context="http://www.springframework.org/schema/context"
|
||||||
|
xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||||
|
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
|
||||||
|
http://www.springframework.org/schema/context
|
||||||
|
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
|
||||||
|
|
||||||
|
<context:component-scan base-package="com.baeldung"/>
|
||||||
|
|
||||||
|
<bean class="com.baeldung.configuration.WebAppInitializer"/>
|
||||||
|
</beans>
|
16
spring-boot-servlet/src/main/webapp/WEB-INF/dispatcher.xml
Normal file
16
spring-boot-servlet/src/main/webapp/WEB-INF/dispatcher.xml
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<beans xmlns:context="http://www.springframework.org/schema/context"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xsi:schemaLocation="
|
||||||
|
http://www.springframework.org/schema/beans
|
||||||
|
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||||
|
http://www.springframework.org/schema/context
|
||||||
|
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
|
||||||
|
|
||||||
|
<context:component-scan base-package="com.baeldung"/>
|
||||||
|
|
||||||
|
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
||||||
|
<property name="prefix" value="/WEB-INF/jsp/"/>
|
||||||
|
<property name="suffix" value=".jsp"/>
|
||||||
|
</bean>
|
||||||
|
</beans>
|
40
spring-boot-servlet/src/main/webapp/WEB-INF/web.xml
Normal file
40
spring-boot-servlet/src/main/webapp/WEB-INF/web.xml
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||||
|
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
|
||||||
|
id="WebApp_ID" version="3.1">
|
||||||
|
<display-name>JSP</display-name>
|
||||||
|
<welcome-file-list>
|
||||||
|
<welcome-file>index.html</welcome-file>
|
||||||
|
<welcome-file>index.htm</welcome-file>
|
||||||
|
<welcome-file>index.jsp</welcome-file>
|
||||||
|
</welcome-file-list>
|
||||||
|
|
||||||
|
<!-- Java EE Servlets -->
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>EEWebXmlServlet</servlet-name>
|
||||||
|
<servlet-class>com.baeldung.servlets.javaee.EEWebXmlServlet</servlet-class>
|
||||||
|
</servlet>
|
||||||
|
|
||||||
|
<servlet-mapping>
|
||||||
|
<servlet-name>EEWebXmlServlet</servlet-name>
|
||||||
|
<url-pattern>/eewebxmlservlet</url-pattern>
|
||||||
|
</servlet-mapping>
|
||||||
|
|
||||||
|
<!-- Spring Boot Servlets -->
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>SpringBootWebXmlServlet</servlet-name>
|
||||||
|
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||||
|
<init-param>
|
||||||
|
<param-name>contextConfigLocation</param-name>
|
||||||
|
<param-value>/WEB-INF/dispatcher.xml</param-value>
|
||||||
|
</init-param>
|
||||||
|
<load-on-startup>1</load-on-startup>
|
||||||
|
</servlet>
|
||||||
|
|
||||||
|
<servlet-mapping>
|
||||||
|
<servlet-name>SpringBootWebXmlServlet</servlet-name>
|
||||||
|
<url-pattern>/</url-pattern>
|
||||||
|
</servlet-mapping>
|
||||||
|
</web-app>
|
||||||
|
|
||||||
|
|
@ -0,0 +1 @@
|
|||||||
|
<p>Annotation Servlet!</p>
|
1
spring-boot-servlet/src/main/webapp/index.jsp
Normal file
1
spring-boot-servlet/src/main/webapp/index.jsp
Normal file
@ -0,0 +1 @@
|
|||||||
|
<p>Hello!</p>
|
Loading…
x
Reference in New Issue
Block a user