jsp 페이지 넘기기 / forward 방식 코드 간소화한 예제
start.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 |
<%@ 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>
<form action = "forwardTagTest.jsp">
<select name = "number">
<option value = "1">1</option>
<option value = "2">2</option>
<option value = "3">3</option>
</select>
<input type = "submit" value = "전송">
</form>
</body>
</html> |
cs |
start.jsp에서는 1,2,3중에서 숫자를 선택합니다.
forwardTagTest.jsp
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
28
29
30
31 |
<%@ 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">
<%!
public String select(String type){
String url = "";
if(type.equals("1")){
url = "a.jsp";
}
else if(type.equals("2")){
url = "b.jsp";
}
else if(type.equals("3")){
url = "c.jsp";
}
return url;
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String type = request.getParameter("number");
String url = select(type);
%>
<jsp:forward page="<%=url %>"></jsp:forward>
</body>
</html> |
cs |
이렇게 위쪽에서 함수를 작성해서 url에 a.jsp , b.jsp , c.jsp 등의 이동하고자 하는 jsp 파일 이름을 적습니다.
그리고 <jsp.forward page="url의 값"></jsp:forward> 코드에서 <%=url>을 사용해서 페이지를 이동합니다.
a.jsp
1
2
3
4
5
6
7
8
9
10
11
12 |
<%@ 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>
a.jsp
</body>
</html> |
cs |
b.jsp
1
2
3
4
5
6
7
8
9
10
11
12 |
<%@ 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>
b.jsp
</body>
</html> |
cs |
c.jsp
1
2
3
4
5
6
7
8
9
10
11
12 |
<%@ 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>
c.jsp
</body>
</html> |
cs |
이동하고자 하는 a.jsp , b.jsp , c.jsp 코드입니다.
함수를 사용해서 코드를 간략하게 작성해봤습니다.
jswoo030@gmail.com 으로 질문을 보내시면 빠른 답변을 받으실 수 있습니다.
댓글