JSP

JSP 내장 객체 request,response (2탄)

9400 2022. 12. 28. 15:40

2. response(내장객체)

 

요청을 보낸 클라이언트에게 응답정보를 보내기 위한 객체

사용자의 요청을 처리한 결과를 서버에서 웹 브라우저로 전달하는 정보를 저장하고 서버는 응답헤더와 요청 처리결과 데이터를 웹 브라우저로 보냄 

JSP 컨테이너는 서버에서 웹 브라우저로 응답하는 정보를 처리하기 위해 javax.servlet.HttpSesrvletResponse 객체 타입의response내장 객체를 사용하여 사용자의 요청에 응답

 

페이지 이동관련 메소드

- 사용자가 새로운 페이지를 요청할때와 같이 페이지를 강제로 이동하는 것을 리다이렉션(Redirection)이라고함.

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

페이지 이동시에는 인코딩을 알맞게 설정해야한다. 

 

 

//페이지 이동방식

<jsp:forward page="이동할페이지" />

1. 포워드(foward) 방식 : 현재 jsp페이지에서 이동할 URL로 요청정보를 그대로 전달하므로 사용자가 최초로 요청한 정보가 이동된 URL에서도 유효함.

그러나 이동된 URL이 웹 브라우저의 주소 창에 나타나지 않고 처음 요청한 URL이 나타나기 때문에 이동여부를 사용자가 알 수 없음.

 

 

response.sendRedirect("이동할 페이지")

2. 리다이렉트(redirect)방식 : 처음 요청받은 현재 JSP 페이지로부터 이동할 URL을 웹 브라우저로 반환 이 대 웹 브라우저에서는 새로운 요청을 생성하여 이동할 URL에 다시 요청을 전송하므로 처음 보낸 요청 정보가 이동된 URL에서는 유효하지 않음. 즉 클라이언트가 새로운 페이지를 요청한 것과 같은 방식으로 페이지가 이동함. 따라서 이동된 URL이 웹 브라우저의 주소창에 보인다.

<!DOCTYPE html>
<html>
<head>
<title>Implicit Objects(내장객체)</title>
</head>
<body>
<% //스크립틀릿
	response.sendRedirect("http://www.google.com");

%>
</body>
</html>

 

 

response의 내장 객체로 3초 마다 새로고침하기 

<!DOCTYPE html>
<html>
<head>
<title>Implicit Object</title>
</head>
<body>
	<p>이 페이지는 3초마다 새로고침 됩니다.</p>
	<% //스크립틀릿
		response.setIntHeader("Refresh",3);
	%>
	<p><%=new Date().toLocaleString() %></p>
</body>
</html>

출력결과

 

 

사용예제)

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<title>Implicit Objects(내장객체)</title>
</head>
<body>
	<!-- 
	response01_process/jsp?id=a001&passwd=java
	 -->
	<form action="response01_process.jsp" method="post">
		<p>아이디 : <input type="text" name="id" required /></p>
		<p>비밀번호 : <input type="password" name="passwd" required /> </p>
		<p><input type="submit" value="전송" /></p>
	</form>
</body>
</html>

 

 

response01_process.jsp로 전달 

입력받은 id와 password를 request객체로 받아옴.

만약 id가 001이면서 password의값이 java인경우 -->

강제(sendRedirect)로 response01_secess.jsp로 보냄 

sendRedirect("response01_secess.jsp")

그게 아니면

sendRedirect("response01_fail.jsp") 로 보냄.

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%  //스크립틀릿
	//response01_process/jsp?id=a001&passwd=java
	request.setCharacterEncoding("UTF-8");
	String userid = request.getParameter("id");
	String password = request.getParameter("passwd");
	
/* 	out.print("<p>userid : "+userid +"</p>");
	out.print("<p>password : "+password +"</p>"); */
	
	
	//만약 userid값이 a001이면서 동시에 password의 값이 java인 경우
	//response01_success.jsp로 강제 재요청
	if(userid.equals("a001")&&password.equals("java")){
		response.sendRedirect("response01_success.jsp");
	}else{ //그렇지 않으면 response01_failed.jsp로 강제 재요청
		response.sendRedirect("response01_failed.jsp");
	}
	
%>

</body>
</html>

 

성공시

<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
	로그인 성공!ㅡㅅㅡ
</body>
</html>

 

실패시

<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
	로그인 실패!
</body>
</html>