Merge branch 'master' into master

This commit is contained in:
Loredana Crusoveanu
2022-04-25 18:02:24 +03:00
committed by GitHub
366 changed files with 3085 additions and 1995 deletions

View File

@@ -2,3 +2,9 @@
This module contains articles about Spring Web MVC in Spring Boot projects.
### Relevant Articles:
- [How to Register a Servlet in Java](https://www.baeldung.com/register-servlet)
- [Guide to Spring WebUtils and ServletRequestUtils](https://www.baeldung.com/spring-webutils-servletrequestutils)
- [Configure a Spring Boot Web Application](https://www.baeldung.com/spring-boot-application-configuration)
- [A Guide to Spring in Eclipse STS](https://www.baeldung.com/eclipse-sts-spring)

View File

@@ -2,24 +2,28 @@
<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>spring-boot-mvc-4</artifactId>
<name>spring-boot-mvc-4</name>
<packaging>jar</packaging>
<description>Module For Spring Boot</description>
<description>Module For Spring Boot MVC Web</description>
<parent>
<groupId>com.baeldung.spring-boot-modules</groupId>
<artifactId>spring-boot-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
@@ -38,6 +42,19 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
</dependencies>
<build>

View File

@@ -0,0 +1,43 @@
package com.baeldung.boot.controller.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Objects;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public HelloWorldServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = null;
try {
out = response.getWriter();
out.println("HelloWorldServlet: GET METHOD");
out.flush();
} finally {
if (!Objects.isNull(out))
out.close();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = null;
try {
out = response.getWriter();
out.println("HelloWorldServlet: POST METHOD");
out.flush();
} finally {
if (!Objects.isNull(out))
out.close();
}
}
}

View File

@@ -0,0 +1,43 @@
package com.baeldung.boot.controller.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Objects;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SpringHelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public SpringHelloWorldServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = null;
try {
out = response.getWriter();
out.println("SpringHelloWorldServlet: GET METHOD");
out.flush();
} finally {
if (!Objects.isNull(out))
out.close();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = null;
try {
out = response.getWriter();
out.println("SpringHelloWorldServlet: POST METHOD");
out.flush();
} finally {
if (!Objects.isNull(out))
out.close();
}
}
}

View File

@@ -0,0 +1,15 @@
package com.baeldung.common.error;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.GetMapping;
public class MyCustomErrorController implements ErrorController {
private static final String PATH = "/error";
@GetMapping(value = PATH)
public String error() {
return "Error haven";
}
}

View File

@@ -0,0 +1,15 @@
package com.baeldung.common.error;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import javax.servlet.Servlet;
public class SpringHelloServletRegistrationBean extends ServletRegistrationBean {
public SpringHelloServletRegistrationBean() {
}
public SpringHelloServletRegistrationBean(Servlet servlet, String... urlMappings) {
super(servlet, urlMappings);
}
}

View File

@@ -0,0 +1,22 @@
package com.baeldung.common.error.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ErrorController {
public ErrorController() {
}
@GetMapping("/400")
String error400() {
return "Error Code: 400 occured.";
}
@GetMapping("/errorHaven")
String errorHeaven() {
return "You have reached the haven of errors!!!";
}
}

View File

@@ -0,0 +1,25 @@
package com.baeldung.common.properties;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
@Component
public class MyServletContainerCustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
public MyServletContainerCustomizationBean() {
}
@Override
public void customize(ConfigurableServletWebServerFactory container) {
container.setPort(8084);
container.setContextPath("/springbootapp");
container.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400"));
container.addErrorPages(new ErrorPage("/errorHaven"));
}
}

View File

@@ -0,0 +1,28 @@
package com.baeldung.common.resources;
import org.springframework.boot.ExitCodeGenerator;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
public class ExecutorServiceExitCodeGenerator implements ExitCodeGenerator {
private ExecutorService executorService;
public ExecutorServiceExitCodeGenerator(ExecutorService executorService) {
}
@Override
public int getExitCode() {
try {
if (!Objects.isNull(executorService)) {
executorService.shutdownNow();
return 1;
}
return 0;
} catch (SecurityException ex) {
return 0;
}
}
}

View File

@@ -0,0 +1,63 @@
package com.baeldung.main;
import com.baeldung.boot.controller.servlet.HelloWorldServlet;
import com.baeldung.boot.controller.servlet.SpringHelloWorldServlet;
import com.baeldung.common.error.SpringHelloServletRegistrationBean;
import com.baeldung.common.resources.ExecutorServiceExitCodeGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@RestController
@EnableAutoConfiguration
@ComponentScan({ "com.baeldung.common.error", "com.baeldung.common.error.controller", "com.baeldung.common.properties", "com.baeldung.common.resources", "com.baeldung.endpoints", "com.baeldung.service", "com.baeldung.monitor.jmx", "com.baeldung.boot.config" })
public class SpringBootApplication {
private static ApplicationContext applicationContext;
@GetMapping("/")
String home() {
return "TADA!!! You are in Spring Boot Actuator test application.";
}
public static void main(String[] args) {
applicationContext = SpringApplication.run(SpringBootApplication.class, args);
}
@Bean
public ExecutorService executorService() {
return Executors.newFixedThreadPool(10);
}
@Bean
public HelloWorldServlet helloWorldServlet() {
return new HelloWorldServlet();
}
@Bean
public SpringHelloServletRegistrationBean servletRegistrationBean() {
SpringHelloServletRegistrationBean bean = new SpringHelloServletRegistrationBean(new SpringHelloWorldServlet(), "/springHelloWorld/*");
bean.setLoadOnStartup(1);
bean.addInitParameter("message", "SpringHelloWorldServlet special message");
return bean;
}
@Bean
@Autowired
public ExecutorServiceExitCodeGenerator executorServiceExitCodeGenerator(ExecutorService executorService) {
return new ExecutorServiceExitCodeGenerator(executorService);
}
public void shutDown(ExecutorServiceExitCodeGenerator executorServiceExitCodeGenerator) {
SpringApplication.exit(applicationContext, executorServiceExitCodeGenerator);
}
}

View File

@@ -0,0 +1,19 @@
package com.baeldung.servlets;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.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);
}
}

