JSP
221130 JSP response redirect
주영재
2022. 11. 30. 12:43
response객체
응답할때 사용되는 객체
필요한 정보를 response객체에 담아 클라이언트에 보내줌
★★★sendRedirect(URL)
리디렉션이라고도 함
지정한 URL로 이동합니다.
원하는 페이지로 강제로 이동(응답)이 가능
res_ex01
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="res_ex01_result.jsp" method="post">
이름:<input type="text" name="name"><br>
나이:<input type="text" name="age"><br>
<input type="submit" value="확인"><br>
</form>
</body>
</html>
res_ex01_result
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");//한글처리
String name = request.getParameter("name");
String age = request.getParameter("age");
//age에 따라서 다른 결과 페이지로 이동
int result = Integer.parseInt(age);
if(result>=20){
response.sendRedirect("res_ex01_ok.jsp");//강제 페이지 이동(응답)
}else{
response.sendRedirect("res_ex01_no.jsp");
}
%>
response태그가 적힌곳으로 이동한 후, 그 다음에 sendRedirect의 url페이지로 이동하는 것
연산 ro 처리 or 데이터베이스 받고 이동
그래서 response태그가 적힌 곳에는
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
가 필요없을수도 있다.
==>때문에 여기는 servlet으로도 가능하다!
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
ok페이지
</body>
</html>
res_ex01_no
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
no페이지(20세 미만)
</body>
</html>