티스토리 뷰

Send15.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

	
<!--  포워딩 / 리다이렉트 관련한 ★ 중요한 ★실습 -->
<!--  사칙 연산 수행을 위항 정수 입력 페이지를 구성한다. -->
<!--  이 과정에서 연산자를 함께 입력받을 수 있도록 한다. -->
<!--  사용자 최초 요청 페이지 -->
<!--  ->http://localhost:8090/WebApp05/Send15.jsp -->

<!--  데이터 수신 및 연산 전용페이지(send15에서 수신한 데이터로) -->
<!--  ->http://localhost:8090/WebApp05/Forward15.jsp -->
<!--  ->java ->Servlet담당으로 나중에는 변환할 것임. -->

<!--  최종 결과 출력 페이지 -->
<!--  구성한 내용을 최종적으로 -->
<!--  ->http://localhost:8090/WebApp05/Receive15.jsp-->
	<div>
		<h1>JSP를 이용한 데이터 송수신 실습</h1>
		
	</div>
	
	<div>
		<h2>Send15.jsp ● -> Forward15.jsp ○ ->Receive15.jsp ○</h2>
		
	</div>
	
	<div>
		<!-- <form action="Forward15.jsp" method="post"> -->
		<form action="Forward15_1.jsp" method="post">
			정수1<input type="text" name="num1" class="txtNumber">
			<select name="calResult">
				<option value="0" selected="selected">연산선택
				<option value="+">더하기</option>
				<option value="-">빼기</option>
				<option value="*">곱하기</option>
				<option value="/">나누기</option>
			</select>
			정수2<input type="text" name="num2" class="txtNumber">
			
			<button type="submit" >확인</button>
		</form>
	</div>
</body>
</html>

Forward15.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%

	int num1=Integer.parseInt(request.getParameter("num1"));
	int num2=Integer.parseInt(request.getParameter("num2"));
	String calResult=request.getParameter("calResult");
	
	String result="";
	
	if(calResult.equals("+"))
	{
		result=String.format("%d+%d=%d",num1,num2,num1+num2);
	}
	else if(calResult.equals("-"))
	{
		result=String.format("%d-%d=%d",num1,num2,num1-num2);
	}
	else if(calResult.equals("*"))
	{
		result=String.format("%d*%d=%d",num1,num2,num1*num2);
	}
	else if(calResult.equals("/"))
	{
		result=String.format("%d/%d=%d",num1,num2,num1/(double)num2);
	}
	
	request.setAttribute("message",result);
	

%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<jsp:forward page="Receive15.jsp"></jsp:forward>
</body>
</html>

Receive15.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%

	String strResult=(String)request.getAttribute("message");


%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<div>
		<p>최종 페이지</p>
	</div>
	
	
	<div>
		<h2><%=strResult %></h2>	
	
	</div>
</body>
</html>

 

 

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

<%@ page contentType="text/html; charset=UTF-8"%>
<%

	request.setCharacterEncoding("UTF-8");
	

	int num1=Integer.parseInt(request.getParameter("num1"));
	int num2=Integer.parseInt(request.getParameter("num2"));
	String calResult=request.getParameter("calResult");
	
	String result="";
	
	if(calResult.equals("+"))
	{
		result=String.format("%d+%d=%d",num1,num2,num1+num2);
	}
	else if(calResult.equals("-"))
	{
		result=String.format("%d-%d=%d",num1,num2,num1-num2);
	}
	else if(calResult.equals("*"))
	{
		result=String.format("%d*%d=%d",num1,num2,num1*num2);
	}
	else if(calResult.equals("/"))
	{
		result=String.format("%d/%d=%d",num1,num2,num1/(double)num2);
	}
	
	request.setAttribute("message",result);
	
	
	//check ~~ 자바 문법으로 바꿔주는 역할.
	RequestDispatcher dispatcher=request.getRequestDispatcher("Receive11.jsp");
	dispatcher.forward(request,response);
	
	/*------------------------------------------------------------------------------------
	■■■ RequestDispatcher 인터페이스 ■■■
	※ 이 인터페이스는 『forward()』와 『include()』만 있다.
	
	※ 처리 과정 및 개념
	 
	   일반적으로 HttpServlet 을 상속받는 클래스... 서블릿
	   
	   이렇게 작성된 클래스 내부에는
	   실제 요청을 서비스하는 『doGet()』과 『doPost()』 메소드가
	   정의되어 있으며 (service() 메소드가 상위 메소드)
	   
	   Servlet Container는 『HttpServlet』의 인스턴스를 생성하고,
	   『init()』 메소드를 실행해주고,
	   이 메소드에 의해 매핑된 URL에 (페이지 요청 방식에 따라)
	   doGet()과 doPost()중 메소드를 호출해 주고(실행시켜 주고)
	   Container가 종료될 때 『destroy()』 메소드를 호출해 주고,
	   관련된 처리 과정이 마무리 된다.
	   
	   즉,Servlet Container가
	   init() ->처음
	   service() ->중간중간 요청이 있을 때 마다
	   destroy() ->마지막
	   메소드를 호출한다
	   (절대 우리가 직접 호출하는 것이 아님 ~!!!!)
	   
	   결국 『HttpServlet은』
	   하나의 인스턴스만 생성되어 멀티 스레딩으로 돌아가게 된다.
	   
	   이렇게 구성되는 『HttpServlet』의 상속된 클래스의 인스턴스는
	   스레드에 안전하게 설계되어야 하고 따라서 스레드를 안전하게 설계하지 않은 경우
	   상위 클래스를 마구 접근하게 되어 에러가 발생할 수 밖에 없다.
	   
	   이와 같은 이유로 환경 설정이나 J2EE 서비스에 관한 냐용은
	   『ServletContext』에서 할 수 있게 된다.
	   (서블릿에 대한 환경 설정, 상태 등을 설정할 수 있는 객체)
	   이 『ServletContext』는 『getServletContext』로만 받을 수 있다.
	   이 『getServletContext』는 동기화가 제대로 구현되어 있을 것이라고
	   예측할 수 있다.
	   그 이유는 멀티 스레드에 안전하게 설계되어 있어야
	   우리가 『ServletConetxt』의 『setAttribute()』나 getAttribute()를 
	   스레드 걱정 없이 마음대로 읽고 쓸 수 있기 때문이다.
	   
	   『ServletContext』의 또다른 커다란 기능 중 하나는
	   다른 서블릿 인스턴스를 가져올 수 있다거나
	   서블릿 환경 설정값을 가져올 수 있는 기능이다.
	   
	   『RequestDispatcher』역시 그 기능 중 하나이다.
	   사전적인 의미로는 ... 요청을 제공하는 도구
	   즉,요청을 보내주는 인터페이스이다.
	-------------------------------------------------------------------------------------*/
%>
<jsp:forward page="Receive15.jsp"></jsp:forward>

'Web(국비) > JSP' 카테고리의 다른 글

[JSP]post방식 데이터 값 넘기기  (0) 2019.10.25
[JSP]RequestDispatcher인터페이스란?  (0) 2019.10.25
JSP  (0) 2019.10.24
포워딩 리다이렉트 차이  (0) 2019.10.24
포워딩과 리다이렉트란?  (0) 2019.10.24