Search in sources :

Example 16 with Servlet

use of jakarta.servlet.Servlet in project tomcat by apache.

the class JspServletWrapper method getServlet.

public Servlet getServlet() throws ServletException {
    /*
         * DCL on 'reload' requires that 'reload' be volatile
         * (this also forces a read memory barrier, ensuring the new servlet
         * object is read consistently).
         *
         * When running in non development mode with a checkInterval it is
         * possible (see BZ 62603) for a race condition to cause failures
         * if a Servlet or tag is reloaded while a compile check is running
         */
    if (getReloadInternal() || theServlet == null) {
        synchronized (this) {
            // of different pages, but not the same page.
            if (getReloadInternal() || theServlet == null) {
                // This is to maintain the original protocol.
                destroy();
                final Servlet servlet;
                try {
                    InstanceManager instanceManager = InstanceManagerFactory.getInstanceManager(config);
                    servlet = (Servlet) instanceManager.newInstance(ctxt.getFQCN(), ctxt.getJspLoader());
                } catch (Exception e) {
                    Throwable t = ExceptionUtils.unwrapInvocationTargetException(e);
                    ExceptionUtils.handleThrowable(t);
                    throw new JasperException(t);
                }
                servlet.init(config);
                if (theServlet != null) {
                    ctxt.getRuntimeContext().incrementJspReloadCount();
                }
                theServlet = servlet;
                reload = false;
            // Volatile 'reload' forces in order write of 'theServlet' and new servlet object
            }
        }
    }
    return theServlet;
}
Also used : JasperException(org.apache.jasper.JasperException) InstanceManager(org.apache.tomcat.InstanceManager) Servlet(jakarta.servlet.Servlet) UnavailableException(jakarta.servlet.UnavailableException) ServletException(jakarta.servlet.ServletException) JasperException(org.apache.jasper.JasperException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 17 with Servlet

use of jakarta.servlet.Servlet in project tomcat by apache.

the class TestAsyncContextImpl method doTestDispatchWithSpaces.

private void doTestDispatchWithSpaces(boolean async) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Context context = tomcat.addContext("", null);
    if (async) {
        Servlet s = new AsyncDispatchUrlWithSpacesServlet();
        Wrapper w = Tomcat.addServlet(context, "space", s);
        w.setAsyncSupported(true);
    } else {
        Tomcat.addServlet(context, "space", new ForwardDispatchUrlWithSpacesServlet());
    }
    context.addServletMappingDecoded("/space/*", "space");
    tomcat.start();
    ByteChunk responseBody = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/sp%61ce/foo%20bar", responseBody, null);
    Assert.assertEquals(200, rc);
}
Also used : AsyncContext(jakarta.servlet.AsyncContext) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) Wrapper(org.apache.catalina.Wrapper) ServletResponseWrapper(jakarta.servlet.ServletResponseWrapper) ServletRequestWrapper(jakarta.servlet.ServletRequestWrapper) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) Servlet(jakarta.servlet.Servlet) GenericServlet(jakarta.servlet.GenericServlet) HttpServlet(jakarta.servlet.http.HttpServlet)

Example 18 with Servlet

use of jakarta.servlet.Servlet in project tomcat by apache.

the class TestAsyncContextImpl method testAsyncContextListenerClearing.

// https://bz.apache.org/bugzilla/show_bug.cgi?id=57326
@Test
public void testAsyncContextListenerClearing() throws Exception {
    resetTracker();
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Servlet stage1 = new DispatchingServletTracking("/stage2", true);
    Wrapper wrapper1 = Tomcat.addServlet(ctx, "stage1", stage1);
    wrapper1.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/stage1", "stage1");
    Servlet stage2 = new DispatchingServletTracking("/stage3", false);
    Wrapper wrapper2 = Tomcat.addServlet(ctx, "stage2", stage2);
    wrapper2.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/stage2", "stage2");
    Servlet stage3 = new NonAsyncServlet();
    Tomcat.addServlet(ctx, "stage3", stage3);
    ctx.addServletMappingDecoded("/stage3", "stage3");
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    tomcat.start();
    getUrl("http://localhost:" + getPort() + "/stage1");
    Assert.assertEquals("doGet-startAsync-doGet-startAsync-onStartAsync-NonAsyncServletGet-onComplete-", getTrack());
    // Check the access log
    alv.validateAccessLog(1, 200, 0, REQUEST_TIME);
}
Also used : AsyncContext(jakarta.servlet.AsyncContext) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) Wrapper(org.apache.catalina.Wrapper) ServletResponseWrapper(jakarta.servlet.ServletResponseWrapper) ServletRequestWrapper(jakarta.servlet.ServletRequestWrapper) Tomcat(org.apache.catalina.startup.Tomcat) Servlet(jakarta.servlet.Servlet) GenericServlet(jakarta.servlet.GenericServlet) HttpServlet(jakarta.servlet.http.HttpServlet) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 19 with Servlet

use of jakarta.servlet.Servlet in project spring-framework by spring-projects.

the class DispatcherServletTests method servletHandlerAdapter.

@Test
public void servletHandlerAdapter() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/servlet.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    complexDispatcherServlet.service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("body");
    Servlet myServlet = (Servlet) complexDispatcherServlet.getWebApplicationContext().getBean("myServlet");
    assertThat(myServlet.getServletConfig().getServletName()).isEqualTo("complex");
    assertThat(myServlet.getServletConfig().getServletContext()).isEqualTo(getServletContext());
    complexDispatcherServlet.destroy();
    assertThat((Object) myServlet.getServletConfig()).isNull();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Servlet(jakarta.servlet.Servlet) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 20 with Servlet

use of jakarta.servlet.Servlet in project spring-framework by spring-projects.

the class MockFilterChainTests method doFilterWithServletAndFilters.

@Test
void doFilterWithServletAndFilters() throws Exception {
    Servlet servlet = mock(Servlet.class);
    MockFilter filter2 = new MockFilter(servlet);
    MockFilter filter1 = new MockFilter(null);
    MockFilterChain chain = new MockFilterChain(servlet, filter1, filter2);
    chain.doFilter(this.request, this.response);
    assertThat(filter1.invoked).isTrue();
    assertThat(filter2.invoked).isTrue();
    verify(servlet).service(this.request, this.response);
    assertThatIllegalStateException().isThrownBy(() -> chain.doFilter(this.request, this.response)).withMessage("This FilterChain has already been called!");
}
Also used : Servlet(jakarta.servlet.Servlet) Test(org.junit.jupiter.api.Test)

Aggregations

Servlet (jakarta.servlet.Servlet)20 ServletException (jakarta.servlet.ServletException)6 HttpServlet (jakarta.servlet.http.HttpServlet)6 IOException (java.io.IOException)5 Context (org.apache.catalina.Context)5 UnavailableException (jakarta.servlet.UnavailableException)4 Tomcat (org.apache.catalina.startup.Tomcat)4 Test (org.junit.jupiter.api.Test)4 AsyncContext (jakarta.servlet.AsyncContext)3 GenericServlet (jakarta.servlet.GenericServlet)3 ServletRequestWrapper (jakarta.servlet.ServletRequestWrapper)3 ServletResponseWrapper (jakarta.servlet.ServletResponseWrapper)3 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)3 Wrapper (org.apache.catalina.Wrapper)3 TesterContext (org.apache.tomcat.unittest.TesterContext)3 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)3 ReflectorServletProcessor (org.atmosphere.handler.ReflectorServletProcessor)3 ServletRegistration (jakarta.servlet.ServletRegistration)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2 FileNotFoundException (java.io.FileNotFoundException)2