상세 컨텐츠

본문 제목

[JSP] 예외 발생! -> 예외 처리

error note

by 미로910 2024. 7. 5. 17:13

본문

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>세션을 읽어 보는 페이지</title>
</head>
<body>
	<h2>세션 읽기</h2>
	<%
		// 삼항연산자
		// if else
		// try catch 사용 가능
		String username = (String) session.getAttribute("username");
		int age = (int) session.getAttribute("age");
		
				
				
		if(username != null){
			out.println("<p>안녕" + username + "</p>");
			out.println("나이는" + age + " 이네요! </p>");
		}else {
			out.println("세션 설정이 되지 않았습니다");
		}
		
		
		
	%>
</body>
</html>

❗❗ 예외  발생!!

예외가 발생하면

// 삼항 연산자

// if else

// try catch 사용

중에 사용해서 예외 처리를 해줘야 한다

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>세션을 읽어 보는 페이지</title>
</head>
<body>
	<h2>세션 읽기</h2>
	<%
		// 삼항연산자
		// if else
		// try catch 사용 가능
		try{
			
			String username = (String) session.getAttribute("username");
			int age = (int) session.getAttribute("age");
			
					
					
			if(username != null){
				out.println("<p>안녕" + username + "</p>");
				out.println("나이는" + age + " 이네요! </p>");
			}else {
				out.println("세션 설정이 되지 않았습니다");
			}
			
		} catch(Exception e) {
			response.sendRedirect("setSession.jsp");
		}
	
		
		
		
		
		
	%>
</body>
</html>

 

관련글 더보기