반응형
<%@ 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>
<H2>forward , sendRedirect 테스트</H2>
<HR>
<form method = post action = "forward_action2.jsp">
	forward action : <input type = text name = username>
	<input type = submit value = "확인"> <!-- URL이 forward_action을 가르킨다 -->
</form>

<form method = post action = "response_sendRedirect.jsp">
	response.sendRedirect : <input type = text name = username>
	<input type = submit value = "확인"> <!-- URL이 control_end를 가르킨다 -->
</form>
</body>
</html>

에는 맨 처음 페이지 화면입니다.

 

 

 

 

<% response.sendRedirect("page_control_end.jsp"); %>

<%//	request.setCharacterEncoding("utf-8"); // post방식을 사용할때 utf-8 캐릭터셋 맞춰주기 + 한글 쓸 경우 사용 %>
<%@ 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>
<H2>forward action 및 sendRedirect() 결과</H2>
<HR>
이름 : <%= request.getParameter("username") %> <BR> 
<%-- null값 왜냐하면 control에 요청 Redirect에서 control_end를 요청해라 지시 다시 
클라이언트가 control_end 요청 즉 username은 비어있음 --%>
<%-- forward로 한 경우 control username값하고 forword에 tel도 같이 넘어옴 --%>
전화번호 : <%= request.getParameter("tel") %>	
</body>
</html>

sendRedirect에 대해서 알아보도록 하겠습니다.

sendRedirect페이지를 요청하는 것입니다.

 

<form method = post action = "response_sendRedirect.jsp"> 

첫 페이지에서 SendRedirect 부분에 검색해서 전송을 누르면 response_sendRedirect.jsp(a.jsp)로 이동해

<% response.sendRedirect("page_control_end.jsp"); %> 을 통해 page_control_end.jsp(b.jsp)로 연결합니다.

 

좀 다른 점이 있는데 page_control_end.jsp(b.jsp)바로 넘어가는게 아니라 다시 클라이언트에게 

page_control_end.jsp(b.jsp)열어달라고 명령을 하는 겁니다.

 

<input type = text name = username>

그 과정에서 저장되어있는 username값이 사라지게 되어서 null값이 출력되는 것입니다.

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<% request.setCharacterEncoding("utf-8"); %>

<jsp:forward page="page_control_end.jsp">
	<jsp:param name = "tel" value = "000-000-0000"/> 
</jsp:forward>

<%-- param태그는 추가적으로 전달하고 싶은 태그 --%>
<%-- page 저기로 이동시키면서 name 하고 value를 같이 보냄 --%>
<%@ 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>
<H2>forward action 및 sendRedirect() 결과</H2>
<HR>
이름 : <%= request.getParameter("username") %> <BR> 
<%-- null값 왜냐하면 control에 요청 Redirect에서 control_end를 요청해라 지시 다시 
클라이언트가 control_end 요청 즉 username은 비어있음 --%>
<%-- forward로 한 경우 control username값하고 forword에 tel도 같이 넘어옴 --%>
전화번호 : <%= request.getParameter("tel") %>	
</body>
</html>

forward 방식을 통해 페이지 이동을 시킬 시에 sendRedirect처럼하지 않고 바로 페이지로 이동값이 남게 됩니다.

 

<jsp:forward page="page_control_end.jsp">
	<jsp:param name = "tel" value = "000-000-0000"/> 
</jsp:forward>

<jsp:forward page = "이동할 페이지"> 이걸로 페이지를 이동할 수 있습니다.

<jsp:param 속성 = 값 ....>을 이용해 default값을 설정할 수 있습니다. 저는 aa라고 치고 넘겼기 때문에

이름 : tel 이 아니라 aa가 나왔지만 전화번호 같은 경우 입력 안 했기 때문에 param에 입력한 게 나오게 됩니다.

반응형