本文整理汇总了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());
}
示例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");
}
示例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);
}
示例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());
}
示例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());
}
示例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);
}