본문 바로가기

BackEnd/JSP

[JSP 웹 프로그래밍] 내장 객체 2 <javax> (response)

728x90
반응형

1. response 내장 객체

   ▷ 사용자의 요청을 처리한 결과를 서버에서 웹 브라우저로 전달하는 정보를 저장하고

       서버는 응답 헤더와 요청 처리 결과 데이터를 웹 브라우저로 보냄

   ▷ JSP 컨테이너는 서버에서 웹 브라우저로 응답하는 정보를 처리하기 위해

        javax.servlet.http.HttpServletResponse 객체 타입의 response 내장 객체를 사용하여 사용자의 요청에 응답

 

◎ 페이지 이동 관련 메소드

   ▷ 페이지 이동 = 리다이렉션(redirection)

      ▶ 사용자가 새로운 페이지를 요청할 때와 같이 페이지를 강제로 이동하는 것

   ▷ 서버는 웹 브라우저에 다른 페이지로 강제 이동하도록 response 내장 객체의 리다이렉션 메소드를 제공

   ▷ 페이지 이동 시에는 문자 인코딩을 알맞게 설정해야 함

 

◎ 페이지 이동 관련 메소드 종류

 

◎ response 내장 객체로 페이지 이동하기 예제

1. response01.jsp
<%@ 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>​

 



2. response01_process.jsp
<%@ 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>
	<%
		request.setCharacterEncoding("utf-8");
		String userid = request.getParameter("id");
		String password = request.getParameter("passwd");
		
		if(userid.equals("admin") && password.equals("1234")){
			// 성공시
			response.sendRedirect("response01_success.jsp");
		} else{
			response.sendRedirect("response01_failed.jsp");			
		}
	%>
</body>
</html>​


3. response01_success.jsp

<%@ 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>

 



4. response01_failed.jsp

<%@ 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>
	<p> 로그인을 실패했습니다.
	<p> <a href="./response01.jsp"> 로그인 가기</a>
</body>
</html>

 

 

◎ 응답 HTTP 헤더 관련 메소드

   ▷ 응답 HTTP 헤더 관련 메소드는 서버가 웹 브라우저에 응답하는 정보에 헤더를 추가하는 기능을 제공

   ▷ 헤더 정보에는 주로 서버에 대한 정보가 저장되어 있음

  

◎ 응답 HTTP 헤더 관련 메소드 종류

 

◎ response 내장 객체로 5초마다 JSP 페이지 갱신하기 예제

<%@ 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>
	<p> 이 페이지는 5초마다 새로고침 됩니다.
		<%
			response.setIntHeader("Refresh", 5);
		%>
	<p> <%=(new java.util.Date()) %>>
</body>
</html>

<!-- 출력 : 
이 페이지는 5초마다 새로고침 됩니다.
Tue Feb 28 15:45:56 KST 2023>
 -->

 

◎ response 내장 객체로 오류 응답 코드와 오류 메시지 보내기 예제

<%@ 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(404, "요청 페이지를 찾을 수 없습니다.");
	%>
</body>
</html>

<!-- 출력 : 
HTTP 상태 404 – 찾을 수 없음
타입 상태 보고
메시지 요청 페이지를 찾을 수 없습니다.
설명 Origin 서버가 대상 리소스를 위한 현재의 representation을 찾지 못했거나,
그것이 존재하는지를 밝히려 하지 않습니다.
 -->

 

내장 객체는 스크립틀릿 태그나 표현문 태그에 선언을 하거나 객체를 생성하지 않고도 직접 호출하여 사용 가능합니다.

 

request는 값들을 입력받아 출력하는 것이고 respose는 해당 조건에 맞으면 메소드를 이용해 해당 페이지로 이동하는 것이라고 보면 되겠네요!

 

많은 분들의 피드백은 언제나 환영합니다!  많은 댓글 부탁드려요~~

 

 

728x90
반응형