Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

[예외] Servlet 예외처리 - 오류화면 등록 본문

인프런/[인프런] 스프링 MVC 2

[예외] Servlet 예외처리 - 오류화면 등록

Seung__ 2021. 9. 27. 21:36

Servlet에서 제공하는 기본 예외 처리 화면은 너무 투박하다 ㅋㅋㅋ

 

  • 서블릿은 Exception (예외)가 발생해서 서블릿 밖으로 전달되거나 
  • 또는 response.sendError() 가 호출 되었을 때 
  • 각각의 상황에 맞춘 오류 처리 기능을 제공한다.
  • 오류 화면을 준비해두면 이 기능을 사용했을 때 좀더 깔끔한 오류화면 가능.

 

 

1. Servlet 오류 페이지 등록


  • ErrorPage(status or Exception, path) : 에러,예외 터졌을 때 어디로?
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.http.HttpStatus;

@Component
public class WebServerCustomizer implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {

    @Override
    public void customize(ConfigurableWebServerFactory factory) {
        ErrorPage errorPage404 = new ErrorPage(HttpStatus.NOT_FOUND, "/error-page/404");
        ErrorPage errorPage500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error-page/500");
        ErrorPage errorPageEx = new ErrorPage(RuntimeException.class, "/error-page/500");

        factory.addErrorPages(errorPage404, errorPage500, errorPageEx);
    }
}
  • 오류 페이지는 예외를 다룰 때 해당 예외와 그 자식 타입의 오류를 함께 처리한다. 
  • 예를 들어서 위의 경우 RuntimeException 은 물론이고 RuntimeException 의 자식도 함께 처리한다.
  • 오류가 발생했을 때 처리할 수 있는 컨트롤러가 필요하다.
  • 예를 들어서 RuntimeException 예외가 발생하면 errorPageEx 에서 지정한 /error-page/500 이 호출된다.

 

 

2. GitHub : 210927 Servlet ErrorPage


 

bsh6463/Exception

Contribute to bsh6463/Exception development by creating an account on GitHub.

github.com

 

Comments