반응형
response
: 서버가 요청에 대한 답변을 클라이언트에게 보내는 것. (사용자 요청에 응답)
서버 : 응답 헤더 + 처리 결과 데이터 => 웹 브라우저로 전송
- 페이지 이동 관련 메소드
페이지 이동 = 리다이렉션(redirection)
페이지 이동 시에는 문자 인코딩에 알맞게 설정해야함
- forwarding과 redirect 의 차이
forwarding (JSP)
: 상세 페이지를 볼 때 주로 사용
redirect (URL)
: 값을 변경, 삭제, 등록 등 후에 이동할 때 사용
- 페이지 이동 관련 메소드 종류
페이지 이동 관련 메소드 | 반환 유형 | 설명 |
sendRedirect(String url) | void | 설정한 URL 페이지로 강제 이동 |
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Implicit Objects(내장 객체)</title>
</head>
<body>
<% // 스크립틀릿
// redirect : 새로운 URL로 이동
response.sendRedirect("http://www.google.com");
%>
</body>
</html>
response 객체 예시1
response01.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Implicit Object</title>
</head>
<body>
<!--
요청URI : response01_process.jsp
요청파라미터(쿼리스트링) : id=a001&passwd=java
요청방식 : post
-->
<form action="response01_process.jsp" method="post">
<!-- 폼데이터 -->
<p>아이디 : <input type="text" name="id" placeholder="아이디" required /></p>
<p>비밀번호 : <input type="password" name="passwd" placeholder="비밀번호" required /></p>
<p><input type="submit" value="전송" /></p>
</form>
</body>
</html>
response01_process.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<% // 스크립틀릿
/* 요청URI : response01_process.jsp
요청파라미터(쿼리스트링) : request{id=a001&passwd=java}
요청방식 : post
*/
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id"); // a001
String passwd = request.getParameter("passwd"); // java
out.print("<p>id : " + id + "</p>");
out.print("<p>passwd : " + passwd + "</p>");
%>
</body>
</html>
response01_process.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<% // 스크립틀릿
/* 요청URI : response01_process.jsp
요청파라미터(쿼리스트링) : request{id=a001&passwd=java}
요청방식 : post
*/
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id"); // a001
String passwd = request.getParameter("passwd"); // java
out.print("<p>id : " + id + "</p>");
out.print("<p>passwd : " + passwd + "</p>");
/*
비교
1) 숫자 == 숫자, 객체 == 객체
2) 문자열.equals(문자열)
*/
if(id.equals("a001")&&passwd.equals("java")) { // 관리자
// redirect : 새로운 URL
response.sendRedirect("response01_sucess.jsp");
} else { // 일반
// redirect : 새로운 URL 재요청
response.sendRedirect("response01_failed.jsp");
}
%>
</body>
</html>
response01_sucess.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h3>관리자 로그인 성공</h3>
</body>
</html>
response01_failed.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h3>관리자 로그인 실패!</h3>
<!-- redirect?(x)-자동처리 -> a link-수동처리 -->
<p><a href="response01.jsp">로그인가기</a></p>
</body>
</html>
- 5초마다 JSP 페이지 갱신
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ page import="java.util.Date"%>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h3>이 페이지는 5초마다 새로고침 됩니다.</h3>
<% // 스크립틀릿
response.setIntHeader("Refresh", 5); // 5초마다 새로고침
%>
<p><%=new Date()%>
<p><%=new Date().toLocaleString()%>
</body>
</html>
응답 HTTP 헤더 관련 메소드 ( response 객체 예시2)
응답 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 설정 |
setIntHeader(String name, int value) | void | 헤더 이름 name에 정수값 value 설정 |
containsHeader(String name) | boolean | 헤더 이름 name이 HTTP 헤더에 포함되었는지 여부 확인 포함됨 - true 포함되지 않음 - false |
getHeader(String name) | String | 헤더 이름 name 값을 가져옴 |
response0101.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<% // 스크립틀릿
response.setHeader("Cache-control", "use_chache");
response.setHeader("contentType", "text/html;charset=UTF-8");
response.setDateHeader("date", 1L);
%>
<p>Cache-control : <%=response.getHeader("Cache-control")%> </p>
<p>contentType : <%=response.getHeader("contentType")%></p>
<p>date : <%=response.getHeader("date")%></p>
</body>
</html>
response 객체 예시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 코드 설정 |
- sendError
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<% // 스크립틀릿
response.sendError(404, "요청 페이지를 찾을 수 없습니다");
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<% // 스크립틀릿
// HTTP STATUS CODE
response.sendError(200, "성공");
// response.sendError(404, "요청 페이지를 찾을 수 없습니다");
out.print("<p>"+(3+2) +"</p>"); // 위의 코드로 인해 가려짐
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<% // 스크립틀릿
// HTTP STATUS CODE
// response.sendError(200, "성공");
// response.sendError(404, "요청 페이지를 찾을 수 없습니다");
response.sendError(500, "개발자 오류");
out.print("<p>"+(3+2) +"</p>"); // 위의 코드로 인해 가려짐
%>
</body>
</html>
반응형
'스프링' 카테고리의 다른 글
[스프링] 10장 쇼핑몰 시스템2 (0) | 2024.04.02 |
---|---|
[스프링] 8.5장 과제 (0) | 2024.04.01 |
[스프링] 7장 내장 객체, request 객체 (0) | 2024.04.01 |
[스프링] 6.5장 과제 (0) | 2024.04.01 |
[스프링] 6장 쇼핑몰 시스템 (0) | 2024.03.29 |