-
Notifications
You must be signed in to change notification settings - Fork 54.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
23 changed files
with
402 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/target/ | ||
.settings/ | ||
.classpath | ||
.project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Manifest-Version: 1.0 | ||
Main-Class: com.baeldung.ApplicationMain |
19 changes: 19 additions & 0 deletions
19
spring-boot-servlet/src/main/java/com/baeldung/ApplicationMain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
spring-boot-servlet/src/main/java/com/baeldung/configuration/WebAppInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("/"); | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
spring-boot-servlet/src/main/java/com/baeldung/configuration/WebMvcConfigure.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
20 changes: 20 additions & 0 deletions
20
spring-boot-servlet/src/main/java/com/baeldung/props/Constants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
spring-boot-servlet/src/main/java/com/baeldung/props/PropertyLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
spring-boot-servlet/src/main/java/com/baeldung/props/PropertySourcesLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
spring-boot-servlet/src/main/java/com/baeldung/servlets/GenericCustomServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>"); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
spring-boot-servlet/src/main/java/com/baeldung/servlets/javaee/AnnotationServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
spring-boot-servlet/src/main/java/com/baeldung/servlets/javaee/EEWebXmlServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>"); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...servlet/src/main/java/com/baeldung/servlets/springboot/SpringRegistrationBeanServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
|
16 changes: 16 additions & 0 deletions
16
...ervlet/src/main/java/com/baeldung/servlets/springboot/embedded/EmbeddedTomcatExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
spring-boot-servlet/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
spring-boot-servlet/src/main/webapp/WEB-INF/dispatcher.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.