當前位置: 首頁>>代碼示例>>Java>>正文


Java Context.addErrorPage方法代碼示例

本文整理匯總了Java中org.apache.catalina.Context.addErrorPage方法的典型用法代碼示例。如果您正苦於以下問題:Java Context.addErrorPage方法的具體用法?Java Context.addErrorPage怎麽用?Java Context.addErrorPage使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.catalina.Context的用法示例。


在下文中一共展示了Context.addErrorPage方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testTimeoutDispatchCustomErrorPage

import org.apache.catalina.Context; //導入方法依賴的package包/類
@Test
public void testTimeoutDispatchCustomErrorPage() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Context context = tomcat.addContext("", null);
    tomcat.addServlet("", "timeout", Bug58751AsyncServlet.class.getName())
            .setAsyncSupported(true);
    CustomErrorServlet customErrorServlet = new CustomErrorServlet();
    Tomcat.addServlet(context, "customErrorServlet", customErrorServlet);
    context.addServletMapping("/timeout", "timeout");
    context.addServletMapping("/error", "customErrorServlet");
    ErrorPage errorPage = new ErrorPage();
    errorPage.setLocation("/error");
    context.addErrorPage(errorPage);
    tomcat.start();

    ByteChunk responseBody = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/timeout", responseBody, null);

    Assert.assertEquals(503, rc);
    Assert.assertEquals(CustomErrorServlet.ERROR_MESSAGE, responseBody.toString());
}
 
開發者ID:liaokailin,項目名稱:tomcat7,代碼行數:22,代碼來源:TestAsyncContextImpl.java

示例2: testErrorPageHandling

import org.apache.catalina.Context; //導入方法依賴的package包/類
@Test
public void testErrorPageHandling() throws Exception {
    // Set up a container
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    // Add the error page
    Tomcat.addServlet(ctx, "error", new ErrorServlet());
    ctx.addServletMapping("/error", "error");

    // Add the error handling page
    Tomcat.addServlet(ctx, "report", new ReportServlet());
    ctx.addServletMapping("/report/*", "report");

    // And the handling for 500 responses
    ErrorPage errorPage500 = new ErrorPage();
    errorPage500.setErrorCode(Response.SC_INTERNAL_SERVER_ERROR);
    errorPage500.setLocation("/report/500");
    ctx.addErrorPage(errorPage500);

    // And the default error handling
    ErrorPage errorPageDefault = new ErrorPage();
    errorPageDefault.setLocation("/report/default");
    ctx.addErrorPage(errorPageDefault);

    tomcat.start();

    doTestErrorPageHandling(500, "/500");
    doTestErrorPageHandling(501, "/default");
}
 
開發者ID:liaokailin,項目名稱:tomcat7,代碼行數:33,代碼來源:TestStandardHostValve.java

示例3: testInvalidErrorPage

import org.apache.catalina.Context; //導入方法依賴的package包/類
@Test(expected=IllegalArgumentException.class)
public void testInvalidErrorPage() throws Exception {
    // Set up a container
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    // Add a broken error page configuration
    ErrorPage errorPage500 = new ErrorPage();
    errorPage500.setErrorCode("java.lang.Exception");
    errorPage500.setLocation("/report/500");
    ctx.addErrorPage(errorPage500);
}
 
開發者ID:liaokailin,項目名稱:tomcat7,代碼行數:15,代碼來源:TestStandardHostValve.java

示例4: testBug51653a

import org.apache.catalina.Context; //導入方法依賴的package包/類
@Test
public void testBug51653a() throws Exception {
    // Set up a container
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    // Traces order of events across multiple components
    StringBuilder trace = new StringBuilder();

    //Add the error page
    Tomcat.addServlet(ctx, "errorPage", new Bug51653ErrorPage(trace));
    ctx.addServletMapping("/error", "errorPage");
    // And the handling for 404 responses
    ErrorPage errorPage = new ErrorPage();
    errorPage.setErrorCode(Response.SC_NOT_FOUND);
    errorPage.setLocation("/error");
    ctx.addErrorPage(errorPage);

    // Add the request listener
    Bug51653RequestListener reqListener =
        new Bug51653RequestListener(trace);
    ((StandardContext) ctx).addApplicationEventListener(reqListener);

    tomcat.start();

    // Request a page that does not exist
    int rc = getUrl("http://localhost:" + getPort() + "/invalid",
            new ByteChunk(), null);

    // Need to allow time (but not too long in case the test fails) for
    // ServletRequestListener to complete
    int i = 20;
    while (i > 0) {
        if (trace.toString().endsWith("Destroy")) {
            break;
        }
        Thread.sleep(250);
        i--;
    }

    assertEquals(Response.SC_NOT_FOUND, rc);
    assertEquals("InitErrorDestroy", trace.toString());
}
 
