JSP

response 기본 객체 02_리다이렉트

yayeun 2016. 6. 1. 15:29

리다이렉트를 이용해서 페이지 이동하기

▣ 리다이렉트 : 웹 서버가 웹 브라우저에게 다른 페이지로 이동하라고 지시하는 것을 의미

- 특정 페이지를 실행 한 후 지정한 페이지로 이동하길 원할 때 (리다이렉트로 지시한 JSP 페이지가 있을 경우 웹 브라우저는 실질적으로 요청을 두번 한것 )


- 리다이렉트 형태

 response.sendRedirect(String location)


< -- login.jsp : 리다이렉트 형태 -- >

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%
    String id = request.getParameter("memberId");
    if(id.equals("yayeun")){
        response.sendRedirect("index.jsp");
    }else{
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    잘못된 아이디 입니다.
</body>
</html>
<%}%>
cs


< -- login.jsp : 출력화면(memberId 의 파라미터 값이 tt 일 경우) -- >



< -- login.jsp : 출력화면(memberId 의 파라미터 값이 yayeun 일 경우) -- >



- 다른 서버로 이동하도록 지정할 수도 있다.

 response.sendRedirect("http://yayeun.tistory.com") ;


- 리다이렉트도 인코딩을 알맞게 수행해주어야 한다.

URLEncoder.encode() 메서드를 사용해 파라미터 값을 지정한 캐릭터 셋으로 이용해 인코딩 할 수 있다.

1
2
3
4
5
6
7
8
<%@page import="java.net.URLEncoder"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%
    String value = "자바";
    String encodedValue = URLEncoder.encode(value,"utf-8");
    response.sendRedirect("index.jsp?name="+encodedValue);
%>



cs