Skip to content

Commit 8576967

Browse files
committed
Cleanup for Servlet error mapping and adding test
Signed-off-by: arjantijms <arjan.tijms@gmail.com>
1 parent 2c1ccfb commit 8576967

File tree

7 files changed

+141
-97
lines changed

7 files changed

+141
-97
lines changed

servlet/cookies/src/test/java/org/javaee7/servlet/cookies/SimpleServletTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ public void testCookies() throws IOException, SAXException {
5151
page = webClient.getPage(base + "TestServlet");
5252

5353
assertTrue(page.asText().contains("Found cookie: myCookieKey Found cookie: myHttpOnlyCookieKey"));
54-
55-
page = webClient.getPage(base + "ClientCookieServlet");
56-
webClient.waitForBackgroundJavaScript(1000);
57-
58-
System.out.println(page.asText());
59-
6054
}
6155

6256
@Test
@@ -69,6 +63,8 @@ public void testHttpOnlyCookies() throws IOException, SAXException {
6963
page = webClient.getPage(base + "ClientCookieServlet");
7064
webClient.waitForBackgroundJavaScript(1000);
7165

66+
System.out.println(page.asText());
67+
7268
assertTrue(page.asText().contains("myCookieKey"));
7369
assertFalse(page.asText().contains("myHttpOnlyCookieKey"));
7470

servlet/error-mapping/pom.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
<groupId>org.javaee7</groupId>
77
<artifactId>servlet</artifactId>
88
<version>1.0-SNAPSHOT</version>
9-
<relativePath>../pom.xml</relativePath>
109
</parent>
1110

12-
<groupId>org.javaee7</groupId>
1311
<artifactId>servlet-error-mapping</artifactId>
14-
<version>1.0-SNAPSHOT</version>
1512
<packaging>war</packaging>
1613

1714
<name>Java EE 7 Sample: servlet - error-mapping</name>

servlet/error-mapping/src/main/webapp/error-404.jsp renamed to servlet/error-mapping/src/main/java/org/javaee7/servlet/error/mapping/ErrorServlet.java

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<!--
21
/*
32
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
43
*
@@ -38,19 +37,36 @@
3837
* only if the new code is made subject to such option by the copyright
3938
* holder.
4039
*/
41-
-->
42-
<%@page contentType="text/html" pageEncoding="UTF-8"%>
43-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
44-
"http://www.w3.org/TR/html4/loose.dtd">
40+
package org.javaee7.servlet.error.mapping;
4541

46-
<html>
47-
<head>
48-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
49-
<title>Error Mapping Sample - 404 page not found</title>
50-
</head>
51-
<body>
52-
<h1>Error Mapping Sample - 404 page not found</h1>
53-
54-
Go <a href="${pageContext.request.contextPath}/index.jsp">home</a>.
55-
</body>
56-
</html>
42+
import java.io.IOException;
43+
44+
import javax.servlet.ServletException;
45+
import javax.servlet.annotation.WebServlet;
46+
import javax.servlet.http.HttpServlet;
47+
import javax.servlet.http.HttpServletRequest;
48+
import javax.servlet.http.HttpServletResponse;
49+
50+
/**
51+
* @author Arun Gupta
52+
* @author Arjan Tijms
53+
*/
54+
@WebServlet("/error")
55+
public class ErrorServlet extends HttpServlet {
56+
57+
private static final long serialVersionUID = -3681304530279446784L;
58+
59+
/**
60+
* Handles the HTTP <code>GET</code> method.
61+
*
62+
* @param request servlet request
63+
* @param response servlet response
64+
* @throws ServletException if a servlet-specific error occurs
65+
* @throws IOException if an I/O error occurs
66+
*/
67+
@Override
68+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
69+
response.getWriter().write("!error!");
70+
}
71+
72+
}

servlet/error-mapping/src/main/webapp/error-exception.jsp renamed to servlet/error-mapping/src/main/java/org/javaee7/servlet/error/mapping/NotFoundServlet.java

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<!--
21
/*
32
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
43
*
@@ -38,21 +37,35 @@
3837
* only if the new code is made subject to such option by the copyright
3938
* holder.
4039
*/
41-
-->
42-
<%@page contentType="text/html" pageEncoding="UTF-8"%>
43-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
44-
"http://www.w3.org/TR/html4/loose.dtd">
40+
package org.javaee7.servlet.error.mapping;
4541

46-
<html>
47-
<head>
48-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
49-
<title>Error Mapping Sample - Exception Mapping</title>
50-
</head>
51-
<body>
52-
<h1>Error Mapping Sample - Exception Mapping</h1>
53-
54-
This page is shown when a java.lang.RuntimeException is thrown.<br><br>
55-
56-
Go <a href="${pageContext.request.contextPath}/index.jsp">home</a>.
57-
</body>
58-
</html>
42+
import java.io.IOException;
43+
44+
import javax.servlet.ServletException;
45+
import javax.servlet.annotation.WebServlet;
46+
import javax.servlet.http.HttpServlet;
47+
import javax.servlet.http.HttpServletRequest;
48+
import javax.servlet.http.HttpServletResponse;
49+
50+
/**
51+
* @author Arun Gupta
52+
* @author Arjan Tijms
53+
*/
54+
@WebServlet("/notfound")
55+
public class NotFoundServlet extends HttpServlet {
56+
57+
private static final long serialVersionUID = -3681304530279446784L;
58+
59+
/**
60+
* Handles the HTTP <code>GET</code> method.
61+
*
62+
* @param request servlet request
63+
* @param response servlet response
64+
* @throws ServletException if a servlet-specific error occurs
65+
* @throws IOException if an I/O error occurs
66+
*/
67+
@Override
68+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
69+
response.getWriter().write("!not found!");
70+
}
71+
}

servlet/error-mapping/src/main/java/org/javaee7/servlet/error/mapping/TestServlet.java

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,20 @@
4040
package org.javaee7.servlet.error.mapping;
4141

4242
import java.io.IOException;
43-
import java.io.PrintWriter;
43+
4444
import javax.servlet.ServletException;
4545
import javax.servlet.http.HttpServlet;
4646
import javax.servlet.http.HttpServletRequest;
4747
import javax.servlet.http.HttpServletResponse;
4848

4949
/**
5050
* @author Arun Gupta
51+
* @author Arjan Tijms
5152
*/
5253
public class TestServlet extends HttpServlet {
5354

54-
/**
55-
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
56-
* methods.
57-
*
58-
* @param request servlet request
59-
* @param response servlet response
60-
* @throws ServletException if a servlet-specific error occurs
61-
* @throws IOException if an I/O error occurs
62-
*/
63-
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
64-
throws ServletException, IOException {
65-
throw new RuntimeException();
66-
}
55+
private static final long serialVersionUID = -3681304530279446784L;
6756

68-
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
6957
/**
7058
* Handles the HTTP <code>GET</code> method.
7159
*
@@ -75,33 +63,8 @@ protected void processRequest(HttpServletRequest request, HttpServletResponse re
7563
* @throws IOException if an I/O error occurs
7664
*/
7765
@Override
78-
protected void doGet(HttpServletRequest request, HttpServletResponse response)
79-
throws ServletException, IOException {
80-
processRequest(request, response);
81-
}
82-
83-
/**
84-
* Handles the HTTP <code>POST</code> method.
85-
*
86-
* @param request servlet request
87-
* @param response servlet response
88-
* @throws ServletException if a servlet-specific error occurs
89-
* @throws IOException if an I/O error occurs
90-
*/
91-
@Override
92-
protected void doPost(HttpServletRequest request, HttpServletResponse response)
93-
throws ServletException, IOException {
94-
processRequest(request, response);
66+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
67+
throw new RuntimeException();
9568
}
9669

97-
/**
98-
* Returns a short description of the servlet.
99-
*
100-
* @return a String containing servlet description
101-
*/
102-
@Override
103-
public String getServletInfo() {
104-
return "Short description";
105-
}// </editor-fold>
106-
10770
}

servlet/error-mapping/src/main/webapp/WEB-INF/web.xml

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@
4040
* holder.
4141
*/
4242
-->
43-
<web-app
44-
version="3.1"
45-
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
46-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
47-
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
43+
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
4844
<servlet>
4945
<servlet-name>TestServlet</servlet-name>
5046
<servlet-class>org.javaee7.servlet.error.mapping.TestServlet</servlet-class>
@@ -53,17 +49,13 @@
5349
<servlet-name>TestServlet</servlet-name>
5450
<url-pattern>/TestServlet</url-pattern>
5551
</servlet-mapping>
56-
<session-config>
57-
<session-timeout>
58-
30
59-
</session-timeout>
60-
</session-config>
52+
6153
<error-page>
6254
<error-code>404</error-code>
63-
<location>/error-404.jsp</location>
55+
<location>/notfound</location>
6456
</error-page>
6557
<error-page>
6658
<exception-type>java.lang.RuntimeException</exception-type>
67-
<location>/error-exception.jsp</location>
59+
<location>/error</location>
6860
</error-page>
6961
</web-app>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.javaee7.servlet.error.mapping;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.net.URL;
8+
9+
import org.jboss.arquillian.container.test.api.Deployment;
10+
import org.jboss.arquillian.junit.Arquillian;
11+
import org.jboss.arquillian.test.api.ArquillianResource;
12+
import org.jboss.shrinkwrap.api.ShrinkWrap;
13+
import org.jboss.shrinkwrap.api.spec.WebArchive;
14+
import org.junit.Before;
15+
import org.junit.Test;
16+
import org.junit.runner.RunWith;
17+
import org.xml.sax.SAXException;
18+
19+
import com.gargoylesoftware.htmlunit.TextPage;
20+
import com.gargoylesoftware.htmlunit.WebClient;
21+
22+
/**
23+
* @author Arjan Tijms
24+
*/
25+
@RunWith(Arquillian.class)
26+
public class ErrorMappingTest {
27+
28+
private static final String WEBAPP_SRC = "src/main/webapp";
29+
30+
@ArquillianResource
31+
private URL base;
32+
33+
private WebClient webClient;
34+
35+
@Deployment(testable = false)
36+
public static WebArchive createDeployment() {
37+
return ShrinkWrap.create(WebArchive.class)
38+
.addAsWebInfResource((new File(WEBAPP_SRC + "/WEB-INF", "web.xml")))
39+
.addClasses(
40+
TestServlet.class,
41+
ErrorServlet.class,
42+
NotFoundServlet.class);
43+
}
44+
45+
@Before
46+
public void setup() {
47+
webClient = new WebClient();
48+
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
49+
}
50+
51+
@Test
52+
public void testError() throws IOException, SAXException {
53+
TextPage page = webClient.getPage(base + "TestServlet");
54+
55+
System.out.println(page.getContent());
56+
57+
assertTrue(page.getContent().contains("!error!"));
58+
}
59+
60+
@Test
61+
public void test404() throws IOException, SAXException {
62+
TextPage page = webClient.getPage(base + "does-not-exist");
63+
64+
assertTrue(page.getContent().contains("!not found!"));
65+
}
66+
67+
}

0 commit comments

Comments
 (0)