티스토리 뷰


getRequestDispatcher 사용해서 url값으로 다시 넘김... forward를 통해 req,res를 같이 보냄


 

   request.getRequestDispatcher(url).forward(request, response);



getAttribute를 통해 request에 값을 넣음


 

   request.setAttribute("result", result);




[ 예제 ]


start.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
<%@ 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>Start</title>
</head>
<body>
    <form action="forwardtest.jsp">
        <center>
        
        <input type="text" name="num1">
        <input type="text" name="num2">
        
            <select name="type" style="width: 50px; height: 25px;">
                <option value="1"> + </option>
                <option value="2"> - </option>
                <option value="3"> * </option>
            </select> <input type="submit" value="전송">
        </center>
    </form>
</body>
</html>
cs


forwardtest.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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>forwardTest</title>
</head>
<body>
    <%
        String param = request.getParameter("type");
        String url = "result.jsp";
 
        int num1 = Integer.parseInt(request.getParameter("num1"));
        int num2 = Integer.parseInt(request.getParameter("num2"));
        int result = 0;
        
        if(param.equals("1"))
            result = num1 + num2;
        else if(param.equals("2"))
            result = num1 - num2;
        else if(param.equals("3"))
            result = num1 * num2;
        
        //request에 result 저장
        request.setAttribute("result", result);
        //Dispatcher를 사용해서 url값으로 다시 넘겨 주세요~ forward를 통해 req,res를 같이 보냄
        request.getRequestDispatcher(url).forward(request, response);
    %>
</body>
</html>
cs


result.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ 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>
<%
    int result = (Integer)request.getAttribute("result");
    out.print(result);
%>
</body>
</html>
cs



반응형

'언어 > JSP' 카테고리의 다른 글

[JSP] 영역 나누기(header,footer)  (0) 2016.10.28
[JSP] EL / JSTL 정리  (0) 2016.10.27
[JSP] request시 한글 깨질 경우  (0) 2016.10.26
[JSP] 톰캣 Server 한글 지원  (0) 2016.10.26
[JSP] 스크립틀릿, 표현식  (0) 2016.10.26
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
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
글 보관함