Today's Study/Academy
33) 오류 처리
Hello-Melo
2022. 5. 17. 09:00
22.5.16
1. web.xml에 처리
- WEB-INF 아래에 있는 web.xml 페이지에 error-page 태그로 오류페이지 호출
- error-code에는 주요 코드 종류 입력(ex) 404 500 503 등등)
- exception-type에는 자바 예외유형의 정규화 클래스 설정에 사용(ex)NullpointException, NumberformatException 등)
- location은 오류 페이지의 URL 설정
실제 태그
<error-page>
<error-code>404</error-code>
<location>/error/exceptionNoPage.jsp</location>
</error-page>
이런식으로 web.xml에 태그를 주면 예외처리가 가능함.
2. try-catch 문 사용
- 자바에서 사용하던 것 처럼, 스크립틀릿(<% %>)태그 안에 try-catch문을 줘서 예외 혹은 에러를 처리할 수 있음
<%
try {
conn = DBconnection.getconnConnection();
String sql = "select * from member_tbl where id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
member = new Member(
rs.getInt("id"),
rs.getString("userName"),
rs.getString("email"),
rs.getString("password"));
}
} catch (SQLException e) {
}
%>
이렇게 스크립틀릿 태그 안에 try-catch문을 사용
---
오늘은 여기까지!