은은하게 코드 뿌시기

[jsp 내부객체] - session, application, pageContext 본문

웹/JSP

[jsp 내부객체] - session, application, pageContext

은은하게미친자 2022. 8. 9. 22:16
728x90

session, application, pageContext  내부객체는 현재 실행되는 페이지의 외부환경정보와 관련된 내부객체들 입니다.

session 은 요청에 관한 Context를 제공하고

application은 서블릿 Context를 제공하며

pageContext는 jsp페이지 자체의 Context를 제공합니다.

 

3-1. session

: 클라이언트 요청에 대한 context 정보의 세션과 고나련된 정보를 저장하고 관리하는 내부객체, page 지시자의 session 속성이 true(기본값)로 설정 되어 있어야 사용할 수있습니다.

메소드 설명
String getid() 세션 id를 반환합니다
long getCreationTime() 세션의 생성된 시간을 반환합니다.
long getLastAccessedTime() 클라이언트 요청이 마지막으로 시도된 시간을 반환합니다.
void setMaxinactiveinterval(time) 세션을 유지할 시간을 초단위로 설정합니다.
int getMaxinactiveinterval() string을 브라우저에 출력합니다.
boolean isNew() 클라이언트 세션ID를 할당하지 않은 경우 true값을 반환합니다.
void invalidate() 해당 세션을 종료 시킵니다.

 

예제

더보기
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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
    <%
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        String pw   = request.getParameter("pw");
        
        
        if(id.equals("admin")&&pw.equals("1234")){
            
            session.setAttribute("sid", id);
            session.setAttribute("spw", pw);
            
            %>
                <script>
                    alert("로그인이 되었습니다.");
                    location.href="indexttest.jsp"
                </script>
            <%
            
            
        } else {
            %>
                <script>
                    alert("로그인실패");
                    location.href="indexttest.jsp"
                </script>
            <%
        }
    
    
    %>
    
    
    
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    getParameter : <%=id %> <br>
    getParameter : <%=pw %> <br>
 
</body>
</html>
cs

 

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
 
<head>
    <jsp:include page="include/header.jsp"></jsp:include>
</head>
 
<body id="page-top">
 
    <!-- Page Wrapper -->
    <div id="wrapper">
 
        <!-- Sidebar -->
        <jsp:include page="include/nav.jsp"></jsp:include>
        <!-- End of Sidebar -->
 
        <!-- Content Wrapper -->
        <div id="content-wrapper" class="d-flex flex-column">
 
            <!-- Main Content -->
            <div id="content">
 
                <!-- Topbar -->
                <jsp:include page="include/topbar.jsp"></jsp:include>
                <!-- End of Topbar -->
 
                <!-- Begin Page Content -->
                <div class="container-fluid">
 
                    <!-- Page Heading -->
                    <div class="d-sm-flex align-items-center justify-content-between mb-4">
                        <h1 class="h3 mb-0 text-gray-800">LOGIN</h1>
                        <a href="#" class="d-none d-sm-inline-block btn btn-sm btn-primary shadow-sm"><i
                                class="fas fa-download fa-sm text-white-50"></i> Generate Report</a>
                    </div>
 
                    <!-- Content Row -->
                    <div class="row">
                    
                        <%
                            if (session.getAttribute("sid")==null) {
                        %>
                           <form action="logOK.jsp" method="post">
                            <input type="ID" name="id" class="form-control form-control-user"   aria-describedby="ID" placeholder="Enter ID">                    
                            <input type="password" name="pw" class="form-control form-control-user" placeholder="Password">
                            <input type="submit" class="btn btn-primary btn-user btn-block">
                            <input type="reset" class="btn btn-primary btn-user btn-block">
                               
                           </form>
 
                        <%
                            } else {
                                
                                
                        %>
                            <div>
                                <%=session.getAttribute("sid")%> 님<br>
                                <a href="logout.jsp">로그아웃</a>
                            </div>
                        
                        <% 
                                
                            }                           
                            
                        %>
                    
                        
                    </div>
 
                </div>
                <!-- /.container-fluid -->
 
            </div>
            <!-- End of Main Content -->
 
            <!-- Footer -->
            <jsp:include page="include/footer.jsp"></jsp:include>
            <!-- End of Footer -->
 
        </div>
        <!-- End of Content Wrapper -->
 
    </div>
    <!-- End of Page Wrapper -->
 
    <!-- Scroll to Top Button-->
    <a class="scroll-to-top rounded" href="#page-top">
        <i class="fas fa-angle-up"></i>
    </a>
 
    <!-- Logout Modal-->
    <div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
                    <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">Select "Logout" below if you are ready to end your current session.</div>
                <div class="modal-footer">
                    <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
                    <a class="btn btn-primary" href="login.html">Logout</a>
                </div>
            </div>
        </div>
    </div>
 
    <!-- Bootstrap core JavaScript-->
    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
 
    <!-- Core plugin JavaScript-->
    <jsp:include page="include/plugin.jsp"></jsp:include>
 
</body>
 
</html>
cs

3-2. application

: 서블릿 또는 어플리케이션 외부 환경 정보(Context)를 나타내는 내부 객체, 

application객체를 통해서 어플리 케이션이 실행되는 서버의 정보와 서버 측 자원에 대한 정보를 얻어내거나 어플리케이션이 실행하고 있는 동안에 발 생 할  수있는 이벤트 로그와 관련된 기능을 제공합니다.

*application 내부객체의 메소드

메소드 설명
String getServerInfo() 서블릿 컨테이너의 이름과 버전을 반환합니다.
String getMimeType(fileName) 지정한 파일의 mime타입을 반환합니다.
String getRealPath(url) url를 로컬 파일 시스템으로 변경하여 반환합니다.
void log(message) 로그파일에 message를 기록합니다.

3-3 pageContext

: 현재 jsp페이지의 context를 나타내며, pageContext 내부 객체를 통해서 다른 내부객체에 접근할 수가 있습니다.

ex)) JspWriter pageout=pageContext.getOut();

 

*pageContext 내부객체의 메소드

메소드 설명
ServeletRequest getRequest() 페이지 요청 정보를 담고있는 객체를 반환합니다.
ServeletResponse getResponse() 페이지 요청에 대한 응답 객체를 반환합니다.
JspWriter getOut() 페이지 요청에 대한 응답 출력 스트림을 반환합니다.
HttpSession getSession() 요청한 클라이언트의 세션정보를 담고있는 객체를 반환합니다.
ServeletContex getServiceContext() 페이지에 대한 서블릿 실행 환경 정보를 담고있는 객체를 반환합니다.
Object getPage() 페이지의 서블릿 객체를 반환합니다.
ServletConfig getServeletConfig() 페이지의 서블릿 초기 정보의 설정 정보를 담고있는 객체를 반환합니다.
Exception getExeption() 페이지 실행 중에 발생되는 에러 페이지에 대한 예외 객체를 반환합니다.

 

 

 

 

728x90
Comments