7. 주석

jspComment.jsp

<%@ page contentType = "text/html; charset=utf-8" %>
<html>
<head><title>JSP 주석</title></head>
<body>
<%-- JSP 주석입니다. --%>
<!-- HTML 주석입니다. -->
주석은 출력 결과에 포함되지 않습니다.
</body>
</html>

8. JSP 웹어플리케이션 폴더 구조와 URL 매핑

8.1. 웹어플리케이션 폴더와 URL 관계

8.1.1 컨텍스트 경로(Context path) 리턴 메소드

getContextPath.jsp

<%@ page contentType = "text/html; charset=utf-8" %>
<html>
<head><title>웹 어플리케이션 경로 구하기</title></head>
<body>

웹 어플리케이션 컨텍스트 경로: "<%= request.getContextPath() %>"

</body>
</html>

8.1.2. 웹어플리케이션 그외 폴더(Tomcat)

8.1.3 웹어플리케이션 배포

9. JSP 기본 객체 SCOPE

9.1 page scope

page_scope_set.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Page Scope Set</h1>
<%
pageContext.setAttribute("scope", "Page");
String scope = (String)pageContext.getAttribute("scope");
%>
Scope : <%=scope %> <br>
<hr>
<a href="page_scope_use.jsp">Scope Use</a>
</body>
</html>

page_scope_use.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Request Scope Use</h1>
<%
String scope = (String)request.getAttribute("scope");
%>
Scope : <%=scope %> <br>

</body>
</html>

9.2 request scope

request_scope_set.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Request Scope Set</h1>
<%
request.setAttribute("scope", "Request");
String scope = (String)request.getAttribute("scope");
%>
Scope : <%=scope %> <br>
<hr>
<a href="request_scope_use.jsp">Scope Use</a>

<jsp:forward page="request_scope_use.jsp"></jsp:forward>
</body>
</html>

request_scope_use.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Request Scope Use</h1>
<%
String scope = (String)request.getAttribute("scope");
%>
Scope : <%=scope %> <br>

</body>
</html>

9.3 session scope

session_scope_set.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Session Scope Set</h1>
<%
session.setAttribute("scope", "Session");
String scope = (String)session.getAttribute("scope");
%>
Scope : <%=scope %> <br>
<hr>
<a href="session_scope_use1.jsp">Scope Use</a>
</body>
</html>

session_scope_use1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Session Scope Use1</h1>
<%
String scope = (String)session.getAttribute("scope");
%>
Scope : <%=scope %> <br>
<hr>
<a href="session_scope_use2.jsp">Scope Use</a>
</body>
</html>

session_scope_use2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Session Scope Use2</h1>
<%
String scope = (String)session.getAttribute("scope");
%>
Scope : <%=scope %> <br>
<hr>
<a href="session_scope_use1.jsp">Scope Use</a>
</body>
</html>

9.4 application scope

application_scope_set.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Application Scope Set</h1>
<%
application.setAttribute("scope", "Application");
String scope = (String)application.getAttribute("scope");
%>
Scope : <%=scope %> <br>
<hr>
<a href="application_scope_use1.jsp">Scope Use</a>
</body>
</html>

application_scope_use1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Application Scope Use1</h1>
<%
application.setAttribute("scope", "Application");
String scope = (String)application.getAttribute("scope");
%>
Scope : <%=scope %> <br>
<hr>
<a href="application_scope_use2.jsp">Scope Use</a>
</body>
</html>

application_scope_use2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Application Scope Use1</h1>
<%
application.setAttribute("scope", "Application");
String scope = (String)application.getAttribute("scope");
%>
Scope : <%=scope %> <br>
<hr>
<a href="application_scope_use1.jsp">Scope Use</a>
</body>
</html>

9.5 scope 활용

total_num.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
Object oVisit = application.getAttribute("visit");

int iVisit = 0;
if(oVisit != null){
    iVisit = (int)oVisit;
}
iVisit = iVisit + 1;
application.setAttribute("visit", iVisit);
%>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="total_num.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
총 페이지호출 수 : <%=application.getAttribute("visit"); %> <br>
현재 로그인 수 : <%=application.getAttribute("login"); %>
<hr>
<% if(session.getAttribute("id") == null){ %>

<form action="login_ok.jsp" method="post">
i d : <input type="text" name="id" > <br>
pwd : <input type="text" name="pwd" > <br>
<input type="submit" value="LOGIN">
</form>
<%} else { %>

로그인 ID : <%=session.getAttribute("id") %> |
<a href="logout_ok.jsp">LOGOUT</a>

<%} %>
</body>
</html>

login_ok.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%

String id = request.getParameter("id");
String pwd = request.getParameter("pwd");

if(id.equals("test")){
    if(pwd.equals("1234")){
        // ok
        session.setAttribute("id", id);

        int iLogin = 0;
        Object oLogin = application.getAttribute("login");
        if(oLogin != null){
            iLogin = (int)oLogin;
        }
    iLogin = iLogin + 1;
        application.setAttribute("login", iLogin);        
    }
}

response.sendRedirect("index.jsp");
%>

logout_ok.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%
session.invalidate();
int iLogin = 0;
Object oLogin = application.getAttribute("login");
if(oLogin != null){
  iLogin = (int)oLogin;
}
iLogin = iLogin - 1;
application.setAttribute("login", iLogin);

response.sendRedirect("index.jsp");
%>