본문 바로가기
JSP

포워드(forward),액션태그(actiontag)

by 임혁진 2024. 1. 23.

forward와 sendRedirect의 차이

 

한마디로 데이터를 가지고 가냐 아니냐의 차이다

 

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 = "forward_ex02.jsp" method = "post">
	
		아이디 : <input type = "text" name = "id"><br>
		이름 : <input type = "test" name = "name"><br>
		<input type ="submit" value = "확인">
	
	
	</form>
	
</body>
</html>

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
	
    	<jsp:forward page="forward_ex03.jsp"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

</body>
</html>

바로 3페이지로 보낸다

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String id = request.getParameter("id");	
	String name = request.getParameter("name");	

%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

	<%=id %><br>
	<%= name %><br>
</body>
</html>

데이터가 저장되어 있다.

 

 

sendRedirect

<%@ 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 = "send_ex02.jsp" method = "post">
	
		아이디 : <input type = "text" name = "id"><br>
		이름 : <input type = "test" name = "name"><br>
		<input type ="submit" value = "확인">
	
	
	</form>
	
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
    	response.sendRedirect("send_ex03.jsp");
    
    
    %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

</body>
</html>

 

바로 2페이지로 보낸다

3페이지에 값이 없다.

 

자바 빈(VO)

자바 빈 이라는 폴더를 만들어 vo를 만들어준다

package com.example.bean;

public class quizVO {
	private String id,pw,name,address,kor,math ;

	public quizVO(String id, String pw, String name, String address, String kor, String math) {
		super();
		this.id = id;
		this.pw = pw;
		this.name = name;
		this.address = address;
		this.kor = kor;
		this.math = math;
	}

	public quizVO() {
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPw() {
		return pw;
	}

	public void setPw(String pw) {
		this.pw = pw;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getKor() {
		return kor;
	}

	public void setKor(String kor) {
		this.kor = kor;
	}

	public String getMath() {
		return math;
	}

	public void setMath(String math) {
		this.math = math;
	}

}

vo 생성

<%@ 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="quiz01_ok.jsp" method="post">
		아이디:<input type="text" name="id"><br>
		비밀번호:<input type="password" name="pw"><br>
		이름:<input type="text" name="name"><br>
		주소:<input type="text" name="address"><br>
		국어:<input type="text" name="kor"><br>
		수학:<input type="text" name="math"><br>	
		<input type="submit" value="확인">
	</form>
	
</body>
</html>

<%@page import="com.example.bean.quizVO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
/*
1. 폼태그를 처리할 수 있는 bean을 선언합니다.
2. 폼값을 받아서 평균과 총점을 구합니다.
3. quiz01_result페이지로 이동해서 
	학생정보와 평균, 총점을 모두 출력하세요.

조건: session (x)
*/
	request.setCharacterEncoding("utf-8");

	String id = request.getParameter("id");
	String pw = request.getParameter("pw");
	String name = request.getParameter("name");
	String address = request.getParameter("address");
	String kor = request.getParameter("kor");
	String math = request.getParameter("math");

	quizVO vo = new quizVO(id,pw,name,address,kor,math);

	request.setAttribute("vo",vo);
	
	request.getRequestDispatcher("quiz01_result.jsp").forward(request, response);

%>
<%@page import="com.example.bean.quizVO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
    	quizVO vo = (quizVO)request.getAttribute("vo");
    	double avg = (Double.valueOf(vo.getKor())+Double.valueOf(vo.getMath()))/2 ;
    	double sum = avg *2 ;
    %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	
	아이디:<%=vo.getId() %><br>
	패스워드:<%=vo.getPw() %><br>
	이름:<%=vo.getName() %><br>
	주소:<%=vo.getAddress()%><br>
	국어점수:<%=vo.getKor() %><br>
	수학점수:<%=vo.getMath() %><br>
	
	평균 <%=avg %><br>
	총점 <%=sum %>
	
</body>
</html>

 

ActionTage 

 

JSP파일로 생성한다.

 

ex01 page

<%@ 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>

	<%--
	액션태그 -
	<jsp : 이름으로 시작하고, 특정한 기능들을 가지고 있는 jsp태그
	
	종속되는 태그가 없다면 /> 이용해서 반드시 마감처리
	
	
	 --%>
	 
	 
	 <jsp:forward page="forward_ex02.jsp" />
	 
</body>
</html>

 

액션 태그

JSP : 이름으로 시작하고 , 특정한 기능을 가지고 있다. /> 이용해서 마감처리 하지 않으면 실행이 되지않는다.

 

ex02 page

<%@ 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>

<h3>여기는 ex02 페이지</h3>
</body>
</html>

 

ex03 page

<%@ 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>

	<a href="../forward?name=홍길동">forward_ex04</a>
</body>
</html>

 

ex04 page( ex03 page에서의 name=홍길동 이란 변수가 넘어온다)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% 
	String name = request.getParameter("name");
	
	System.out.println("넘어온 값을 소비:" +name);
	
	//리퀘스트에 강제로 값을 저장
	request.setAttribute("data", "홍길동 20세");
	
	
	//자바코드로 포워드를 사용하는방법
	//RequestDispatcher dp =request.getRequestDispatcher("forward_ex05.jsp");
	//dp.forward(request, response);
	
	request.getRequestDispatcher("forward_ex05.jsp").forward(request,response);



%>

ex03에서 받은 name 변수(홍길동이 들어있다) 를 받아서 사용할 수 있다.

 

 

ex05 page ( request에 setAttribute로 값을 담는다)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
    
    //setAttribute로 담은 값을 받는방법
    String data = (String)request.getAttribute("data");
    
    
    
    %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%=data %>
		
</body>
</html>

 

'JSP' 카테고리의 다른 글

페이징  (0) 2024.01.23
MVC 패턴  (0) 2024.01.23
예외 페이지  (0) 2024.01.23
쿠키와 세션  (0) 2024.01.22
JSP 내장객체  (0) 2024.01.22