From c636b0d4b73ba3954c973766ec8d1dc63357733f Mon Sep 17 00:00:00 2001 From: Joel Juarez Date: Thu, 21 Feb 2019 23:56:32 +0100 Subject: [PATCH] java web application without web.xml - servlets 3.0 --- javax-servlets-3/.gitignore | 6 --- javax-servlets-3/Dockerfile | 2 - javax-servlets-3/README.md | 10 ---- javax-servlets-3/pom.xml | 52 ------------------- .../servlets3/spring/AppInitializer.java | 27 ---------- .../servlets3/spring/config/AppConfig.java | 11 ---- .../controllers/UppercaseController.java | 17 ------ .../baeldung}/filters/EmptyParamFilter.java | 4 +- .../com/baeldung}/listeners/AppListener.java | 2 +- .../baeldung}/listeners/RequestListener.java | 2 +- .../baeldung}/servlets/CounterServlet.java | 6 +-- .../baeldung}/servlets/UppercaseServlet.java | 6 +-- 12 files changed, 6 insertions(+), 139 deletions(-) delete mode 100644 javax-servlets-3/.gitignore delete mode 100644 javax-servlets-3/Dockerfile delete mode 100644 javax-servlets-3/README.md delete mode 100644 javax-servlets-3/pom.xml delete mode 100644 javax-servlets-3/src/main/java/com/baeldung/servlets3/spring/AppInitializer.java delete mode 100644 javax-servlets-3/src/main/java/com/baeldung/servlets3/spring/config/AppConfig.java delete mode 100644 javax-servlets-3/src/main/java/com/baeldung/servlets3/spring/controllers/UppercaseController.java rename {javax-servlets-3/src/main/java/com/baeldung/servlets3/web => javax-servlets/src/main/java/com/baeldung}/filters/EmptyParamFilter.java (88%) rename {javax-servlets-3/src/main/java/com/baeldung/servlets3/web => javax-servlets/src/main/java/com/baeldung}/listeners/AppListener.java (91%) rename {javax-servlets-3/src/main/java/com/baeldung/servlets3/web => javax-servlets/src/main/java/com/baeldung}/listeners/RequestListener.java (94%) rename {javax-servlets-3/src/main/java/com/baeldung/servlets3/web => javax-servlets/src/main/java/com/baeldung}/servlets/CounterServlet.java (78%) rename {javax-servlets-3/src/main/java/com/baeldung/servlets3/web => javax-servlets/src/main/java/com/baeldung}/servlets/UppercaseServlet.java (78%) diff --git a/javax-servlets-3/.gitignore b/javax-servlets-3/.gitignore deleted file mode 100644 index dfbd063287..0000000000 --- a/javax-servlets-3/.gitignore +++ /dev/null @@ -1,6 +0,0 @@ -# Created by .ignore support plugin (hsz.mobi) -.idea -classes -target -*.iml -out \ No newline at end of file diff --git a/javax-servlets-3/Dockerfile b/javax-servlets-3/Dockerfile deleted file mode 100644 index 27d1450acb..0000000000 --- a/javax-servlets-3/Dockerfile +++ /dev/null @@ -1,2 +0,0 @@ -FROM tomcat -ADD ./target/uppercasing-app.war /usr/local/tomcat/webapps/ \ No newline at end of file diff --git a/javax-servlets-3/README.md b/javax-servlets-3/README.md deleted file mode 100644 index 1f4855f4b3..0000000000 --- a/javax-servlets-3/README.md +++ /dev/null @@ -1,10 +0,0 @@ -## Build with maven: -mvn package - -## Run with Tomcat on Docker container: -docker build --tag my-tomcat . - -docker run -it --rm -p 8080:8080 my-tomcat - -## Relevant Articles: -- [Java Web Application Without Web.xml] diff --git a/javax-servlets-3/pom.xml b/javax-servlets-3/pom.xml deleted file mode 100644 index 8c339ce6d4..0000000000 --- a/javax-servlets-3/pom.xml +++ /dev/null @@ -1,52 +0,0 @@ - - - 4.0.0 - javax-servlets-3 - 1.0-SNAPSHOT - javax-servlets-3 - war - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - - - - - - javax.servlet - javax.servlet-api - ${javax.servlet-api.version} - - - org.springframework - spring-webmvc - ${spring.version} - - - - 4.0.1 - 5.1.3.RELEASE - - - uppercasing-app - - - org.apache.maven.plugins - maven-war-plugin - 3.1.0 - - - default-war - prepare-package - - false - - - - - - - diff --git a/javax-servlets-3/src/main/java/com/baeldung/servlets3/spring/AppInitializer.java b/javax-servlets-3/src/main/java/com/baeldung/servlets3/spring/AppInitializer.java deleted file mode 100644 index 837d439cf4..0000000000 --- a/javax-servlets-3/src/main/java/com/baeldung/servlets3/spring/AppInitializer.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.baeldung.servlets3.spring; - -import com.baeldung.servlets3.spring.config.AppConfig; -import org.springframework.web.WebApplicationInitializer; -import org.springframework.web.context.ContextLoaderListener; -import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; -import org.springframework.web.servlet.DispatcherServlet; - -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletRegistration; - -public class AppInitializer implements WebApplicationInitializer { - @Override - public void onStartup(ServletContext servletContext) throws ServletException { - AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); - context.register(AppConfig.class); - - servletContext.addListener(new ContextLoaderListener(context)); - - ServletRegistration.Dynamic dispatcher = servletContext.addServlet("spring-dispatcher", - new DispatcherServlet(context)); - - dispatcher.setLoadOnStartup(1); - dispatcher.addMapping("/spring/*"); - } -} diff --git a/javax-servlets-3/src/main/java/com/baeldung/servlets3/spring/config/AppConfig.java b/javax-servlets-3/src/main/java/com/baeldung/servlets3/spring/config/AppConfig.java deleted file mode 100644 index 0088bad770..0000000000 --- a/javax-servlets-3/src/main/java/com/baeldung/servlets3/spring/config/AppConfig.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.baeldung.servlets3.spring.config; - -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; - -@Configuration -@EnableWebMvc -@ComponentScan("com.baeldung.servlets3.spring") -public class AppConfig { -} diff --git a/javax-servlets-3/src/main/java/com/baeldung/servlets3/spring/controllers/UppercaseController.java b/javax-servlets-3/src/main/java/com/baeldung/servlets3/spring/controllers/UppercaseController.java deleted file mode 100644 index 74585e6b5e..0000000000 --- a/javax-servlets-3/src/main/java/com/baeldung/servlets3/spring/controllers/UppercaseController.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baeldung.servlets3.spring.controllers; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequestMapping("/uppercase") -public class UppercaseController { - - @GetMapping(produces = "text/html") - public String getUppercase(@RequestParam(required = false)String param) { - String response = param != null ? param.toUpperCase() : "Missing param"; - return "From Spring: " + response; - } -} diff --git a/javax-servlets-3/src/main/java/com/baeldung/servlets3/web/filters/EmptyParamFilter.java b/javax-servlets/src/main/java/com/baeldung/filters/EmptyParamFilter.java similarity index 88% rename from javax-servlets-3/src/main/java/com/baeldung/servlets3/web/filters/EmptyParamFilter.java rename to javax-servlets/src/main/java/com/baeldung/filters/EmptyParamFilter.java index 61a7e896cc..b0b5392237 100644 --- a/javax-servlets-3/src/main/java/com/baeldung/servlets3/web/filters/EmptyParamFilter.java +++ b/javax-servlets/src/main/java/com/baeldung/filters/EmptyParamFilter.java @@ -1,4 +1,4 @@ -package com.baeldung.servlets3.web.filters; +package com.baeldung.filters; import javax.servlet.Filter; import javax.servlet.FilterChain; @@ -9,7 +9,7 @@ import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import java.io.IOException; -@WebFilter(servletNames = { "uppercaseServlet" }, urlPatterns = "/uppercase") +@WebFilter(urlPatterns = "/uppercase") public class EmptyParamFilter implements Filter { @Override diff --git a/javax-servlets-3/src/main/java/com/baeldung/servlets3/web/listeners/AppListener.java b/javax-servlets/src/main/java/com/baeldung/listeners/AppListener.java similarity index 91% rename from javax-servlets-3/src/main/java/com/baeldung/servlets3/web/listeners/AppListener.java rename to javax-servlets/src/main/java/com/baeldung/listeners/AppListener.java index d61af65c31..ed16dd1654 100644 --- a/javax-servlets-3/src/main/java/com/baeldung/servlets3/web/listeners/AppListener.java +++ b/javax-servlets/src/main/java/com/baeldung/listeners/AppListener.java @@ -1,4 +1,4 @@ -package com.baeldung.servlets3.web.listeners; +package com.baeldung.listeners; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; diff --git a/javax-servlets-3/src/main/java/com/baeldung/servlets3/web/listeners/RequestListener.java b/javax-servlets/src/main/java/com/baeldung/listeners/RequestListener.java similarity index 94% rename from javax-servlets-3/src/main/java/com/baeldung/servlets3/web/listeners/RequestListener.java rename to javax-servlets/src/main/java/com/baeldung/listeners/RequestListener.java index aeebf482fb..7f0c37b666 100644 --- a/javax-servlets-3/src/main/java/com/baeldung/servlets3/web/listeners/RequestListener.java +++ b/javax-servlets/src/main/java/com/baeldung/listeners/RequestListener.java @@ -1,4 +1,4 @@ -package com.baeldung.servlets3.web.listeners; +package com.baeldung.listeners; import javax.servlet.ServletContext; import javax.servlet.ServletRequestEvent; diff --git a/javax-servlets-3/src/main/java/com/baeldung/servlets3/web/servlets/CounterServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/CounterServlet.java similarity index 78% rename from javax-servlets-3/src/main/java/com/baeldung/servlets3/web/servlets/CounterServlet.java rename to javax-servlets/src/main/java/com/baeldung/servlets/CounterServlet.java index 4bb92bbf77..b9ea55de73 100644 --- a/javax-servlets-3/src/main/java/com/baeldung/servlets3/web/servlets/CounterServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/CounterServlet.java @@ -1,4 +1,4 @@ -package com.baeldung.servlets3.web.servlets; +package com.baeldung.servlets; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; @@ -10,10 +10,6 @@ import java.io.PrintWriter; @WebServlet(urlPatterns = "/counter", name = "counterServlet") public class CounterServlet extends HttpServlet { - public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { - doGet(request, response); - } - public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html"); diff --git a/javax-servlets-3/src/main/java/com/baeldung/servlets3/web/servlets/UppercaseServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/UppercaseServlet.java similarity index 78% rename from javax-servlets-3/src/main/java/com/baeldung/servlets3/web/servlets/UppercaseServlet.java rename to javax-servlets/src/main/java/com/baeldung/servlets/UppercaseServlet.java index 9b948cd994..0357ab28b5 100644 --- a/javax-servlets-3/src/main/java/com/baeldung/servlets3/web/servlets/UppercaseServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/UppercaseServlet.java @@ -1,4 +1,4 @@ -package com.baeldung.servlets3.web.servlets; +package com.baeldung.servlets; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; @@ -10,10 +10,6 @@ import java.io.PrintWriter; @WebServlet(urlPatterns = "/uppercase", name = "uppercaseServlet") public class UppercaseServlet extends HttpServlet { - public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { - doGet(request, response); - } - public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { String inputString = request.getParameter("input").toUpperCase();