Skip to content

Commit

Permalink
BAEL-9 #3 (#1207)
Browse files Browse the repository at this point in the history
* BAEL-9 #3

* pom.xml fix
  • Loading branch information
Thoughtscript authored and KevinGilmore committed Feb 22, 2017
1 parent b802653 commit 72d5bfc
Show file tree
Hide file tree
Showing 23 changed files with 402 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<module>spring-autowire</module>
<module>spring-batch</module>
<module>spring-boot</module>
<module>spring-boot-servlet</module>
<module>spring-cloud-data-flow</module>
<module>spring-cloud</module>
<module>spring-core</module>
Expand Down
4 changes: 4 additions & 0 deletions spring-boot-servlet/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/target/
.settings/
.classpath
.project
2 changes: 2 additions & 0 deletions spring-boot-servlet/README.md
Original file line number Diff line number Diff line change
@@ -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 changes: 55 additions & 0 deletions spring-boot-servlet/pom.xml
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 0 deletions spring-boot-servlet/src/main/java/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Manifest-Version: 1.0
Main-Class: com.baeldung.ApplicationMain
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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("/");
}

}
Original file line number Diff line number Diff line change
@@ -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();
}
}

Original file line number Diff line number Diff line change
@@ -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");

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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>");
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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>");
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}


Original file line number Diff line number Diff line change
@@ -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;
}
}
10 changes: 10 additions & 0 deletions spring-boot-servlet/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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 changes: 4 additions & 0 deletions spring-boot-servlet/src/main/resources/custom.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dispatcher.servlet.name=dispatcherExample
dispatcher.servlet.mapping=/dispatcherExampleURL
example.servlet.name=dispatcherExample
example.servlet.mapping=/dispatcherExampleURL
12 changes: 12 additions & 0 deletions spring-boot-servlet/src/main/webapp/WEB-INF/context.xml
Original file line number Diff line number Diff line change
@@ -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 changes: 16 additions & 0 deletions spring-boot-servlet/src/main/webapp/WEB-INF/dispatcher.xml
Original file line number Diff line number Diff line change
@@ -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>
Loading

0 comments on commit 72d5bfc

Please sign in to comment.