코딩기록 저장소 🐕/JSP

Servlet forward redirect

kimkongmom 2023. 9. 11. 16:03

 

 

 

▶forward 

    1. 다른 경로로 이동하는 방식 중 하나이다.
    2. 다른 경로로 직접 요청 파라미터를 이동시킨다.
    3. 경로를 작성할 때 URLMapping만 작성한다. (ContextPath는 작성하지 않는다)

 

 

forward1

 

1
2
3
4
5
6
7
8
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      
      // 요청의 전달 객체 생성 경유하는곳 경유
      RequestDispatcher dispatcher = request.getRequestDispatcher("/forward2");
      
      // 전달 (요청과 응답을 모두 전달함)
      dispatcher.forward(request, response);
    }
cs

 

forward2

 

1
2
3
4
5
6
7
8
9
10
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
      // 요청 인코딩
      request.setCharacterEncoding("UTF-8");
      
      // 요청 파라미터
      String name = request.getParameter("name");
      
      System.out.println("forward:" + name);
    }
cs

 

html.file

 

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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 
  <!--
                      /servlet
                      ┌----------------------------------┐
    NewFile.html  ->  | Forward1.java  ->  Forward2.java |
                      | /forward1          /forward2     |
                      └----------------------------------┘
    
    NewFile.html                  ->          Forward1.java 
    <a href="/servlet/forward1">
                      
    Forward1.java                 ->          Forward2.java
    request.getRequestDispatcher("/forward2")
    
  -->
 
  <div>
    <a href="/servlet/forward1?name=alice">포워드</a>
  </div>
</body>
</html>
cs

 

주소

 

 redirect

      1. 다른 경로로 이동하는 방식 중 하나이다.
      2. 서버가 다른 경로를 응답하면 클라이언트가 해당 경로로 직접 이동하는 방식이다.
      3. 경로를 작성할 때 ContextPath와 URLMapping을 모두 작성한다.

 

 

1
2
3
4
5
6
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
      // redirect할 경로를 응답함
      response.sendRedirect("/servlet/redirect2");
      
    }
cs

 

 

1
2
3
4
5
6
7
8
9
10
11
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
 
      // 1. 요청 인코딩
      request.setCharacterEncoding("UTF-8");
      
      // 2. 요청 파라미터
      String name = request.getParameter("name");
      
      System.out.println("redirect:" + name);
    }
cs

 

 

html.file

 

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
32
33
34
35
36
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 
  <!--
                      /servlet
                      ┌----------------┐
    NewFile.html  ->  | Redirect1.java |
                      | /request1      |
                      └----------------┘
                      
                      /servlet
                      ┌----------------┐
    NewFile.html  ->  | Redirect2.java |
                      | /request2      |
                      └----------------┘
    
    NewFile.html                  -> Redirect1.java 
    <a href="/servlet/request1">
                      
    Redirect1.java                -> NewFile.html
    response.sendRedirect("/servlet/redirect2")
    
    NewFile.html                  -> Redirect2.java
    get 방식으로 "/servlet/redirect2"로 이동 
    
  -->
  <div>
    <a href="/servlet/redirect1?name=alice">리다이렉트</a>
  </div>
</body>
</html>
cs

 

 

주소

 

 

 

 

'코딩기록 저장소 🐕 > JSP' 카테고리의 다른 글

니가만든 Cookie🍪  (0) 2023.09.13
Servlet binding session  (0) 2023.09.13
Servlet xml, json  (0) 2023.09.11
Servlet 응답  (0) 2023.09.11
JSP환경설정, Servlet 요청  (0) 2023.09.08