Coding Note

[JSP] ForwardActionTag 로그인 - 환영합니다! 본문

Web/Jsp

[JSP] ForwardActionTag 로그인 - 환영합니다!

jinnkim 2022. 2. 5. 10:30

 

 

forwardAction 태그 이용해서 로그인 회원 결과 구현하기!

 

 

구현

 

 

 

 

 

코드

 

1. login.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("utf-8");
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>로그인</title>
    <style type="text/css">
        form {
            padding: 22px;    
        }
        h1{
            margin: 20px 110px 0 110px
        }
        .btn {
            width: 330px;
            height: 250px;
            border: 5px solid #CEE3F6;
        }
        .cBtn {
            border: 2px solid #CEE3F6;
            background-color: #CEE3F6;
            border-radius: 5px;
            margin: 10px;
            width: 100px;
        }
        .id {
            margin: 15px;
        }
    </style>
    
</head>
<body>
    <div class="btn">
    <h1>로그인</h1>
    <form action="loginResult.jsp" method="post">
            아이디 : <input type="text" name="userId" class="id"><br>
            비밀번호 : <input type="password" name="userPwd" class="pwd"><br>
            <input type="submit" value="로그인하기" class="cBtn">
            <input type="reset" value="다시 입력하기" class="cBtn">    
    </form>
</div>    
</body>
</html>
cs

 

 

 

2. loginResult.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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("utf-8");
%>
<%-- ID 입력하지 않은 경우 자바의 RequestDispatcher를 사용하지 않고 포워드 액션태그를 이용해 다시 로그인 창으로 이동함.--%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>결과</title>
</head>
<body>
    <%
        String userId = request.getParameter("userId");
        if(userId.length() == 0){
            
    %>
            <jsp:forward page="login.jsp"/>        
    <%            
        }
    %>
            
    <h1>환영합니다. <%=userId %>님! </h1>
 
</body>
</html>
cs

 

 

 

< 공부하기 >

1.  <jsp:forward page="이동할 페이지. jsp"/>

    - 하나의 JSP 페이지에서 다른 JSP페이지로 요청 처리를 전달할 때 사용

 

 

Comments