페이지 리디렉션을 하는 가장 간단한 방법은 response 객체의 sendRedirect() 메소드를 이용하는 것이랍니다.
public void response.sendRedirect(String location) throws IOException
이 메소드는 상태코드와 새로운 페이지 주소를 함께 response객체에 담아서 브라우저로 전송합니다. setStatus() 와 setHeader() 를 이용하여 세팅을 해주면 됩니다. 아래처럼 말이죠.
.... String site = "http://www.newpage.com" ; response.setStatus(response.SC_MOVED_TEMPORARILY); response.setHeader("Location", site); ....
JSP 로 어떻게 페이지 리디렉션을 하는지 예제를 통해 알아볼까요?
<%@ page import="java.io.*,java.util.*" %> <html> <head> <title>Page Redirection</title> </head> <body> <center> <h1>Page Redirection</h1> </center> <% // New location to be redirected String site = new String("http://www.naver.com"); response.setStatus(response.SC_MOVED_TEMPORARILY); response.setHeader("Location", site); %> </body> </html>
위 코드를 PageRedirect.jsp 에 넣고 http://localhost:8080/PageRedirect.jsp 를 웹브라우저에서 요청해보세요.
http://www.naver.com 사이트로 리디렉션이 되나요?? ^____^
Reference : http://www.tutorialspoint.com/jsp/jsp_page_redirect.htm
[JSP] Database CRUD (데이터베이스 연동) (0) | 2019.02.15 |
---|---|
[JSP] JSTL ( JSP Standard Tag Library ) (0) | 2019.02.15 |
[JSP] File Upload ( 파일 업로드 ) (0) | 2019.02.15 |
[JSP] Session ( 세션 ) (0) | 2019.02.15 |
[JSP] Cookies ( 쿠키 세팅, 읽기, 삭제하기) (0) | 2019.02.15 |