Write it/JSP

JSP) 예외 처리

develop_mii 2025. 8. 7. 13:43

1. Try-Catch

<%
try {
  int a = 10;
  int b = 0;
 } catch (Exception e) {
  e.printStackTrace();
  out.println("에러내용 : " +e.getMessatge()); 
}
%>

 

 # 예제 화면 출력 시

* out.println(e.getMessage());   에러내용 화면에 보여준다

 


 

2. 에러 페이지설정 (예외처리)

 

①  폴더 만들때 설정

 Dynamic Web Project  생성 시 

 next  → next  하면 보이는   Generate web.xml deployment descriptor체크하고 생성

 

② 이미 만든 폴더에 추가설정

 이미 만든 Dynamic Web Project   오른쪽마우스 - Java EE Tools  
Generate deployment descriptor  stub  클릭하여 설정

 

 

① / ② 설정 시

  WEB-INF 폴더안에 web.xml 파일이 생성된다.

  web.xml 파일 열고 소스보기에서  <web-app> 태그 안 각 에러별 보여줄 화면 설정가능

 

# Error인 경우  만들어 놓은 jsp 파일 보여주는 예시

<web-app>
   // 500error시 보여줄 화면 설정
   <error-page>
        <error-code>500</error-code>
        <location>/error500page.jsp</location>
    </error-page>
    
    // 404error시 보여줄 화면 설정
    <error-page>
        <error-code>404</error-code>
        <location>/error404page.jsp</location>
    </error-page>
</web-app>

 

'Write it > JSP' 카테고리의 다른 글

JSP) EL 표기법과 , JSTL  (4) 2025.08.06