開發者ID:liaokailin,項目名稱:tomcat7,代碼行數:46,代碼來源:TestStandardContextValve.java

示例5: testBug51653b

import org.apache.catalina.Context; //導入方法依賴的package包/類
@Test
public void testBug51653b() throws Exception {
    // Set up a container
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    // Traces order of events across multiple components
    StringBuilder trace = new StringBuilder();

    // Add the page that generates the error
    Tomcat.addServlet(ctx, "test", new Bug51653ErrorTrigger());
    ctx.addServletMapping("/test", "test");

    // Add the error page
    Tomcat.addServlet(ctx, "errorPage", new Bug51653ErrorPage(trace));
    ctx.addServletMapping("/error", "errorPage");
    // And the handling for 404 responses
    ErrorPage errorPage = new ErrorPage();
    errorPage.setErrorCode(Response.SC_NOT_FOUND);
    errorPage.setLocation("/error");
    ctx.addErrorPage(errorPage);

    // Add the request listener
    Bug51653RequestListener reqListener =
        new Bug51653RequestListener(trace);
    ((StandardContext) ctx).addApplicationEventListener(reqListener);

    tomcat.start();

    // Request a page that does not exist
    int rc = getUrl("http://localhost:" + getPort() + "/test",
            new ByteChunk(), null);

    // Need to allow time (but not too long in case the test fails) for
    // ServletRequestListener to complete
    int i = 20;
    while (i > 0) {
        if (trace.toString().endsWith("Destroy")) {
            break;
        }
        Thread.sleep(250);
        i--;
    }

    assertEquals(Response.SC_NOT_FOUND, rc);
    assertEquals("InitErrorDestroy", trace.toString());
}
 
開發者ID:liaokailin,項目名稱:tomcat7,代碼行數:50,代碼來源:TestStandardContextValve.java

示例6: doTestBug51197

import org.apache.catalina.Context; //導入方法依賴的package包/類
private void doTestBug51197(boolean threaded, boolean customError) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    AsyncErrorServlet asyncErrorServlet =
        new AsyncErrorServlet(HttpServletResponse.SC_BAD_REQUEST, threaded);
    Wrapper wrapper =
        Tomcat.addServlet(ctx, "asyncErrorServlet", asyncErrorServlet);
    wrapper.setAsyncSupported(true);
    ctx.addServletMapping("/asyncErrorServlet", "asyncErrorServlet");

    if (customError) {
        CustomErrorServlet customErrorServlet = new CustomErrorServlet();
        Tomcat.addServlet(ctx, "customErrorServlet", customErrorServlet);
        ctx.addServletMapping("/customErrorServlet", "customErrorServlet");

        ErrorPage ep = new ErrorPage();
        ep.setLocation("/customErrorServlet");

        ctx.addErrorPage(ep);
    }

    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);

    tomcat.start();

    StringBuilder url = new StringBuilder(48);
    url.append("http://localhost:");
    url.append(getPort());
    url.append("/asyncErrorServlet");

    ByteChunk res = new ByteChunk();
    int rc = getUrl(url.toString(), res, null);

    assertEquals(HttpServletResponse.SC_BAD_REQUEST, rc);

    // SRV 10.9.2 - Handling an error is entirely the application's
    // responsibility when an error occurs on an application thread.
    // Calling sendError() followed by complete() and expecting the standard
    // error page mechanism to kick in could be viewed as handling the error
    String responseBody = res.toString();
    Assert.assertNotNull(responseBody);
    assertTrue(responseBody.length() > 0);
    if (customError) {
        assertTrue(responseBody, responseBody.contains(CustomErrorServlet.ERROR_MESSAGE));
    } else {
        assertTrue(responseBody, responseBody.contains(AsyncErrorServlet.ERROR_MESSAGE));
    }

    // Without this test may complete before access log has a chance to log
    // the request
    Thread.sleep(REQUEST_TIME);

    // Check the access log
    alv.validateAccessLog(1, HttpServletResponse.SC_BAD_REQUEST, 0,
            REQUEST_TIME);
}
 
開發者ID:liaokailin,項目名稱:tomcat7,代碼行數:62,代碼來源:TestAsyncContextImpl.java


注:本文中的org.apache.catalina.Context.addErrorPage方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。