View File

@@ -0,0 +1,32 @@
package com.baeldung.servlets.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("/");
}
}

View File

@@ -0,0 +1,38 @@
package com.baeldung.servlets.configuration;
import org.springframework.boot.web.servlet.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.WebMvcConfigurer;
import org.springframework.web.servlet.resource.PathResourceResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
public class WebMvcConfigure implements WebMvcConfigurer {
@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();
}
}

View File

@@ -0,0 +1,20 @@
package com.baeldung.servlets.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");
}

View File

@@ -0,0 +1,27 @@
package com.baeldung.servlets.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;
}
}

View File

@@ -0,0 +1,24 @@
package com.baeldung.servlets.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.servlets.*" })
@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);
}
}

View File

@@ -0,0 +1,18 @@
package com.baeldung.servlets.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>");
}
}

View File

@@ -0,0 +1,18 @@
package com.baeldung.servlets.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);
}
}

View File

@@ -0,0 +1,20 @@
package com.baeldung.servlets.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>");
}
}

View File

@@ -0,0 +1,17 @@
package com.baeldung.servlets.servlets.springboot;
import com.baeldung.servlets.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> genericCustomServlet() {
ServletRegistrationBean<GenericCustomServlet> bean = new ServletRegistrationBean<>(new GenericCustomServlet(), "/springregistrationbeanservlet/*");
bean.setLoadOnStartup(1);
return bean;
}
}

View File

@@ -0,0 +1,16 @@
package com.baeldung.servlets.servlets.springboot.embedded;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class EmbeddedTomcatExample {
@Bean
public ConfigurableServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
return tomcat;
}
}

View File

@@ -0,0 +1,18 @@
package com.baeldung.utils;
import javax.annotation.security.RolesAllowed;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "com.baeldung.utils")
public class UtilsApplication {
@RolesAllowed("*")
public static void main(String[] args) {
SpringApplication.run(UtilsApplication.class, args);
}
}

View File

