Example usage for javax.servlet.http HttpServlet HttpServlet

List of usage examples for javax.servlet.http HttpServlet HttpServlet

Introduction

In this page you can find the example usage for javax.servlet.http HttpServlet HttpServlet.

Prototype


public HttpServlet() 

Source Link

Document

Does nothing, because this is an abstract class.

Usage

From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java

public void test_ErrorPage8() throws Exception {
    Servlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override//w ww  .j  a  va2 s  . c o m
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {

            throw new RuntimeException();
        }
    };

    Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "E8");
    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/TestErrorPage8/*");
    registrations.add(getBundleContext().registerService(Servlet.class, servlet, props));
    props = new Hashtable<String, Object>();
    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "E8.error");
    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_ERROR_PAGE, RuntimeException.class.getName());
    registrations.add(getBundleContext().registerService(Servlet.class, new ErrorServlet("500"), props));

    Map<String, List<String>> response = requestAdvisor.request("TestErrorPage8/a", null);

    String responseCode = response.get("responseCode").get(0);
    String responseBody = response.get("responseBody").get(0);

    Assert.assertEquals("500", responseCode);
    Assert.assertEquals("500 : 500 : ERROR : /TestErrorPage8/a", responseBody);
}

From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java

public void test_ErrorPage9() throws Exception {
    Servlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override//from   w  w w .j av a 2s  . c om
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {

            throw new IOException();
        }
    };

    Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "E9");
    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/TestErrorPage9/*");
    registrations.add(getBundleContext().registerService(Servlet.class, servlet, props));
    props = new Hashtable<String, Object>();
    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "E9.error");
    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_ERROR_PAGE, IOException.class.getName());
    registrations.add(getBundleContext().registerService(Servlet.class, new ErrorServlet("500"), props));

    Map<String, List<String>> response = requestAdvisor.request("TestErrorPage9/a", null);

    String responseCode = response.get("responseCode").get(0);
    String responseBody = response.get("responseBody").get(0);

    Assert.assertEquals("500", responseCode);
    Assert.assertEquals("500 : 500 : ERROR : /TestErrorPage9/a", responseBody);
}

From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java

public void test_ErrorPage10() throws Exception {
    Servlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override//ww  w.j ava 2s . c  o m
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {

            resp.getWriter().write("some output");
            resp.flushBuffer();

            throw new IOException();
        }
    };

    Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "E10");
    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/TestErrorPage10/*");
    registrations.add(getBundleContext().registerService(Servlet.class, servlet, props));

    try {
        requestAdvisor.request("TestErrorPage10/a");
    } catch (Exception e) {
        Assert.assertTrue(e instanceof IOException);

        return;
    }

    Assert.fail("Expecting java.io.IOException: Premature EOF");
}

From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java

public void test_ErrorPage11() throws Exception {
    Servlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override//  w  w  w  .j  a v  a  2  s. c  o m
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {

            resp.sendError(403);
            resp.getOutputStream().flush();
        }
    };

    Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "E10");
    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/TestErrorPage11/*");
    registrations.add(getBundleContext().registerService(Servlet.class, servlet, props));

    try {
        requestAdvisor.request("TestErrorPage11/a");
    } catch (IOException e) {
        // This is expected because of old behavior
        // TODO is this really the correct behavior though
    }
}

From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java

public void test_Registration14() throws Exception {
    Servlet initError = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override//from ww w .  j ava2  s .c om
        public void init(ServletConfig config) throws ServletException {
            throw new ServletException("Init error.");
        }

    };
    ExtendedHttpService extendedHttpService = (ExtendedHttpService) getHttpService();
    try {
        extendedHttpService.registerServlet("/foo", initError, null, null);
        fail("Expected an init failure.");
    } catch (ServletException e) {
        //expected
        assertEquals("Wrong exception message.", "Init error.", e.getMessage());
    }
}

From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java

public void test_Registration15() throws Exception {
    Servlet initError = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override/*from  w w  w.j  av  a  2  s .  c  om*/
        public void init(ServletConfig config) throws ServletException {
            throw new IllegalStateException("Init error.");
        }

    };
    ExtendedHttpService extendedHttpService = (ExtendedHttpService) getHttpService();
    try {
        extendedHttpService.registerServlet("/foo", initError, null, null);
        fail("Expected an init failure.");
    } catch (IllegalStateException e) {
        //expected
        assertEquals("Wrong exception message.", "Init error.", e.getMessage());
    }
}

