Skip to content

programmatic servlet registration fix #321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.javaee7.servlet.programmatic.registration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author OrelGenya
*/
public class DynamicServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().print("dynamic GET");
}

@Override
public String getServletInfo() {
return "My dynamic awesome servlet!";
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.javaee7.servlet.programmatic.registration;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRegistration;
import javax.servlet.annotation.WebListener;

/**
* @author OrelGenya
*/
@WebListener
public class SimpleServletContextListener implements ServletContextListener {

@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("Servlet context initialized: " + sce.getServletContext().getContextPath());
ServletRegistration.Dynamic registration = sce.getServletContext().addServlet("dynamic", DynamicServlet.class);
registration.addMapping("dynamic");
}

@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("Servlet context destroyed: " + sce.getServletContext().getContextPath());
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
package org.javaee7.servlet.programmatic.registration;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.TextPage;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.net.URL;

import static org.junit.Assert.assertEquals;

/**
* @author arungupta
* @author OrelGenya
*/
@RunWith(Arquillian.class)
public class ServletTest {
public class DynamicServletTest {

@ArquillianResource
private URL base;
Expand All @@ -33,8 +31,8 @@ public class ServletTest {
@Deployment(testable = false)
public static WebArchive createDeployment() {
WebArchive war = ShrinkWrap.create(WebArchive.class).
addClass(ParentServlet.class).
addClass(ChildServlet.class);
addClass(DynamicServlet.class).
addClass(SimpleServletContextListener.class);
return war;
}

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

@Test
public void testChildServlet() throws IOException, SAXException {
try {
webClient.getPage(base + "/ChildServlet");
} catch (FailingHttpStatusCodeException e) {
assertNotNull(e);
assertEquals(404, e.getStatusCode());
return;
}
fail("/ChildSevlet could be accessed with programmatic registration");
webClient.getPage(base + "/ParentServlet");
webClient.getPage(base + "/ChildServlet");
TextPage page = webClient.getPage(base + "dynamic");
assertEquals("dynamic GET", page.getContent());
}
}