response 내장 객체(forward/Redirect)

조수경·2022년 1월 7일
0

JSP

목록 보기
17/45

response.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>response 내장 객체</title>
</head>
<body>
 <%
   //페이지를 이동하고 싶을때..
   //설정한 URL 페이지로 강제 이동
   response.sendRedirect("request03.jsp");
 %>
</body>
</html>

response01.jsp

<form method="post" action="response01_process.jsp">
  <p>아이디: <input type="text" name="id"></p>
  <p>비밀번호: <input type="text" name="pass"></p>
  <p><input type="submit" value="전송"></p>
</form>

포워드 방식과 리다이렉트 방식의 차이는?

1) 포워드 방식 : 최초 요청 정보가 이동된 URL에서도 유효

  • 왜? 이동할 URL로 요청 정보를 그대로 전달하는 것(인수인계)

2) 리다이렉트 방식 : 최초 요청 정보가 이동된 URL에서 유효하지 않음(사라짐)

  • 왜? 새로운 요청을 생성
    응답하기전에 새로운 요청을 보냄 그래서 새로운 요청정보를 출력하는것

response01_process.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>response 내장 객체</title>
</head>
<body>

<%
  request.setCharacterEncoding("UTF-8");
  String userId = request.getParameter("id");
  String passWord = request.getParameter("pass");
  
  if(userId.equals("admin")&&passWord.equals("1234")){
	  response.sendRedirect("response01_success.jsp");//보내는곳
  }else{//관리자 로그인 실패 시..
%>	  
	 <%--  <jsp:forward page="response01_failed.jsp" />
	  <!-- 포워드로하면 페일드가 아니라 프로세스로 뜸!!! --> --%>
<%	  
	  response.sendRedirect("response01_failed.jsp"); 
  }
%>
</body>
</html>

response01_success.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>response 내장 객체</title>
</head>
<body>
  로그인 성공!
</body>
</html>

forward로 했을때

Redirect로 했을때

<%@ 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>response 내장 객체</title>
</head>
<body>
   <p>로그인 실패</p>
   <p><a href="response01.jsp">로그인 가기</a></p>
</body>
</html>

response02.jsp - 5초마다 새로고침되는 페이지

<%@ page import="java.util.Date"%>
<%@ 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>response 내장 객체</title>
</head>
<body>
  <p>이 페이지는 5초마다 새로고침 됩니다.</p>
  <%
      response.setIntHeader("Refresh", 5);
      //헤더 이름 name에 value를 추가함
      //response.addHeader("ContentType", "text/html;charset=UTF-8");
      //설정한 헤더 이름 name에 날짜/시간을 설정(1L : long date(초))
      //response.setDateHeader("date", 1L);
      response.setCharacterEncoding("UTF-8");
      response.setContentType("text/html;charset=UTF-8");
  %>
  <p><%=new Date()%></p>
  <%-- contentType : <%=response.getHeader("ContentType")%><br />
  date : <%=response.getHeader("date")%> --%>
</body>
</html>
profile
신입 개발자 입니다!!!

0개의 댓글