From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java

public void test_Registration18_WhiteboardServletByNameOnly() throws Exception {
    String expected = "a";
    final String servletName = "hello_servlet";
    Servlet namedServlet = new BaseServlet(expected);
    Servlet targetServlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override//w ww .  ja v a2s.c o  m
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {

            request.getServletContext().getNamedDispatcher(servletName).forward(request, response);
        }

    };

    Hashtable<String, String> props = new Hashtable<String, String>();
    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, servletName);
    registrations.add(getBundleContext().registerService(Servlet.class, namedServlet, props));

    props = new Hashtable<String, String>();
    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s");
    registrations.add(getBundleContext().registerService(Servlet.class, targetServlet, props));

    String actual = requestAdvisor.request("s");
    Assert.assertEquals(expected, actual);
}

From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java

public void test_RegistrationTCCL1() {
    final Set<String> filterTCCL = Collections.synchronizedSet(new HashSet<String>());
    final Set<String> servletTCCL = Collections.synchronizedSet(new HashSet<String>());
    Filter tcclFilter = new Filter() {

        @Override/*from   w  ww .  j  a  va 2  s  .  c o m*/
        public void init(FilterConfig filterConfig) throws ServletException {
            filterTCCL.add(Thread.currentThread().getContextClassLoader().getClass().getName());
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
            filterTCCL.add(Thread.currentThread().getContextClassLoader().getClass().getName());
            chain.doFilter(request, response);
        }

        @Override
        public void destroy() {
            filterTCCL.add(Thread.currentThread().getContextClassLoader().getClass().getName());
        }
    };
    HttpServlet tcclServlet = new HttpServlet() {

        @Override
        public void destroy() {
            super.destroy();
            servletTCCL.add(Thread.currentThread().getContextClassLoader().getClass().getName());
        }

        @Override
        public void init(ServletConfig config) throws ServletException {
            super.init(config);
            servletTCCL.add(Thread.currentThread().getContextClassLoader().getClass().getName());
        }

        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            servletTCCL.add(Thread.currentThread().getContextClassLoader().getClass().getName());
            response.getWriter().print(Thread.currentThread().getContextClassLoader().getClass().getName());
        }

    };

    ClassLoader originalTCCL = Thread.currentThread().getContextClassLoader();
    ClassLoader dummy = new ClassLoader() {
    };
    Thread.currentThread().setContextClassLoader(dummy);
    String expected = dummy.getClass().getName();
    String actual = null;
    ExtendedHttpService extendedHttpService = (ExtendedHttpService) getHttpService();
    try {
        extendedHttpService.registerFilter("/tccl", tcclFilter, null, null);
        extendedHttpService.registerServlet("/tccl", tcclServlet, null, null);
        actual = requestAdvisor.request("tccl");
    } catch (Exception e) {
        fail("Unexpected exception: " + e);
    } finally {
        Thread.currentThread().setContextClassLoader(originalTCCL);
        try {
            extendedHttpService.unregister("/tccl");
            extendedHttpService.unregisterFilter(tcclFilter);
        } catch (IllegalArgumentException e) {
            // ignore
        }
    }
    assertEquals(expected, actual);
    assertEquals("Wrong filterTCCL size: " + filterTCCL, 1, filterTCCL.size());
    assertTrue("Wrong filterTCCL: " + filterTCCL, filterTCCL.contains(expected));
    assertEquals("Wrong httpTCCL size: " + servletTCCL, 1, servletTCCL.size());
    assertTrue("Wrong servletTCCL: " + servletTCCL, servletTCCL.contains(expected));

}

From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java

