Merge pull request #10124 from hmdrzsharifi/master
BAEL-4330: DispatcherServlet and web.xml in Spring Boot
This commit is contained in:
commit
7881b8e9c2
|
@ -26,6 +26,7 @@
|
|||
<module>spring-boot-artifacts</module>
|
||||
<module>spring-boot-autoconfiguration</module>
|
||||
<module>spring-boot-basic-customization</module>
|
||||
<module>spring-boot-basic-customization-2</module>
|
||||
<module>spring-boot-bootstrap</module>
|
||||
<module>spring-boot-client</module>
|
||||
<module>spring-boot-config-jpa-error</module>
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
## Spring Boot Basic Customization 2
|
||||
|
||||
This module contains articles about Spring Boot customization 2
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [DispatcherServlet and web.xml in Spring Boot](https://www.baeldung.com/)
|
|
@ -0,0 +1,33 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||
<artifactId>spring-boot-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>spring-boot-basic-customization-2</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-boot-basic-customization-2</name>
|
||||
<description>Module For Spring Boot Basic Customization 2</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.dispatchservlet;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.web.servlet.ServletComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DispatchServletApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DispatchServletApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.dispatchservlet.conf;
|
||||
|
||||
import com.baeldung.dispatchservlet.listener.CustomListener;
|
||||
import com.baeldung.dispatchservlet.servlet.CustomServlet;
|
||||
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
|
||||
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.servlet.ServletContextListener;
|
||||
|
||||
@Configuration
|
||||
public class WebConf {
|
||||
|
||||
@Bean
|
||||
public ServletRegistrationBean customServletBean() {
|
||||
ServletRegistrationBean bean
|
||||
= new ServletRegistrationBean(new CustomServlet(), "/servlet");
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ServletListenerRegistrationBean<ServletContextListener> customListenerBean() {
|
||||
ServletListenerRegistrationBean<ServletContextListener> bean = new ServletListenerRegistrationBean();
|
||||
bean.setListener(new CustomListener());
|
||||
return bean;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.dispatchservlet.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/")
|
||||
public class Controller {
|
||||
|
||||
@GetMapping
|
||||
public String getRequest(){
|
||||
return "Baeldung DispatcherServlet";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.dispatchservlet.filter;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.*;
|
||||
import java.io.IOException;
|
||||
|
||||
@Component
|
||||
public class CustomFilter implements Filter {
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(CustomFilter.class);
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
||||
logger.info("CustomFilter is invoked");
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.dispatchservlet.listener;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
|
||||
public class CustomListener implements ServletContextListener {
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(CustomListener.class);
|
||||
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent sce) {
|
||||
logger.info("CustomListener is initialized");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent sce) {
|
||||
logger.info("CustomListener is destroyed");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.dispatchservlet.servlet;
|
||||
|
||||
import com.baeldung.dispatchservlet.filter.CustomFilter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
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;
|
||||
|
||||
public class CustomServlet extends HttpServlet {
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(CustomServlet.class);
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
logger.info("CustomServlet doGet() method is invoked");
|
||||
super.doGet(req, resp);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
logger.info("CustomServlet doPost() method is invoked");
|
||||
super.doPost(req, resp);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue