Skip to content

Commit 74a13b2

Browse files
committed
Merge pull request javaee-samples#321 from orelgenya/master
programmatic servlet registration fix
2 parents 2b478e8 + 90efe3f commit 74a13b2

File tree

5 files changed

+59
-180
lines changed

5 files changed

+59
-180
lines changed

servlet/programmatic-registration/src/main/java/org/javaee7/servlet/programmatic/registration/ChildServlet.java

Lines changed: 0 additions & 78 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.javaee7.servlet.programmatic.registration;
2+
3+
import javax.servlet.ServletException;
4+
import javax.servlet.http.HttpServlet;
5+
import javax.servlet.http.HttpServletRequest;
6+
import javax.servlet.http.HttpServletResponse;
7+
import java.io.IOException;
8+
9+
/**
10+
* @author OrelGenya
11+
*/
12+
public class DynamicServlet extends HttpServlet {
13+
14+
@Override
15+
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
16+
resp.getWriter().print("dynamic GET");
17+
}
18+
19+
@Override
20+
public String getServletInfo() {
21+
return "My dynamic awesome servlet!";
22+
}
23+
}

servlet/programmatic-registration/src/main/java/org/javaee7/servlet/programmatic/registration/ParentServlet.java

Lines changed: 0 additions & 81 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.javaee7.servlet.programmatic.registration;
2+
3+
import javax.servlet.ServletContextEvent;
4+
import javax.servlet.ServletContextListener;
5+
import javax.servlet.ServletRegistration;
6+
import javax.servlet.annotation.WebListener;
7+
8+
/**
9+
* @author OrelGenya
10+
*/
11+
@WebListener
12+
public class SimpleServletContextListener implements ServletContextListener {
13+
14+
@Override
15+
public void contextInitialized(ServletContextEvent sce) {
16+
System.out.println("Servlet context initialized: " + sce.getServletContext().getContextPath());
17+
ServletRegistration.Dynamic registration = sce.getServletContext().addServlet("dynamic", DynamicServlet.class);
18+
registration.addMapping("dynamic");
19+
}
20+
21+
@Override
22+
public void contextDestroyed(ServletContextEvent sce) {
23+
System.out.println("Servlet context destroyed: " + sce.getServletContext().getContextPath());
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
package org.javaee7.servlet.programmatic.registration;
22

3-
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
4-
import com.gargoylesoftware.htmlunit.HttpMethod;
53
import com.gargoylesoftware.htmlunit.TextPage;
64
import com.gargoylesoftware.htmlunit.WebClient;
7-
import com.gargoylesoftware.htmlunit.WebRequest;
8-
import java.io.File;
9-
import java.io.IOException;
10-
import java.net.URL;
115
import org.jboss.arquillian.container.test.api.Deployment;
126
import org.jboss.arquillian.junit.Arquillian;
137
import org.jboss.arquillian.test.api.ArquillianResource;
148
import org.jboss.shrinkwrap.api.ShrinkWrap;
159
import org.jboss.shrinkwrap.api.spec.WebArchive;
1610
import org.junit.Before;
1711
import org.junit.Test;
18-
import static org.junit.Assert.*;
1912
import org.junit.runner.RunWith;
2013
import org.xml.sax.SAXException;
2114

15+
import java.io.IOException;
16+
import java.net.URL;
17+
18+
import static org.junit.Assert.assertEquals;
19+
2220
/**
23-
* @author arungupta
21+
* @author OrelGenya
2422
*/
2523
@RunWith(Arquillian.class)
26-
public class ServletTest {
24+
public class DynamicServletTest {
2725

2826
@ArquillianResource
2927
private URL base;
@@ -33,8 +31,8 @@ public class ServletTest {
3331
@Deployment(testable = false)
3432
public static WebArchive createDeployment() {
3533
WebArchive war = ShrinkWrap.create(WebArchive.class).
36-
addClass(ParentServlet.class).
37-
addClass(ChildServlet.class);
34+
addClass(DynamicServlet.class).
35+
addClass(SimpleServletContextListener.class);
3836
return war;
3937
}
4038

@@ -45,15 +43,7 @@ public void setup() {
4543

4644
@Test
4745
public void testChildServlet() throws IOException, SAXException {
48-
try {
49-
webClient.getPage(base + "/ChildServlet");
50-
} catch (FailingHttpStatusCodeException e) {
51-
assertNotNull(e);
52-
assertEquals(404, e.getStatusCode());
53-
return;
54-
}
55-
fail("/ChildSevlet could be accessed with programmatic registration");
56-
webClient.getPage(base + "/ParentServlet");
57-
webClient.getPage(base + "/ChildServlet");
46+
TextPage page = webClient.getPage(base + "dynamic");
47+
assertEquals("dynamic GET", page.getContent());
5848
}
5949
}

0 commit comments

Comments
 (0)