public void test_Sessions01() {
    final AtomicBoolean valueBound = new AtomicBoolean(false);
    final AtomicBoolean valueUnbound = new AtomicBoolean(false);
    final HttpSessionBindingListener bindingListener = new HttpSessionBindingListener() {

        @Override//from  w w  w . j  a v  a  2s  .com
        public void valueUnbound(HttpSessionBindingEvent event) {
            valueUnbound.set(true);
        }

        @Override
        public void valueBound(HttpSessionBindingEvent event) {
            valueBound.set(true);
        }
    };
    final AtomicBoolean sessionCreated = new AtomicBoolean(false);
    final AtomicBoolean sessionDestroyed = new AtomicBoolean(false);
    HttpSessionListener sessionListener = new HttpSessionListener() {

        @Override
        public void sessionDestroyed(HttpSessionEvent se) {
            sessionDestroyed.set(true);
        }

        @Override
        public void sessionCreated(HttpSessionEvent se) {
            sessionCreated.set(true);
        }
    };
    HttpServlet sessionServlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            HttpSession session = request.getSession();
            if (session.getAttribute("test.attribute") == null) {
                session.setAttribute("test.attribute", bindingListener);
                response.getWriter().print("created");
            } else {
                session.invalidate();
                response.getWriter().print("invalidated");
            }
        }

    };
    ServiceRegistration<Servlet> servletReg = null;
    ServiceRegistration<HttpSessionListener> sessionListenerReg = null;
    Dictionary<String, Object> servletProps = new Hashtable<String, Object>();
    servletProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/sessions");
    String actual = null;
    CookieHandler previous = CookieHandler.getDefault();
    CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
    try {
        servletReg = getBundleContext().registerService(Servlet.class, sessionServlet, servletProps);
        Dictionary<String, String> listenerProps = new Hashtable<String, String>();
        listenerProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_LISTENER, "true");
        sessionListenerReg = getBundleContext().registerService(HttpSessionListener.class, sessionListener,
                listenerProps);

        sessionCreated.set(false);
        valueBound.set(false);
        sessionDestroyed.set(false);
        valueUnbound.set(false);
        // first call will create the session
        actual = requestAdvisor.request("sessions");
        assertEquals("Wrong result", "created", actual);
        assertTrue("No sessionCreated called", sessionCreated.get());
        assertTrue("No valueBound called", valueBound.get());
        assertFalse("sessionDestroyed was called", sessionDestroyed.get());
        assertFalse("valueUnbound was called", valueUnbound.get());

        sessionCreated.set(false);
        valueBound.set(false);
        sessionDestroyed.set(false);
        valueUnbound.set(false);
        // second call will invalidate the session
        actual = requestAdvisor.request("sessions");
        assertEquals("Wrong result", "invalidated", actual);
        assertFalse("sessionCreated was called", sessionCreated.get());
        assertFalse("valueBound was called", valueBound.get());
        assertTrue("No sessionDestroyed called", sessionDestroyed.get());
        assertTrue("No valueUnbound called", valueUnbound.get());

        sessionCreated.set(false);
        sessionDestroyed.set(false);
        valueBound.set(false);
        valueUnbound.set(false);
        // calling again should create the session again
        actual = requestAdvisor.request("sessions");
        assertEquals("Wrong result", "created", actual);
        assertTrue("No sessionCreated called", sessionCreated.get());
        assertTrue("No valueBound called", valueBound.get());
    } catch (Exception e) {
        fail("Unexpected exception: " + e);
    } finally {
        if (servletReg != null) {
            servletReg.unregister();
        }
        if (sessionListenerReg != null) {
            sessionListenerReg.unregister();
        }
        CookieHandler.setDefault(previous);
    }
}

From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java

public void test_Servlet12() throws Exception {
    Servlet sA = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override/*from   www  .j  ava  2 s . c  o  m*/
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {

            response.getWriter().write('a');
        }

    };

    Servlet sB = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {

            response.getWriter().write('b');
        }

    };

    HttpService httpService = getHttpService();

    HttpContext httpContext = httpService.createDefaultHttpContext();

    httpService.registerServlet("*.txt", sA, null, httpContext);
    httpService.registerServlet("/files/*.txt", sB, null, httpContext);

    Assert.assertEquals("b", requestAdvisor.request("files/help.txt"));
}