웹
다음 페이지로 값 넘기기 ( foward 없이 )
코드 죄수
2022. 9. 20. 09:23
page 1 - 정보를 받는 페이지
<%@ 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>
<form method=post action=forward_action2.jsp>
forward action : <input type=text name=username>
<input type=submit value=확인>
</form>
</body>
</html>
page 2 - 다음 페이지로 정보를 넘기는 페이지
sendRedirect방식
sendRedirect로 페이지를 그냥 넘기면 다음 페이지에서 값을 얻을 수없다
따라서 get방식 으로 url에 값을 붙여서 넘기면 다음 페이지가 받을 수 있다.
<%@ 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>
<%request.setCharacterEncoding("UTF-8"); %>
<%
String name = request.getParameter("username");
request.setAttribute("username", request.getParameter("username"));
request.setAttribute("tel", "000-0000-0000");
response.sendRedirect("page_control_end.jsp?username=" + name+"&tel="+"000-0000-0000");
%>
</body>
</html>
hidden 방식
js를 이용하여 페이지가 로드 되자마자 form에 있는 값을 넘겨 버린다.
<%@ 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>
<script>
window.onload = function(){
var submitfunction = document.getElementById("userinfo");
submitfunction.submit();
}
</script>
</head>
<body>
<%request.setCharacterEncoding("UTF-8"); %>
<form id = "userinfo" method="post" action="page_control_end.jsp">
<input type="hidden" name = "username" value='<%=request.getParameter("username")%>'>
<input type="hidden" name = "tel" value='000-0000-0000'>
</form>
</body>
</html>
page 3 - 정보를 받아서 띄우는 페이지
<%@ 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>
<CENTER>
<h2>foward action 및 sendRedirect() 결과</h2>
<hr>
이 페이지는 page_control_end에서 출력한 결과 입니다.
<hr>
이름 : <%= request.getParameter("username") %><br>
전화번호 : <%= request.getParameter("tel") %>
</CENTER>
</body>
</html>