
2025๋ 4์ 3์ผ
JSP์๋ ์๋์ผ๋ก ์ฌ์ฉํ ์ ์๋ 9๊ฐ์ ๋ด์ฅ ๊ฐ์ฒด๊ฐ ์๋ค.
๊ทธ์ค์์ scope(์ ํจ ๋ฒ์) ๋ฅผ ๊ฐ์ง๋ ๋ํ ๊ฐ์ฒด๋ ๋ค์๊ณผ ๊ฐ๋ค:
| ๊ฐ์ฒด๋ช | ์ค๋ช | ์๋ช ์ฃผ๊ธฐ |
|---|---|---|
pageContext | ํ์ฌ ํ์ด์ง์์๋ง ์ ํจ | ํ์ด์ง ๋จ์ |
request | ํ๋์ ์์ฒญ(request) ๋์ | ์์ฒญ ๋จ์ |
session | ๋ธ๋ผ์ฐ์ ์ธ์ ๋์ | ์ฌ์ฉ์ ๋จ์ |
application | ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ์ฒด | ์๋ฒ ์ ์ญ |
๐ฝ Scope๋ ๋ค์๊ณผ ๊ฐ์ ๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง:
application > session > request > pageContext
์์ Scope๋ ํ์ Scope์ ์ ์ฅ๋ ๊ฐ์ ์ฐธ์กฐ ๊ฐ๋ฅ
pageContext ๋ด์ฅ ๊ฐ์ฒด ํ์ฉ ์์ <%
System.out.println("pageContext : " + pageContext);
System.out.println("Request : " + pageContext.getRequest());
System.out.println("Session : " + pageContext.getSession());
System.out.println("Application : " + pageContext.getServletContext());
%>
ํ๋ก์ ํธ ๊ฒฝ๋ก: <%= pageContext.getServletContext().getContextPath() %><br/>
EL ํํ์ ์ฌ์ฉ: ${ pageContext.request.contextPath }
pageContext๋ ๋จ์ํ scope ์ ๊ทผ ์ธ์๋
include(),forward(),pushBody()๋ฑ JSP์ ํ๋ฆ์ ์ ์ดํ ์ ์๋ ๊ฐ๋ ฅํ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
<%
String protocol = request.getProtocol();
String serverName = request.getServerName();
String remoteAddr = request.getRemoteAddr();
String method = request.getMethod();
String userAgent = request.getHeader("User-Agent");
%>
ํ๋กํ ์ฝ: <%= protocol %><br/>
์๋ฒ๋ช
: <%= serverName %><br/>
์์ฒญ ๋ฐฉ์: <%= method %><br/>
์ ์ IP: <%= remoteAddr %><br/>
๋ธ๋ผ์ฐ์ ์ ๋ณด: <%= userAgent %>
request๋ ํด๋ผ์ด์ธํธ์ ์์ฒญ ๋ฉํ ์ ๋ณด๋ฅผ ์์งํ ์ ์๋ ์ฃผ์ ๊ฐ์ฒด๋ค.
<%
// ๋ฆฌ๋ค์ด๋ ํธ
// response.sendRedirect("some.jsp");
// ์๋ฌ ์๋ต
// response.sendError(HttpServletResponse.SC_NOT_FOUND, "ํ์ด์ง๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค");
// ์๋ก๊ณ ์นจ
// response.setIntHeader("Refresh", 3);
// HTML ์ง์ ์ถ๋ ฅ
PrintWriter out = response.getWriter();
out.write("<h1>Hello, World!</h1>");
%>
response๋ ํด๋ผ์ด์ธํธ๋ก ์๋ต์ ์ง์ ์ ์ดํ ์ ์๋ ๊ฐ๋ ฅํ ๋๊ตฌ
<%
String dirPath = pageContext.getServletContext().getRealPath("/") + "C05\\files\\";
FileInputStream in = new FileInputStream(dirPath + "TEST.txt");
ServletOutputStream bout = response.getOutputStream();
response.setHeader("Content-Type", "application/octet-stream;charset=utf-8");
response.setHeader("Content-Disposition", "attachment; filename=router.zip");
byte[] buffer = new byte[4096];
int data;
while ((data = in.read(buffer)) != -1) {
bout.write(buffer, 0, data);
}
bout.flush(); bout.close(); in.close();
%>
Content-Type: application/octet-stream์
๋ธ๋ผ์ฐ์ ์๊ฒ "์ด๊ฑด ๋ฐ์ด๋๋ฆฌ ํ์ผ์ด์ผ, ์ง์ ์ด์ง ๋ง๊ณ ๋ค์ด๋ก๋ ํด์ค" ๋ผ๊ณ ์๋ ค์ฃผ๋ ์ญํ ์ ํ๋ค.
<%
String dirPath = pageContext.getServletContext().getRealPath("/") + "C05\\files\\";
File[] fileList = new File(dirPath).listFiles();
ServletOutputStream bout = response.getOutputStream();
ZipOutputStream zout = new ZipOutputStream(bout);
response.setHeader("Content-Type", "application/octet-stream;charset=utf-8");
response.setHeader("Content-Disposition", "attachment; filename=ALLFILES.zip");
for (File file : fileList) {
FileInputStream in = new FileInputStream(file);
ZipEntry entry = new ZipEntry(file.getName());
zout.putNextEntry(entry);
// ํ์ผ ๋ด์ฉ์ zip์ ๊ธฐ๋ก
int d;
while ((d = in.read()) != -1) {
zout.write(d);
}
zout.closeEntry(); in.close(); // ๊ฐ ํ์ผ entry ๋ซ๊ธฐ
}
zout.flush(); zout.close(); bout.close();
%>
๊ฐ ํ์ผ๋ง๋ค ZipEntry๋ฅผ ์์ฑํ ๋ค,
๋ฐ์ดํฐ๋ฅผ ๊ธฐ๋กํ๊ณ
closeEntry()๋ก ๋ซ๋ ๊ณผ์ ์ ๋ฐ๋ณตํ๋ฉฐ ZIP ์์ถ์ ์์ฑํ๋ค.
<a href="04Download_SingleFile.jsp">๋จ์ผํ์ผ ๋ค์ด๋ก๋</a>
<hr/>
<a href="05Download_zip.jsp">ZIP ๋ค์ด๋ก๋</a>
์ฌ์ฉ์์๊ฒ ๋ค์ด๋ก๋ ์ ํ UI ์ ๊ณต
pageContext : org.apache.jasper.runtime.PageContextImpl@2a3bc9
Request : org.apache.catalina.connector.RequestFacade@4a1dc
Session : org.apache.catalina.session.StandardSessionFacade@4a8fd
Application : org.apache.catalina.core.ApplicationContextFacade@7a3dc
dirPath : C:\workspace\project\C05\files\
JSP ๋ด์ฅ ๊ฐ์ฒด๋ค์ ๊ฐ๊ธฐ ๋ค๋ฅธ ๋ฒ์(Scope) ๋ฅผ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ,
request/response ๊ฐ์ฒด๋ฅผ ํตํด ์๋ฒ์ ํด๋ผ์ด์ธํธ ๊ฐ ํต์ ํ๋ฆ์ ์ ์ดํ ์ ์๋ค.
ํนํ pageContext๋ ๋ชจ๋ ๋ฒ์๋ฅผ ๊ดํตํ ์ ์๋ ๋งค์ฐ ๊ฐ๋ ฅํ ๋๊ตฌ์ด๋ฉฐ,
JSP์ ํ๋ฆ๊น์ง ์ ์ดํ ์ ์๋ค๋ ์ฌ์ค์ ๋งค์ฐ ์ธ์ ๊น์๋ค.
๋ํ ํ์ผ ๋ค์ด๋ก๋ ๊ธฐ๋ฅ์ ๋จ์ผ๊ณผ ์์ถ ๋ชจ๋ ์ค์ ์๋น์ค์์ ๊ผญ ํ์ํ ๊ธฐ๋ฅ์ด๋ผ๋ ์ ์์ ์ค์ต์ด ๋งค์ฐ ์ ์ตํ๋ค.
pageContext๋ฅผ ํตํด ๋ชจ๋ ๊ฐ์ฒด ์ ๊ทผ ๋ฐ ํ๋ฆ ์ ์ด ๊ฐ๋ฅrequest๋ ํด๋ผ์ด์ธํธ ์์ฒญ ์ ๋ณด๋ฅผ, response๋ ์๋ต์ ์ ์ดํ๋ ๋ฐ ์ฌ์ฉ๋๋คresponse์ OutputStream๊ณผ ์ ์ ํ ํค๋ ์ค์ ์ผ๋ก ๊ฐ๋ฅZipOutputStream์ ์ฌ์ฉํ์ฌ ์ฌ๋ฌ ํ์ผ์ ํ ๋ฒ์ ์ ์กํ ์ ์๋ค