java web application without web.xml - servlets 3.0
This commit is contained in:
parent
de80339a2a
commit
445fee41b3
6
javax-servlets-3/.gitignore
vendored
Normal file
6
javax-servlets-3/.gitignore
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
# Created by .ignore support plugin (hsz.mobi)
|
||||||
|
.idea
|
||||||
|
classes
|
||||||
|
target
|
||||||
|
*.iml
|
||||||
|
out
|
2
javax-servlets-3/Dockerfile
Normal file
2
javax-servlets-3/Dockerfile
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
FROM tomcat
|
||||||
|
ADD ./target/javax-servlets-3-1.0-SNAPSHOT.war /usr/local/tomcat/webapps/
|
9
javax-servlets-3/README.md
Normal file
9
javax-servlets-3/README.md
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
## 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]
|
51
javax-servlets-3/pom.xml
Normal file
51
javax-servlets-3/pom.xml
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
<?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>
|
||||||
|
<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>
|
@ -0,0 +1,27 @@
|
|||||||
|
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/*");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
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 {
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.baeldung.servlets3.web.filters;
|
||||||
|
|
||||||
|
import javax.servlet.Filter;
|
||||||
|
import javax.servlet.FilterChain;
|
||||||
|
import javax.servlet.FilterConfig;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.ServletRequest;
|
||||||
|
import javax.servlet.ServletResponse;
|
||||||
|
import javax.servlet.annotation.WebFilter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
|
@WebFilter(servletNames = { "uppercaseServlet" }, filterName = "emptyParamFilter")
|
||||||
|
public class EmptyParamFilter implements Filter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(FilterConfig filterConfig) throws ServletException {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
|
||||||
|
FilterChain filterChain) throws IOException, ServletException {
|
||||||
|
String inputString = servletRequest.getParameter("input");
|
||||||
|
|
||||||
|
if (inputString == null || inputString.isEmpty()) {
|
||||||
|
response(servletResponse);
|
||||||
|
} else {
|
||||||
|
filterChain.doFilter(servletRequest, servletResponse);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void response(ServletResponse response) throws IOException {
|
||||||
|
response.setContentType("text/html");
|
||||||
|
|
||||||
|
PrintWriter out = response.getWriter();
|
||||||
|
|
||||||
|
out.println("Missing input parameter");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destroy() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.baeldung.servlets3.web.listeners;
|
||||||
|
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
import javax.servlet.ServletContextEvent;
|
||||||
|
import javax.servlet.ServletContextListener;
|
||||||
|
import javax.servlet.annotation.WebListener;
|
||||||
|
|
||||||
|
@WebListener
|
||||||
|
public class AppListener implements ServletContextListener {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void contextInitialized(ServletContextEvent event) {
|
||||||
|
ServletContext context = event.getServletContext();
|
||||||
|
context.setAttribute("counter", 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void contextDestroyed(ServletContextEvent event) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.baeldung.servlets3.web.listeners;
|
||||||
|
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
import javax.servlet.ServletRequestEvent;
|
||||||
|
import javax.servlet.ServletRequestListener;
|
||||||
|
import javax.servlet.annotation.WebListener;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
@WebListener
|
||||||
|
public class RequestListener implements ServletRequestListener {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void requestInitialized(ServletRequestEvent event) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void requestDestroyed(ServletRequestEvent event) {
|
||||||
|
HttpServletRequest request = (HttpServletRequest)event.getServletRequest();
|
||||||
|
if (!request.getServletPath().equals("/counter")) {
|
||||||
|
ServletContext context = event.getServletContext();
|
||||||
|
context.setAttribute("counter", (int)context.getAttribute("counter") + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.servlets3.web.servlets;
|
||||||
|
|
||||||
|
import javax.servlet.annotation.WebServlet;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
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");
|
||||||
|
|
||||||
|
PrintWriter out = response.getWriter();
|
||||||
|
|
||||||
|
int count = (int)request.getServletContext().getAttribute("counter");
|
||||||
|
|
||||||
|
out.println("Request counter: " + count);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.baeldung.servlets3.web.servlets;
|
||||||
|
|
||||||
|
import javax.servlet.annotation.WebServlet;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
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();
|
||||||
|
|
||||||
|
response.setContentType("text/html");
|
||||||
|
|
||||||
|
PrintWriter out = response.getWriter();
|
||||||
|
|
||||||
|
out.println(inputString);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user