JSP - forward
Forward(포워드)
-request스코프에 담긴값 (request,response이 유지된다)
- 이동된 url이 화면에 보이지않는다(사용자는 알수없다)
-포워드 하는법 :
1.<jsp:forward page="이동할페이지">;
2.RequestDispatcher rd = request.getRequestDispatcher("이동할페이지");
rd.forward(request,response);
start.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<!-- 이 화면에서는 1,2,3 중 하나를 선택해서 -->
<!-- forwartTest.jsp로 선택한 값을 하나 들고 -->
<!-- 이동하도록 셋팅 -->
<form action="forwardTest.jsp">
<input type="text" name = "num1">
<input type="text" name = "num2">
<select name="type">
<option value="1">1번</option>
<option value="2">2번</option>
<option value="3">3번</option>
</select>
<input type="submit" value="가즈아!!!">
</form>
</body>
</html>
ForwardTest.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<%
String param = request.getParameter("type");
String url = "result.jsp";
int num1 = Integer.parseInt(request.getParameter("num1"));
int num2 = Integer.parseInt(request.getParameter("num2"));
int result = 0;
if(param.equals("1")){
result = num1 + num2;
}else if(param.equals("2")){
result = num1 - num2;
}else if(param.equals("3")){
result = num1 * num2;
}
//데이터를 리퀘스트 데이터에 담기.
request.setAttribute("result", result);
request.getRequestDispatcher(url).forward(request, response);
%>
</body>
</html>
a.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
여기는 a 라는 페이지입니다.
</body>
</html>
b.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
여기는 b 라는 페이지입니다.
</body>
</html>
c.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
여기는 c 라는 페이지입니다.
</body>
</html>