JSP

[JSP]리다이렉트 방식

worri-pi 2021. 7. 16. 22:19

 

 

리다이렉트 방식은 response 객체에서 제공하는 sendRedirect() 를 사용해서 페이지를 이동하는 방법이다.

브라우저의 URL을 변경하도록한다.

request와 response는 저장되지 않는다.

주의해야할 점은 sendRedirect()는 페이지가 호출되는 즉시 바로 이동하는 것이 아니라 sendRedirect() 이후의 모든 코드를 실행한 후 페이지가 이동한다. 

아직 어려운 코드를 만나보지 못했지만 혹시 만나게된다면 if else 를 잘 써보자.

 

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<form action="domain.jsp">
		<h2>select domain<h2>
			<select name="domain">
				<option value="http://www.google.com">google</option>
				<option value="http://www.naver.com">naver</option>
				<option value="http://www.daum.net">daum</option>		
			</select>
		<input type="submit" value="confirm">
	</form>
</body>
</html>

도메인을 선택하는 select.jsp

 

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
	response.sendRedirect(request.getParameter("domain"));
%>
</body>
</html>

sendRedirect를 사용하여 해당 domain value 값으로 이동하기 위한 domain.jsp

728x90