요청 전달 ↓

1. RequestDispatcher방식 - 동일한 웹서버내에 웹 컨텍스트내의 jsp나 servlet으로 요청을 

전달할 수 있습니다. (다른 웹서버 혹은 다른 웹 컨텍스트로 요청 전달 불가) - 정보 공유 : 요청을 전달할때 다른 추가적인 정보를 전달할 수 있습니다. - url과 실제 응답 content의 page와 다름

2. sendRedirect방식 - 동일한 웹서버, 다른 웹서버로 요청을 전달 가능 - 요청은 get 방식으로 전달됩니다. - 동일한 웹서버의 jsp나 servlet으로 전달된 경우 전달된 페이지에서 request, response객체가 새로 생성됩니다 그러므로 정보 공유 불가능합니다. - 최종 응답페이지의 경로가 url로 표시됩니다.



---------------------------------------------------------------------------------------------------------


샘플 파일 구성


basicform.jsp  ==>  appendform.jsp  =>  resultInfo.jsp


1. basicform.jsp  - 아이디와 이메일을 입력받는다.

2. appendform.jsp  - 추가로 주소와 핸드폰 번호를 추가한다.

3. resultInfo.jsp  - 최종 결과 화면으로 아이디, 비밀번호, 주소, 핸드폰 번호를 출력한다.



▶basicform.jsp

<%@ page contentType="text/html; charset=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>basicform.jsp</title>

</head>

<body>

<center><h3>기본 정보 입력 폼</h3></center><hr>

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

이름 : <input type="text" id="userid" name="userid"><br>

이메일 : <input type="text" id="email" name="email"><br>

<input type="submit" value="다음">

<input type="reset" value="취소">

</form>

</body>

</html>


appendform.jsp

<%@ page contentType="text/html; charset=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>appendform.jsp</title>

</head>

<body>

<%

request.setCharacterEncoding("utf-8");

request.setAttribute("address", "서울시 강남구");

request.setAttribute("phone", "010-123-1234");

//response객체 : javax.servlet.http.HttpServletResponse타입

//절대경로

//response.sendRedirect("http://localhost:8000/web3/resultInfo.jsp"); 

//상대경로 & 쿼리

//response.sendRedirect("./resultInfo.jsp?address=seoul&phone=010-123-1234");

  String addr = "서울시 강남구";

String addrValue=URLEncoder.encode(addr, "utf-8");

response.sendRedirect("./resultInfo.jsp?address=addr&phone=010-123-1234");

response.sendRedirect("http://www.naver.com");

%>

</body>

</html>


resultInfo.jsp

<%@ page contentType="text/html; charset=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>resultInfo.jsp</title>

</head>

<body>

<%

//application 내장객체 : javax.servlet.ServletContext타입

out.println("userid : " + request.getParameter("userid") + "<br>");

out.println("email : " + request.getParameter("email") + "<br>");

out.println("address : " + request.getAttribute("address") + "<br>");

out.println("phone : " + request.getAttribute("phone") + "<br>");

%>


</body>

</html>


결과화면


response.sendRedirect("http://www.naver.com");  를 이용하였기 때문에 네이버로 이동한다.

+ Recent posts