티스토리 뷰

오늘은 이런식으로 데이터베이스에서 로그인 상태인 회원의 정보를 출력하는 방법에 대해서 알아보겠습니다.


우선 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE html>
<html>
    <head>
            <link href="assets/css/bootstrap.css" rel="stylesheet">
        <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
        <link rel="stylesheet" href="${pageContext.request.contextPath}/assets/css/memberinfo.css" type="text/css">
        <script type = "text/javascript" src="${pageContext.request.contextPath}/assets/js/memberinfo.js"></script>
    </head>
    <body>
    <div class="container">
      <div class="row">
      <div class="col-md-5  toppad  pull-right col-md-offset-3 ">
       <br>
      </div>
        <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 col-xs-offset-0 col-sm-offset-0 col-md-offset-3 col-lg-offset-3 toppad" >
   
   
          <div class="panel panel-info">
            <div class="panel-heading">
              <h3 class="panel-title">${userinfo.bId}님의 프로필 정보</h3>
            </div>
            <div class="panel-body">
              <div class="row">
                <div class="col-md-3 col-lg-3 " align="center"> <img alt="User Picture" src="${pageContext.request.contextPath}/assets/rion.png" class="img-circle img-responsive"> </div>
 
                <div class=" col-md-9 col-lg-9 "
                  <table class="table table-user-information">
                    <tbody>
                      <tr>
                        <td>닉네임:</td>
                        <td>${userinfo.bNick}</td>
                      </tr>                 
                      <tr>
                        <td>학교:</td>
                        <td>${userinfo.bSchool}</td>
                      </tr>
                   
                         <tr>
                             <tr>
                      </tr>
                        <tr>
                        <td>학년:</td>
                        <td>${userinfo.bGrade}</td>
                      </tr>
                      <tr>
                        <td>전공:</td>
                        <td>${userinfo.bMajor}</td>
                           
                      </tr>
                      <tr>
                        <td>이메일 주소:</td>
                        <td><a href="mailto:info@support.com">${userinfo.bEmail}</a></td>
                      </tr>
                      
                     
                    </tbody>
                  </table>
                  
                 <!--   <a href="#" class="btn btn-primary">My Sales Performance</a>
                  <a href="#" class="btn btn-primary">Team Sales Performance</a>-->
                  
                </div>
              </div>
            </div>
                 <div class="panel-footer">
                        <a data-original-title="Broadcast Message" data-toggle="tooltip" type="button" class="btn btn-sm btn-primary"><i class="glyphicon glyphicon-envelope"></i></a>
                        <span class="pull-right">
                            <a href="edit.html" data-original-title="Edit this user" data-toggle="tooltip" type="button" class="btn btn-sm btn-warning"><i class="glyphicon glyphicon-edit"></i></a>
                            <a data-original-title="Remove this user" data-toggle="tooltip"  type="button" class="btn btn-sm btn-danger" onClick="self.close();"><i class="glyphicon glyphicon-remove"></i></a>
                        </span>
                    </div>
            
          </div>
        </div>
      </div>
    </div>
    </body>    
</html>
cs

커맨드 객체인 userinfo의 정보를 출력하고 있는 모습입니다. userinfo라는건 어디서 어떻게 가져오는지 알아봅시다.


컨트롤러


@RequestMapping(value="/memberinfo")
public String memberinfo(Model model,HttpSession httpsession) {
return "memberinfo";
}


이런식으로 사용자가 /memberinfo 라는 요청을 전송했을때 컨트롤러는 memberinfo.jsp파일로 제어를 넘깁니다.

httpsession안에 있는 userinfo라는 커맨드 객체도 jsp파일로 넘어가기 때문에 우리가 뷰에서 커맨드 객체를 사용할수 있는것입니다.

이 httpsession은 어디서 설정하는지 알아보겠습니다.


LoginOutController


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
@RequestMapping("/login")
    public String LogIn(UserDto userDto,Errors errors,HttpSession httpSession,HttpServletResponse response) throws IOException
    {
        LogInService logInService = new LogInService();
        
        try {
            UserDto resultDto = logInService.login(sqlsession,userDto);
            httpSession.setAttribute("userinfo", resultDto);
        } catch (Exception e) {
            errors.reject("IDPASSNOTMATCH");
            return "login";
        }
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<script>alert('성공적으로 로그인 되었습니다.');</script>");
        out.flush();
        return "index";
    }
 
 
 

 
cs













로그인 컨트롤러의 빨간색 부분에서 세션에 로그인 한 회원의 정보가 담기게 됩니다.
전체적인 동작 순서를 정리해보면


ㅁㄴㅁㄴㅇㅁㅇㄴㅁㅇㅁㄴㅇ




로그인 컨트롤러의 빨간색 부분에서 세션에 로그인 한 회원의 정보가 담기게 됩니다. 
전체적인 동작 순서를 정리해보면
1.사용자가 로그인창에서 아이디와 비밀번호를 입력한다. (이 정보는 커맨드 객체인 userDto에 담긴다.)
 
2.login 컨트롤러에서 로그인 서비스 객체로 제어를 넘긴다.(userDto도 함께)
 
3. 서비스 객체에서는 MyBatis를 사용해서 쿼리문을 맵핑시킨 Dao를 통해 
 
사용자가 입력한 아이디에 해당하는 모든 정보를 셀렉트 해와서 resultDto라는 결과 커맨드 객체에 담는다.
(쿼리문 -> select * from userinfo where bId = ?) ?는 userDto.bId
 
4.서비스 객체에서 userDto.bId로 가져온 데이터가 없는경우 예외를 컨트롤러로 던지고,  
가져온 데이터가 있을경우 그 가져온 데이터인 resultDto의 패스워드와 처음에 사용자가 입력한 userDto의 패스워드가
일치하는지 확인하고 일치하지않을경우 예외를 발생시키고 컨트롤러로 그 예외를 다시 던진다. 
 
컨트롤러 에서는 세션에 결과 객체인 resultDto를 userinfo라는 이름으로 담는다.
 
 
5. 무난하게 예외없이 실행이 된다면 컨트롤러는 memberinfo.jsp로 제어를 넘기며 
 
동시에 세션도 매개변수를 통해서 뷰로 같이 전달이 되기때문에  우리는 뷰에서 세션의 값을 사용할수 있게 된다








댓글
최근에 올라온 글
최근에 달린 댓글
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
글 보관함