WEB/JSP
JSP 08 - response
inderrom
2022. 12. 28. 12:27

페이지 이동 = 리다이렉션(redirection)
- 페이지를 강제로 이동하는 것

response 연습
<response01.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>Implicit Objects</title>
</head>
<body>
<!--
response01_process.jsp?id=a001&passwd=java
-->
<form action="response01_process.jsp" method="post">
<!-- required : 필수 입력 -->
<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>
<%@ 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>
<%
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로 강제 재요청
// 아닌 경우 response01_failed.jsp로 강제 재요청
if(userId.equals("a001") && password.equals("java")){
response.sendRedirect("response01_success.jsp");
}else{
response.sendRedirect("response01_failed.jsp");
}
%>
</body>
</html>
- 성공일 경우 response01_success.jsp 로 이동
- 실패일 경우 response01_failed.jsp 로 이동
<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>Implicit Object</title>
</head>
<body>
로그인 성공 !
</body>
</html>
<response01_failed.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>Implicit Objects</title>
</head>
<body>
로그인 실패!
</body>
</html>
페이지 새로고침
response.setIntHeader("Refresh", 초);