@@ -0,0 +1,48 @@
package com.baeldung.utils.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.util.WebUtils;
@Controller
public class UtilsController {
@GetMapping("/utils")
public String webUtils(Model model) {
return "utils";
}
@PostMapping("/setParam")
public String post(HttpServletRequest request, Model model) {
String param = ServletRequestUtils.getStringParameter(request, "param", "DEFAULT");
// Long param = ServletRequestUtils.getLongParameter(request, "param",1L);
// boolean param = ServletRequestUtils.getBooleanParameter(request, "param", true);
// double param = ServletRequestUtils.getDoubleParameter(request, "param", 1000);
// float param = ServletRequestUtils.getFloatParameter(request, "param", (float) 1.00);
// int param = ServletRequestUtils.getIntParameter(request, "param", 100);
// try {
// ServletRequestUtils.getRequiredStringParameter(request, "param");
// } catch (ServletRequestBindingException e) {
// e.printStackTrace();
// }
WebUtils.setSessionAttribute(request, "parameter", param);
model.addAttribute("parameter", "You set: " + (String) WebUtils.getSessionAttribute(request, "parameter"));
return "utils";
}
@GetMapping("/other")
public String other(HttpServletRequest request, Model model) {
String param = (String) WebUtils.getSessionAttribute(request, "parameter");
model.addAttribute("parameter", param);
return "other";
}
}

View File

@@ -0,0 +1,30 @@
server.port=9090
server.servlet.contextPath=/springbootapp
management.server.port=8081
management.server.address=127.0.0.1
#debug=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto = update
management.endpoints.jmx.domain=Spring Sample Application
spring.jmx.unique-names=true
management.endpoints.web.exposure.include=*
management.endpoint.shutdown.enabled=true
##jolokia.config.debug=true
##endpoints.jolokia.enabled=true
##endpoints.jolokia.path=jolokia
spring.jmx.enabled=true
## for pretty printing of json when endpoints accessed over HTTP
http.mappers.jsonPrettyPrint=true
logging.level.org.springframework=INFO
#Servlet Configuration
servlet.name=dispatcherExample
servlet.mapping=/dispatcherExampleURL
contactInfoType=email

View File

@@ -0,0 +1,4 @@
dispatcher.servlet.name=dispatcherExample
dispatcher.servlet.mapping=/dispatcherExampleURL
example.servlet.name=dispatcherExample
example.servlet.mapping=/dispatcherExampleURL

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@@ -0,0 +1 @@
kill $(cat ./bin/shutdown.pid)

View File

@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Spring Utils Demo</title>
<style type="text/css">
.param{
color:green;
font-style: italic;
}
</style>
</head>
<body>
Parameter set by you: <p th:text="${parameter}" class="param"/>
</body>
</html>

View File

@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Spring Utils Demo</title>
<style type="text/css">
.param{
color:green;
font-style: italic;
}
</style>
</head>
<body>
<form action="setParam" method="POST">
<h3>Set Parameter: </h3>
<p th:text="${parameter}" class="param"/>
<input type="text" name="param" id="param"/>
<input type="submit" value="SET"/>
</form>
<br/>
<a href="other">Another Page</a>
</body>
</html>

View File

@@ -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.servlets"/>
<bean class="com.baeldung.servlets.configuration.WebAppInitializer"/>
</beans>

View File

@@ -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.servlets"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

View File

@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>JSP</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Java EE Servlets -->
<servlet>
<servlet-name>EEWebXmlServlet</servlet-name>
<servlet-class>com.baeldung.servlets.javaee.EEWebXmlServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EEWebXmlServlet</servlet-name>
<url-pattern>/eewebxmlservlet</url-pattern>
</servlet-mapping>
<!-- Spring Boot Servlets -->
<servlet>
<servlet-name>SpringBootWebXmlServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringBootWebXmlServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

View File

@@ -0,0 +1 @@
<p>Annotation Servlet!</p>

View File

@@ -0,0 +1 @@
<p>Hello!</p>

View File

@@ -0,0 +1,34 @@
package com.baeldung.utils;
import com.baeldung.utils.controller.UtilsController;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
public class UtilsControllerIntegrationTest {
@InjectMocks
private UtilsController utilsController;
private MockMvc mockMvc;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders.standaloneSetup(utilsController).build();
}
@Test
public void givenParameter_setRequestParam_andSetSessionAttribute() throws Exception {
String param = "testparam";
this.mockMvc.perform(post("/setParam").param("param", param).sessionAttr("parameter", param)).andExpect(status().isOk());
}
}