https://pathas.tistory.com/187 >>Action Tag 액션 태그 개요
<jsp:forward>
- 하나의 JSP 페이지에서 다른 JSP 페이지로 요청 처리를 전달할 때 사용된다.
- 제어권을 포워딩되는 페이지로 넘긴다.
- JSP 페이지 내에서 foward 액션 태그를 만나면 그 전까지 출력 버퍼에 저장되어 있던 내용을 제거한 후 지정 페이지로 이동
<%@ 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 bgcolor="pink">
이 파일은 action_red.jsp 파일입니다.<br>
브라우저에 배경색이 핑크로 나타날까요?<br>
노란색으로 나타날깝숑?
<jsp:forward page="0613action_yellow.jsp"/>
</body>
</html>
<%@ 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 bgcolor="yellow">
이 파일은 0613action_yellow.jsp파일
브라우저 주소창에 뭐라고 적혀있느냐?<br>
1)http://localhost:8090/after0613practice/0613action_yellow.jsp<br>
2)http://localhost:8090/after0613practice/0613action_red.jsp
</body>
</html>
- 서블릿에서 foward하는법
- sendredirect 하면 jsp주소가 뜨는데
- forward쓰면 서블릿주소가 뜬다 (제어권을 yellow로 넘기고 그 페이지로 이동돼서 yellow가 뜨는것 )
자자 forward랑 sendredirect의 차이가 무엇이냐
- sendRedirect -주소만 이동 값은 x
- forward - 값 넘겨준다 + url주소 안바뀌고 지정한 페이지에 값을 전달해줌
- 그으으으렇다면 request.getparameter도 있으니 이걸 활용해서 같이 연습해보자
<%@ 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>
<form action="0613pagemove(response).jsp">
<input type="text" name="name" value="쨍구">
<input type="submit" value="제출">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
pageContext.setAttribute("data1","집돌이 짱구");
request.setAttribute("data2", "도망다니는 짱구");
//response.sendRedirect("0613pageMove_result.jsp");
request.getRequestDispatcher("0613pageMove_result.jsp").forward(request,response);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String param = request.getParameter("name");
String data1 = (String)pageContext.getAttribute("data1");
String data2 = (String)request.getAttribute("data2");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
page:<%=data1 %><br>
param으로 한 request1:<%=param %><br>
set으로 한 request:<%=data2 %>
</body>
</html>
- jsp:forward로 forward를 해볼까?
<%@ 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>
<form action="0613pagemove(response).jsp">
<input type="text" name="name" value="쨍구">
<input type="submit" value="제출">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
pageContext.setAttribute("data1","집돌이 짱구");
request.setAttribute("data2", "도망다니는 짱구");
//response.sendRedirect("0613pageMove_result.jsp");
//request.getRequestDispatcher("0613pageMove_result.jsp").forward(request,response);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:forward page="0613pageMove_result.jsp">
<jsp:param value="33" name="age"/>
</jsp:forward>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String param = request.getParameter("name");
String param2 = request.getParameter("age");
String data1 = (String)pageContext.getAttribute("data1");
String data2 = (String)request.getAttribute("data2");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
page:<%=data1 %><br>
param으로 한 request1:<%=param %><br>
set으로 한 request:<%=data2 %><br>
jsp용으로 넘긴 것 param2 <%=param2 %>
</body>
</html>
'JSP > 이론' 카테고리의 다른 글
jsp:useBean/serProperty, getProperty (0) | 2023.06.14 |
---|---|
jsp:include vs include file (0) | 2023.06.13 |
Enumeration/session/ HttpSession (0) | 2023.06.13 |
<%@ %> 지시자 / error /include (0) | 2023.06.09 |
주석/JSP Page, Request, Session, Application (0) | 2023.06.08 |