JSP 내장 객체 (JSP Implicit Objects)
request
request 객체는 HttpServletRequest의 인스턴스로, 클라이언트의 요청 정보를 포함합니다. 요청 파라미터, 헤더, 세션 정보를 가져올 때 사용됩니다.
예제:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Request Example</title>
</head>
<body>
<%
String name = request.getParameter("name");
if (name == null) {
name = "Guest";
}
%>
<h1>Hello, <%= name %>!</h1>
</body>
</html>
위 예제는 요청 파라미터 name을 가져와서 인사말을 출력합니다.
response
response 객체는 HttpServletResponse의 인스턴스로, 서버에서 클라이언트로의 응답을 관리합니다. 헤더 설정, 리다이렉션 등을 할 수 있습니다.
예제:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Response Example</title>
</head>
<body>
<%
response.setContentType("text/html");
response.setHeader("Custom-Header", "HeaderValue");
%>
<h1>Response Header Set!</h1>
</body>
</html>
위 예제는 응답의 Content-Type과 Custom-Header를 설정합니다.
out
out 객체는 JspWriter의 인스턴스로, 클라이언트에게 출력을 보냅니다. JSP 페이지에서 텍스트를 출력할 때 사용됩니다.
예제:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Out Example</title>
</head>
<body>
<%
out.println("<h1>This is printed using 'out' object</h1>");
%>
</body>
</html>
위 예제는 out 객체를 사용하여 HTML 콘텐츠를 출력합니다.
session
session 객체는 HttpSession의 인스턴스로, 사용자 세션 정보를 관리합니다. 사용자별 데이터를 저장하고 공유할 때 사용됩니다.
예제:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Session Example</title>
</head>
<body>
<%
String username = (String) session.getAttribute("username");
if (username == null) {
username = "Guest";
session.setAttribute("username", username);
}
%>
<h1>Welcome, <%= username %>!</h1>
</body>
</html>
위 예제는 세션에서 사용자 이름을 가져오고, 없으면 기본값을 설정합니다.
application
application 객체는 ServletContext의 인스턴스로, 애플리케이션 범위에서 데이터를 저장하고 공유할 때 사용됩니다.
예제:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Application Example</title>
</head>
<body>
<%
Integer visitCount = (Integer) application.getAttribute("visitCount");
if (visitCount == null) {
visitCount = 1;
} else {
visitCount += 1;
}
application.setAttribute("visitCount", visitCount);
%>
<h1>Visit Count: <%= visitCount %></h1>
</body>
</html>
위 예제는 애플리케이션 범위에서 방문 횟수를 저장하고 증가시킵니다.
config
config 객체는 ServletConfig의 인스턴스로, 서블릿의 초기화 파라미터와 서블릿 설정 정보를 포함합니다.
예제:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Config Example</title>
</head>
<body>
<%
String servletName = config.getServletName();
%>
<h1>Servlet Name: <%= servletName %></h1>
</body>
</html>
위 예제는 config 객체를 사용하여 서블릿의 이름을 출력합니다.
pageContext
pageContext 객체는 PageContext의 인스턴스로, JSP 페이지의 컨텍스트 정보를 제공합니다. 페이지 범위에서 속성을 저장하고 관리할 수 있습니다.
예제:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>PageContext Example</title>
</head>
<body>
<%
pageContext.setAttribute("message", "Hello from PageContext", PageContext.PAGE_SCOPE);
%>
<h1><%= pageContext.getAttribute("message") %></h1>
</body>
</html>
위 예제는 pageContext 객체를 사용하여 페이지 범위에서 메시지를 설정하고 가져옵니다.
page
page 객체는 현재 JSP 페이지의 인스턴스로, this 키워드와 동일합니다. 주로 잘 사용되지 않지만, 현재 JSP의 참조를 필요로 할 때 사용됩니다.
예제:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Page Example</title>
</head>
<body>
<%
String className = page.getClass().getName();
%>
<h1>Page Class: <%= className %></h1>
</body>
</html>
위 예제는 page 객체를 사용하여 현재 JSP 페이지의 클래스 이름을 출력합니다.
exception
exception 객체는 Throwable의 인스턴스로, 페이지가 오류 페이지로 선언된 경우 예외 객체를 참조할 수 있습니다. 예외 페이지에서 발생한 오류 정보를 제공합니다.
예제:
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
<html>
<head>
<title>Exception Example</title>
</head>
<body>
<h1>An error occurred:</h1>
<p><%= exception.getMessage() %></p>
</body>
</html>
위 예제는 exception 객체를 사용하여 발생한 오류 메시지를 출력합니다. 해당 페이지는 오류 페이지로 선언되어 있습니다.
종합 예제 (Comprehensive Example)
모든 내장 객체를 사용하는 종합 예제입니다.
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*, java.text.*" errorPage="error.jsp" %>
<html>
<head>
<title>Comprehensive JSP Example</title>
</head>
<body>
<h1>Comprehensive JSP Example</h1>
<h2>Request</h2>
<p>Client IP: <%= request.getRemoteAddr() %></p>
<h2>Response</h2>
<%
response.setContentType("text/html");
%>
<p>Response Content Type set to text/html</p>
<h2>Out</h2>
<%
out.println("<p>Printed using out object</p>");
%>
<h2>Session</h2>
<%
String sessionUser = (String) session.getAttribute("user");
if (sessionUser == null) {
sessionUser = "Guest";
session.setAttribute("user", sessionUser);
}
%>
<p>Session User: <%= sessionUser %></p>
<h2>Application</h2>
<%
Integer appVisitCount = (Integer) application.getAttribute("visitCount");
if (appVisitCount == null) {
appVisitCount = 1;
} else {
appVisitCount += 1;
}
application.setAttribute("visitCount", appVisitCount);
%>
<p>Application Visit Count: <%= appVisitCount %></p>
<h2>Config</h2>
<p>Servlet Name: <%= config.getServletName() %></p>
<h2>PageContext</h2>
<%
pageContext.setAttribute("contextMessage", "Hello from PageContext", PageContext.PAGE_SCOPE);
%>
<p>PageContext Message: <%= pageContext.getAttribute("contextMessage") %></p>
<h2>Page</h2>
<%
String pageClassName = page.getClass().getName();
%>
<p>Page Class: <%= pageClassName %></p>
</body>
</html>
이 종합 예제는 모든 JSP 내장 객체를 사용하여 다양한 정보를 출력하고 설정하는 방법을 보여줍니다. 이를 통해 JSP 페이지에서 내장 객체를 활용하는 방법을 이해할 수 있습니다.
