Context and Servlet Initialization Parameters (#4656)
* Initial Commit * Add source files * Add readme.md * Add web.xml * Update pom.xml * Update pom.xml * Update pom.xml * Remove project folder * Update pom.xml
This commit is contained in:
committed by
KevinGilmore
parent
acf731377e
commit
ca1a1d70fa
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.servlets;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebInitParam;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@WebServlet(name = "UserServlet", urlPatterns = {"/userServlet"}, initParams={
|
||||
@WebInitParam(name="name", value="Not provided"),
|
||||
@WebInitParam(name="email", value="Not provided")})
|
||||
public class UserServlet extends HttpServlet {
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
processRequest(request, response);
|
||||
forwardRequest(request, response, "/WEB-INF/jsp/result.jsp");
|
||||
}
|
||||
|
||||
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
request.setAttribute("name", getRequestParameter(request, "name"));
|
||||
request.setAttribute("email", getRequestParameter(request, "email"));
|
||||
request.setAttribute("province", getContextParameter("province"));
|
||||
request.setAttribute("country", getContextParameter("country"));
|
||||
}
|
||||
|
||||
protected String getRequestParameter(HttpServletRequest request, String name) {
|
||||
String param = request.getParameter(name);
|
||||
return !param.isEmpty() ? param : getInitParameter(name);
|
||||
}
|
||||
|
||||
protected String getContextParameter(String name) {
|
||||
return getServletContext().getInitParameter(name);
|
||||
}
|
||||
|
||||
protected void forwardRequest(HttpServletRequest request, HttpServletResponse response, String path)
|
||||
throws ServletException, IOException {
|
||||
request.getRequestDispatcher(path).forward(request, response);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
|
||||
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
<title>User Data</title>
|
||||
</head>
|
||||
<body>
|
||||
<h2>User Information</h2>
|
||||
<p><strong>Name:</strong> ${name}</p>
|
||||
<p><strong>Email:</strong> ${email}</p>
|
||||
<p><strong>Province:</strong> ${province}</p>
|
||||
<p><strong>Country:</strong> ${country}</p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,9 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://java.sun.com/xml/ns/javaee"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
|
||||
http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
|
||||
version="3.1">
|
||||
|
||||
<context-param>
|
||||
<param-name>province</param-name>
|
||||
<param-value>Mendoza</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>country</param-name>
|
||||
<param-value>Argentina</param-value>
|
||||
</context-param>
|
||||
<error-page>
|
||||
<error-code>404</error-code>
|
||||
<location>/error-404.html</location> <!-- /src/main/webapp/error-404.html-->
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
|
||||
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Context and Servlet Initialization Parameters</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
</head>
|
||||
<body>
|
||||
<h2>Please fill the form below:</h2>
|
||||
<form action="${pageContext.request.contextPath}/userServlet" method="post">
|
||||
<label for="name"><strong>Name:</strong></label>
|
||||
<input type="text" name="name" id="name">
|
||||
<label for="email"><strong>Email:</strong></label>
|
||||
<input type="text" name="email" id="email">
|
||||
<input type="submit" value="Send">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.servlets;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.mockito.Mockito.atLeast;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class UserServletUnitTest {
|
||||
|
||||
private static HttpServletRequest request;
|
||||
private static HttpServletResponse response;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpHttpServletRequestMockInstance() {
|
||||
request = mock(HttpServletRequest.class);
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpHttpServletResponsetMockInstance() {
|
||||
response = mock(HttpServletResponse.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHttpServletRequestMockInstance_whenCalledgetParameter_thenCalledAtLeastOnce() {
|
||||
request.getParameter("name");
|
||||
verify(request, atLeast(1)).getParameter("name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHttpServletRequestMockInstance_whenCalledgetParameter_thenOneAssertion() {
|
||||
when(request.getParameter("name")).thenReturn("username");
|
||||
assertThat(request.getParameter("name")).isEqualTo("username");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHttpServletResponseMockInstance_whenCalledgetContentType_thenCalledAtLeastOnce() {
|
||||
response.getContentType();
|
||||
verify(response, atLeast(1)).getContentType();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHttpServletResponseMockInstance_whenCalledgetContentType_thenOneAssertion() {
|
||||
when(response.getContentType()).thenReturn("text/html");
|
||||
assertThat(response.getContentType()).isEqualTo("text/html");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user