예외처리
- 예외페이지를 이용
<%@ page errorPage = “errorPage_error.jsp”%> : 오류가 났을때 이동
<%@ page isErrorPage=”true” %> : 오류를 담당하는 페이지 설정
[예제]
1. 이름과 나이를 입력 받는 html 코드
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="viewInfo.jsp">
이 름 :<input type="text" name="name" size="20" /><br>
나 이 :<input type="text" name="age" size="20" /><br>
<hr>
* P.S : 나이는 숫자만 입력해야 합니다.
<hr>
<input type="submit" value="전송" />
</form>
</body>
</html
2. 회원 정보 출력하는 jsp 코드
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page errorPage="error02.jsp" %>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%!
String s_name;
int age;
%>
<%
request.setCharacterEncoding("UTF-8");
s_name = request.getParameter("name");
age = Integer.parseInt(request.getParameter("age"));
%>
<h3>회원 정보 출력</h3>
당신의 이름은 <%= s_name %>입니다.<br>
당신의 나이는 <%= age %>살입니다.<br>
</body>
</html>
3. 예외처리 설정 코드
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page isErrorPage="true" %>
<%
response.setStatus(HttpServletResponse.SC_OK);
// 응답하는 상태가 정상적이다고 전달
%>
에러 발생
<br>
<%= exception.getMessage() %>
자바 빈(JavaBean)
- 정의 : 자바를 이용해 소프트웨어를 부품화 시킨 것, 한번 개발되면 계속 재사용 가능
- 만들기 : 클래스로 구성, 필드, 메소드가 있음
- 정의
package hello;
public class HelloBean {
private int age=20
};
- 프로퍼티 : 자바빈에 저장되어 있는 필드 값(접근 지정자를 통해 숨겨진 값) > 값을 설정 : setxxx메서드 / 값을 읽을 때 : getxxx메서드
- 액션 태그
- jsp:useBean : 빈(bean) 객체 생성
- page : 이 페이지 안에서 사용
- request : 요청을 받고 처리
- session : 전체 사이트 범위, 지속시간 동안 유지
- application : 전체 사이트 범위, 계속 유지
- jsp:useBean : 빈(bean) 객체 생성
<jsp:useBean class=”hello.HelloBean”(패키지.클래스) id=”myBean”(참조변수) scope=”page”(생략가능) /> hello.HelloBean myBean = new hello.HelloBean();
package hello;
public class HelloBean {
private String name = "홍길동";
private int age = 20;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
// [helloBean.jsp]
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<jsp:useBean class="hello.HelloBean" id="myBean" scope="page"></jsp:useBean>
-
- jsp:setProperty : 프로퍼티 값을 가져옴
<jsp:setProperty name=”myBean” property =”age” value=”10” />
<% myBean.setAge(10); %>
-
- jsp:getProperty : 프로퍼티 값을 읽음
<jsp:getProperty name=”myBean” property =”age” />
<% myBean.getAge(); %>
*. 빈 생성 후 저장된 값 출력하기<br><br> 이름 : <%= myBean.getName() %><br> 나이 : <%= myBean.getAge() %> <hr> *. 값을 변경한 후 출력하기<br><br> <% myBean.setName("홍길동"); myBean.setAge(18); %> 이름 : <%= myBean.getName() %><br> 나이 : <%= myBean.getAge() %> // #. 빈 생성 후 저장된 값 출력하기<br><br> 이름 : <jsp:getProperty property="name" name="myBean"/><br> 나이 : <jsp:getProperty property="age" name="myBean"/> <hr> #. 값을 변경한 후 출력하기<br><br> <jsp:setProperty property="name" name="myBean" value="홍길동"/> <jsp:setProperty property="age" name="myBean" value="18"/> 이름 : <jsp:getProperty property="name" name="myBean"/><br> 나이 : <jsp:getProperty property="age" name="myBean"/>
'java,jsp,spring > JSP' 카테고리의 다른 글
DBCP 기법 (0) | 2022.08.26 |
---|---|
JSP JDBC (0) | 2022.08.24 |
JSP 세션 (0) | 2022.08.17 |
JSP 쿠키 (0) | 2022.08.15 |
JSP 지시자, request/response 객체, 액션태그 (0) | 2022.08.15 |