본문 바로가기
IT/JSP

페이지 이동하는 두 가지 방법 (redirect, dispatcher)

by dya0 2019. 5. 13.

- Redirect 

ResponseTest1.jsp 폴더를 만든다

1
2
3
4
5
6
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
response.sendRedirect("responseTest2.jsp");
%>
 

그리고 responseTest2.jsp 파일을 생성한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@ 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>
<h1>형재 페이지는 responseTest1.jsp에 의해 <br>
리다이렉트 된 페이지입니다. (요청시 URL과 현재의 URL을 확인해보세요)
</h1>
</body>
</html>

responseTest1.jsp 파일을 실행하면 

1을 실행해도 resposeTest2.jsp 파일이 실행되고 url이 Test2.jsp로 나온다.

 

- Dispatcher

responseTest3.jsp를 생성한다

1
2
3
4
5
6
7
8
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%--forward() : 인자값으로 지정도니 페이지로 이동--%>
<h2>여기는 1번 페이지 시작 : 저는 출력되지 않아요</h2>
<%
pageContext.forward("responseTest4.jsp");
%>
<h2>여기는 1번 페이지 끝 : 여기로는 오지도 않아요</h2>

responseTest4.jsp 를 생성한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title> pageContextText4.jsp</title>
</head>
<body>
 
<h3>여기는 responseTest4.jsp입니다.</h3>
</body>
</html>
<%----%>
Colored by Color Scripter

dispatcher 방식의 경우 내용만 바뀌고 파일 경로는 그대로이다. 

따라서 ResponseTest3에 작성한 h2 태그안의 내용은 반영되지 않는다. 

유의해서 사용하자.

'IT > JSP' 카테고리의 다른 글

web.xml 설정  (0) 2019.05.13
application  (0) 2019.05.13
xml 오류  (0) 2019.05.12
이클립스에서 톰캣 사용하기2  (0) 2019.05.08
이클립스에 톰캣을 서버로 등록하기  (0) 2019.05.08