java web application without web.xml - servlets 3.0

This commit is contained in:
Joel Juarez 2019-02-21 23:56:32 +01:00
parent fc88c99537
commit c636b0d4b7
12 changed files with 6 additions and 139 deletions

View File

@ -1,6 +0,0 @@
# Created by .ignore support plugin (hsz.mobi)
.idea
classes
target
*.iml
out

View File

@ -1,2 +0,0 @@
FROM tomcat
ADD ./target/uppercasing-app.war /usr/local/tomcat/webapps/

View File

@ -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]

View File

@ -1,52 +0,0 @@
<?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>
<artifactId>javax-servlets-3</artifactId>
<version>1.0-SNAPSHOT</version>
<name>javax-servlets-3</name>
<packaging>war</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet-api.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<properties>
<javax.servlet-api.version>4.0.1</javax.servlet-api.version>
<spring.version>5.1.3.RELEASE</spring.version>
</properties>
<build>
<finalName>uppercasing-app</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>default-war</id>
<phase>prepare-package</phase>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -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/*");
}
}

View File

@ -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 {
}

View File

@ -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;
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.servlets3.web.filters; package com.baeldung.filters;
import javax.servlet.Filter; import javax.servlet.Filter;
import javax.servlet.FilterChain; import javax.servlet.FilterChain;
@ -9,7 +9,7 @@ import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebFilter;
import java.io.IOException; import java.io.IOException;
@WebFilter(servletNames = { "uppercaseServlet" }, urlPatterns = "/uppercase") @WebFilter(urlPatterns = "/uppercase")
public class EmptyParamFilter implements Filter { public class EmptyParamFilter implements Filter {
@Override @Override

View File

@ -1,4 +1,4 @@
package com.baeldung.servlets3.web.listeners; package com.baeldung.listeners;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextEvent;

View File

@ -1,4 +1,4 @@
package com.baeldung.servlets3.web.listeners; package com.baeldung.listeners;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestEvent;

View File

@ -1,4 +1,4 @@
package com.baeldung.servlets3.web.servlets; package com.baeldung.servlets;
import javax.servlet.annotation.WebServlet; import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
@ -10,10 +10,6 @@ import java.io.PrintWriter;
@WebServlet(urlPatterns = "/counter", name = "counterServlet") @WebServlet(urlPatterns = "/counter", name = "counterServlet")
public class CounterServlet extends HttpServlet { 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 { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html"); response.setContentType("text/html");

View File

@ -1,4 +1,4 @@
package com.baeldung.servlets3.web.servlets; package com.baeldung.servlets;
import javax.servlet.annotation.WebServlet; import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
@ -10,10 +10,6 @@ import java.io.PrintWriter;
@WebServlet(urlPatterns = "/uppercase", name = "uppercaseServlet") @WebServlet(urlPatterns = "/uppercase", name = "uppercaseServlet")
public class UppercaseServlet extends HttpServlet { 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 { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String inputString = request.getParameter("input").toUpperCase(); String inputString = request.getParameter("input").toUpperCase();