관리 메뉴

nkdk의 세상

13장 requestDispathcer와 sendRedirect 본문

My Programing/JSP and Servlet

13장 requestDispathcer와 sendRedirect

nkdk 2008. 3. 9. 01:02
requestDispathcer와 sendRedirect로 를 이용한
서블릿과 jsp로 보내는 방법을 하였습니다.

member.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<script>
function check() {
if(mem.id.value=="") {
mem.id.focus();
alert("아이디 입력");
return;
}
mem.submit();
}
</script>
</head>
<body>
*** 회원 가입 ***<br>
<form name="mem" method="post" action="member.jsp">
아이디:<input type=text name=id size=10><p>
비밀번호:<input type=password name=pwd size=10><p>
이름:<input type=text name=name size=10><p>
직업:<select name="job">
<option value="학생">학생</option>
<option value="회사원">회사원</option>
<option value="기타">기타</option>
</select><p>
성 별:
<input type=radio name=sex value="남자" checked>남자
<input type=radio name=sex value="여자" >여자<p>
<input type=button value="등록" onClick="check()">
<input type=reset value="취소">
</form>
</body></html>

call.html

<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form method=post action="/jsp/irum.go">
이름 입력:<input type=text name=name value="Tom">
<input type=submit>
</form>
</body>
</html>

CallJsp

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class CallJsp extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=EUC-KR");
request.setCharacterEncoding("EUC-KR");
String str = request.getParameter("name");
PrintWriter out = response.getWriter();
// out.println("name : "+str);
// response.sendRedirect("/jsp/call.jsp?name="+str);
request.setAttribute("test", str); // 리퀘스트에 텍스트를 실어준다.
RequestDispatcher view = request.getRequestDispatcher("call.jsp"); // requestDispathcer를 불러옴
view.forward(request, response); // 실제로 보내는 부분입니
}
}

//call.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<html><body>
<h2>서블릿에서 호출된 JSP파일임.</h2>
<%
// sendRedirect
//String irum = request.getParameter("name");
//out.println("name : "+irum);

//RequestDispathcer
String ss = (String)request.getAttribute("test"); // test로 했기 때문임..
out.println("name : "+ss);
%>
</body></html>

// callhsp.html

<html><body>
<a href="calljsp.jsp">jsp 호출</a>
</body></html>

//calljsp.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
String ss ="jsp->jsp call";
// 이런 저런 처리가 있은 후
// response.sendRedirect("/jsp/calljsp2.jsp?msg=" + ss);

request.setAttribute("msg", ss);

RequestDispatcher view = request.getRequestDispatcher("calljsp2.jsp");
view.forward(request, response);
%>

//calljsp2.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<html>jsp 에서 호출된 jsp 파일임<p>
<%
// sendRedirect()
// String msg = request.getParameter("msg");
// out.println("메세지 : " + msg);

//requestDispathcer
String msg = (String)request.getAttribute("msg");
out.println("msg값의 출력 : "+msg);
%>
<body>

</body>
</html>

오쯔까레 ^_^