본문 바로가기

Programming/[JSP]

[JSP] get방식의 sendRedirect로 Parameter Value 넘기기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<%@ 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>첫 페이지</title>
</head>
<body>
<% request.setCharacterEncoding("UTF-8"); %>
<form action="FowardAction.jsp" method="post">
<input type="text" name="name">
<input type="submit">
</form>
 
<form action="Response_sendRedirect.jsp" method="get">
<input type="text" name="name2">
<input type="submit">
</form>
 
<form action="Response_sendPost.jsp" method="post">
<input type="text" name="name3">
<input type="submit">
</form>
 
</body>
</html>
cs

첫 jsp 파일

여기서 정보를 입력

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ 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>두번째 페이지</title>
</head>
<body>
<% String name = request.getParameter("name"); %>
<jsp:forward page="Page_End.jsp">
    <jsp:param name="name" value="<%=name%>" />
    <jsp:param value="000-0000-0000" name="tell"/>
</jsp:forward>
</body>
</html>
cs

forward의 param으로 값 넘기기

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ 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>세번째 페이지</title>
</head>
<body>
<h2>Forward Action 및 sendRedirect() 결과</h2>
<hr>
지금 보이는 화면은 Page_End에서 출력한 결과 입니다.
<hr>
Name : <%=request.getAttribute("name")  %><br>
Tell : <%=request.getParameter("tell")  %>
</body>
</html>
cs

여기서 출력

 

1
2
3
4
<%@page import="java.net.URLEncoder"%>
<% String name2 = request.getParameter("name2");%> 
<% String encName2 = URLEncoder.encode(name2, "UTF-8"); %>
<% response.sendRedirect("Page_End.jsp?name="+encName2); %>
cs

get으로 한글이 깨지지 않게 인코딩한 후

데이터 넘기기

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@page import="java.net.URLEncoder"%>
<%@ 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 name = request.getParameter("name3");%> 
<% request.getSession().setAttribute("name", name); %>
<% response.sendRedirect("Page_End.jsp"); %>
</body>
</html>
cs

post 방식으로 sendRedirect 사용하기

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ 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>세번째 페이지</title>
</head>
<body>
<h2>Forward Action 및 sendRedirect() 결과</h2>
<hr>
지금 보이는 화면은 Page_End에서 출력한 결과 입니다.
<hr>
Name : <%=request.getSession().getAttribute("name")  %><br>
Tell : <%=request.getParameter("tell")  %>
</body>
</html>
cs

post 방식으로 sendReport를 할때는 page_end를 이렇게 바꿔야한다.

'Programming > [JSP]' 카테고리의 다른 글

[JSP] 쇼핑몰에서 장바구니에 담기  (0) 2018.04.13
[JSP] 스코프(Scope)란?  (0) 2018.04.12
[JSP] 에러 처리  (0) 2018.04.12
[JSP] 한글 깨짐 처리 (get방식)  (0) 2018.04.12
[JSP] 구구단 출력  (0) 2018.04.11