정리노트
[JSP] 내장 객체, 폼 태그
망고고래
2023. 12. 6. 17:18
5장. 내장 객체
3. response 내장 객체의 기능과 사용법
response 내장 객체: 요청 처리 결과를 서버에서 웹 브라우저에서 전달하는 정보(응답 헤더, 요청 처리 결과 데이터) 저장
JSP 컨테이너(톰캣)는 서버(아파치)에서 웹 브라우저로 응답하는 정보를 처리하기 위해 javax.servlet.http.HttpServletResponse 객체 타입의 response 내장 객체를 사용해서 사용자 요청에 응답
3.1 페이지 이동 관련 메서드
리다이렉션(redirection): 페이지를 강제로 이동(사용자가 새로운 페이지를 요청하는 경우 등)
문자 이코딩 필요
포워드(forward)vs리다이렉트(redirect)
포워드
액션 태그. 이동할 URL로 요청 정보를 그대로 전달→최초로 요청한 정보가 이동된 URL에서도 유효
이동된 URL이 주소 창에 나타나지 않아서 이동 여부를 사용자가 알 수 없음
<jsp:forward page="이동할 페이지"/>
리다이렉트
웹 브라우저에서 새로운 요청을 생해서 이동할 URL에 요청 다시 전송→최초 요청 정보가 이동된 URL에서는 무효
클라이언트가 페이지를 새로 요청한 것과 같은 방식으로 페이지 이동. 이동된 URL이 주소 창에 나타남
response.sendRedirect("이동할 페이지")
페이지 이동 관련 메서드
sendRedirect(String url)
반환 유형: void
설정한 URL 페이지로 강제 이동
※sendRedirect는 오류 방지를 위해 코드 마지막에 위치해야 한다.
//response01
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Implicit Objects</title>
</head>
<body>
<form action="response01_process.jsp" method = "post">
<p>아이디: <input type="text" name="id">
<p>비밀번호: <input type="text" name="passwd">
<p><input type="submit" value = "전송">
</form>
</body>
</html>
//response01_process
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String userid = request.getParameter("id");
String password = request.getParameter("passwd");
if(userid.equals("관리자")&&password.equals("1234")){
response.sendRedirect("response01_success.jsp");
}else{
response.sendRedirect("response01_failed.jsp");
}
%>
</body>
</html>
//response01_success
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Implicit Objects</title>
</head>
<body>
로그인 성공
</body>
</html>
//response01_failed
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>로그인 실패
<p><a href="./response01.jsp">로그인 가기</a>
</body>
</html>
3.2 응답 HTTP 헤더 관련 메서드
서버가 웹 브라우저에 응답하는 정보에 헤더를 추가하는 기능 제공
응답 HTTP 헤더 관련 메서드 | 반환 유형 | 설명 |
addCookie(Cookie cookie) | void | 쿠키 추가 |
addDateHeader(String name, long date) | void | 설정한 헤더 이름 name에 날짜/시간 추가 |
addHeader(String name, String value) | void | 설정한 헤더 이름 name에 value 추가 |
addIntHeader(String name, int value) | void | 설정한 헤더 이름 name에 정수 값 value 추가 |
setDateHeader(String name, long date) | void | 설정한 헤더 이름 name에 날짜/시간 설정 |
setHeader(String name, String value) | void | 설정한 헤더 이름 name에 문자열 값 value 설정 |
setHeader(String name, int value) | void | 설정한 헤더 이름 name에 정수 값 value 설정 |
containsHeader(String name) | boolean | 설정한 헤더 이름 name이 HTTP 헤더에 포함되었는지 여부 확인 |
getHeader(String name) | String | 설정한 헤더 이름 name값 가져옴 |
<%
response.setIntHeader("Refresh",5);
<%--5초마다 페이지 새로고침--%>
%>
3.3 응답 콘텐츠 관련 메서드
응답 콘텐츠 관련 메서드 | 반환 유형 | 설명 |
setContentType(String type) | void | 웹브라우저에 응답할 MIME 유형 설정 |
getContentType() | String | 웹 브라우저에 응답할 MIME 유형 취득 |
setCharacterEncoding(String charset) | void | 웹 브라우저에 응답할 문자 인코딩 설정 |
getCharacterEncoding() | String | 웹 브라우저에 응답할 문자 인코딩 취득 |
sendError(int status_code, String message) | void | 웹 브라우저에 응답할 오류 설정 |
setStatus(int statuscode) | void | 웹 브라우저에 응답할 HTTP 코드 설정 |
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Implicit Objects</title>
</head>
<body>
<%
response.sendError(500, "메롱");
%>
</body>
</html>

4. 아웃 내장 객체
6장. 폼 태그
form 태그의 기능과 사용법
속성 | 설명 |
action | 폼 데이터를 받아 처리하는 웹 페이지의 URL |
method(post, get) | 폼 데이터가 전송되는 HTTP 방식 설정 |
name | 폼을 식별하기 위한 이름 설정 |
target | 폼 처리 결과의 응답을 실행할 프레임 설정 |
enctype | 폼을 전송하는 콘텐츠 MIME 유형 설정 |
accept-charset | 폼 전송에 사용할 문자 인코딩 설정 |
※GET 방식과 POST 방식의 차이
GET: URL 끝에 데이터를 붙여서 보냄. 데이터가 외부에 노출→보안에 취약
POST: 개인정보 보호됨
form 데이터 처리하기
1. 요청 파라미터의 값 받기
체크 박스를 선택한 경우 on, 선택하지 않은 경우 null 전송
2. 요청 파라미터의 전체 값 받기