redirect
: 데이터를 안 가지고 이동
ex. 특정 페이지로 이동
브라우저가 다른 페이지를 호출하는 형식
기본적으로 데이터 공유를 하지 못한다 ; get방식을 활용하여 데이터 전송은 가능하다
다른 페이지로 이동시에 URL이 변경된다
페이지를 새로 불러오기 때문에 request와 response객체가 새로 만들어진다
내장객체를 이용하는 방법
<a> 태그를 이용하는 방식
response객체의 sendRedirect 메소드를 이용하는 방식
자바스크립트의 내장 객체를 이용하는 방식
document.location.href = '이동할 페이지'
location.href = '이동할 페이지'
forward
: 서로 연관되어 있는 페이지끼리 이동
데이터를 가지고 이동
ex. login 데이터를 가지고 접속 페이지로 이동
서버 내에서 다른 페이지로 전환하는 형식
데이터 공유가 가능하다
다른 페이지로 이동시에 URL이 변경되지 않는다
request와 response 객체의 제어권을 다른 페이지로 넘겨주는 방식
내장객체를 이용하는 방법
pageContent객체 안에 forward() 메소드를 이용하는 방식
jsp액션태그를 이용하는 방식 ; 거의 사용하지 않는다
자바코드를 태그 형식으로 정의하여 사용하는 태그
자바클래스를 이용하는 방식 ; MVC2에서 이용된다
RequestDispatcher 객체를 사용하는 방식
서버 내에서 다른 페이지를 소환하는 방식
Exercise 1
<!-- start.jsp --!>
<%@ 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>
<div align="center">
<input type="button" value="Redirect" onclick="document.location.href='/JSP/move/redirect.jsp'">
<button onclick="document.location.href='/JSP/move/forward.jsp'">Forward</button>
</div>
</body>
</html>
<!-- redirect.jsp --!>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setAttribute("name", "장모씨");
// response.sendRedirect("/JSP/move/end.jsp");
// response.sendRedirect("/JSP/move/end.jsp?name=김땡땡");
%>
<a href="/JSP/move/end.jsp">넘어간다</a>
<!-- end.jsp --!>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String name = (String)request.getAttribute("name");
// String name = request.getParameter("name");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
이름 : <%=name %>
</div>
</body>
</html>
<!-- forward.jsp --!>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setAttribute("name", "장땡땡");
pageContext.forward("/move/end.jsp");
%>
'Backend > JSP' 카테고리의 다른 글
JSP 웹의 영역과 속성 (0) | 2023.04.25 |
---|---|
JSP jdbc 데이터베이스 접속 방식 Connection Pool (0) | 2023.04.13 |
JSP Eclipse 파라미터 값의 데이터 전송 방식 : get, post (0) | 2023.04.04 |
JSP Eclipse 내장 객체 (0) | 2023.04.04 |
JSP Eclipse 구구단 테이블 예시 풀이 (0) | 2023.04.03 |