Search

JSP ๋‚ด์žฅ๊ฐ์ฒด

JSP ๋‚ด์žฅ๊ฐ์ฒด

๋‚ด์žฅ ๊ฐ์ฒด
์„ค๋ช…
request
HTTP ์š”์ฒญ๊ณผ ๊ด€๋ จ๋œ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ณ  ์ ‘๊ทผํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
response
ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ HTTP ์‘๋‹ต์„ ์ƒ์„ฑํ•˜๊ณ  ๋ณด๋‚ด๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
session
ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„์˜ ์„ธ์…˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
application
์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ „์—ญ์—์„œ ๊ณต์œ ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
out
ํด๋ผ์ด์–ธํŠธ๋กœ ์ถœ๋ ฅ์„ ๋ณด๋‚ด๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
config
ํ˜„์žฌ JSP ํŽ˜์ด์ง€์— ๋Œ€ํ•œ ์„ค์ • ์ •๋ณด์— ์ ‘๊ทผํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
pageContext
ํ˜„์žฌ JSP ํŽ˜์ด์ง€์— ๋Œ€ํ•œ ์ •๋ณด์™€ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.
page
ํ˜„์žฌ JSP ํŽ˜์ด์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด๋กœ, JSP ํŽ˜์ด์ง€ ์ž์ฒด๋ฅผ ์ฐธ์กฐํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
exception
JSP ํŽ˜์ด์ง€์—์„œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

๋‚ด์žฅ ๊ฐ์ฒด์˜ ์˜์—ญ (Scope)

๋‚ด์žฅ ๊ฐ์ฒด
์—ญํ• 
application
์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ „์ฒด์—์„œ ๊ณต์œ ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ์‚ฌ์šฉ์ž๊ฐ€ ๊ณต์œ ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ฑฐ๋‚˜, ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ˆ˜์ค€์˜ ์„ค์ • ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
session
์„ธ์…˜ ๋‹น ํ•˜๋‚˜์”ฉ ์ƒ์„ฑ๋˜๋ฉฐ, ์‚ฌ์šฉ์ž ๊ฐ„์— ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต์œ ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋กœ๊ทธ์ธ ์ •๋ณด๋‚˜ ์‚ฌ์šฉ์ž ์„ค์ •๊ณผ ๊ฐ™์€ ์‚ฌ์šฉ์ž ๊ด€๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐ ์ฃผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
request
ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ๋“ค์–ด์˜จ HTTP ์š”์ฒญ ์ •๋ณด๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ํด๋ผ์ด์–ธํŠธ ๊ฐ„์— ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต์œ ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
page
ํ˜„์žฌ JSP ํŽ˜์ด์ง€์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ํŽ˜์ด์ง€ ๋‚ด์—์„œ๋งŒ ์œ ํšจํ•˜๋ฉฐ, ์ฃผ๋กœ ์Šคํฌ๋ฆฝํŠธ ์š”์†Œ์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

appolication ์˜์—ญ

โ€ข
๊ธฐ๋Šฅ
โ—ฆ
์บ์‹ฑ
โ—ฆ
์ฑ„ํŒ…

session ์˜์—ญ

โ€ข
๊ธฐ๋Šฅ
โ—ฆ
๋กœ๊ทธ์ธ (์‚ฌ์šฉ์ž ์ธ์ฆ)
โ—ฆ
์‚ฌ์šฉ์ž ๋ฐ์ดํ„ฐ ์ €์žฅ
โ–ช
์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ •๋ณด
โ–ช
์ปค์Šคํ…€ ์„ค์ • ์ •๋ณด
โ—ฆ
์‚ฌ์šฉ์ž ๋ถ„์„
โ–ช
์กฐํšŒ์ˆ˜
โ–ช
๋ฐฉ๋ฌธ์ˆ˜

request ์˜์—ญ

โ€ข
๊ธฐ๋Šฅ
โ—ฆ
์š”์ฒญ ์ •๋ณด ์ „๋‹ฌ
โ—ฆ
์š”์ฒญ ์ฒ˜๋ฆฌ ํ›„ ํŽ˜์ด์ง€ ์ด๋™

page ์˜์—ญ

์ฃผ์š” ๋‚ด์žฅ๊ฐ์ฒด

โ€ข
request
โ€ข
response
โ€ข
out

request ๋‚ด์žฅ ๊ฐ์ฒด

๋ฉ”์„œ๋“œ/์†์„ฑ
์„ค๋ช…
getParameter(String name)
์ง€์ •๋œ ์ด๋ฆ„์˜ HTTP ์š”์ฒญ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
getParameterValues(String name)
๋™์ผํ•œ ์ด๋ฆ„์˜ ์—ฌ๋Ÿฌ HTTP ์š”์ฒญ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ’์„ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
getParameterMap()
๋ชจ๋“  HTTP ์š”์ฒญ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋งต ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
setAttribute(String name, Object value)
์ง€์ •๋œ ์ด๋ฆ„์œผ๋กœ ์†์„ฑ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
getAttribute(String name)
์ง€์ •๋œ ์ด๋ฆ„์˜ ์†์„ฑ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
removeAttribute(String name)
์ง€์ •๋œ ์ด๋ฆ„์˜ ์†์„ฑ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
getMethod()
ํ˜„์žฌ ์š”์ฒญ์˜ HTTP ๋ฉ”์„œ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค (์˜ˆ: GET, POST ๋“ฑ).
getRequestURI()
ํ˜„์žฌ ์š”์ฒญ์˜ URI(Uniform Resource Identifier)๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
getRequestURL()
ํ˜„์žฌ ์š”์ฒญ์˜ URL(Uniform Resource Locator)์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
getSession()
ํ˜„์žฌ ์š”์ฒญ๊ณผ ๊ด€๋ จ๋œ ์„ธ์…˜ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
getServletContext()
ํ˜„์žฌ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ServletContext ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
getHeader(String name)
์ง€์ •๋œ ์ด๋ฆ„์˜ HTTP ํ—ค๋” ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
getHeaders(String name)
์ง€์ •๋œ ์ด๋ฆ„์˜ HTTP ํ—ค๋” ๊ฐ’์„ ๋ชจ๋‘ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
getHeaderNames()
๋ชจ๋“  HTTP ํ—ค๋” ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
getCookies()
ํ˜„์žฌ ์š”์ฒญ์— ํฌํ•จ๋œ ๋ชจ๋“  ์ฟ ํ‚ค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ

โ€ข
request.getParameter(โ€ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„โ€)
<% request.setCharacterEncoding("UTF-8"); String id = request.getParameter("id"); String pw = request.getParameter("pw"); %>
Java
๋ณต์‚ฌ

์—ฌ๋Ÿฌ ๊ฐ’์„ ๊ฐ€์ง„ ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ

โ€ข
request.getParameterValues(โ€ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„โ€)
<% // request.getParameterValues() // : 2 ๊ฐœ์ด์ƒ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ String[] hobby = request.getParameterValues("hobby"); %>
Java
๋ณต์‚ฌ

๋ชจ๋“  ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ ์ „๋‹ฌ ๋ฐ›๊ธฐ

<% request.setCharacterEncoding("UTF-8"); Enumeration params = request.getParameterNames(); while( params.hasMoreElements() ) { String name = (String) params.nextElement(); out.print(name + " : "); String[] values = request.getParameterValues(name); for( int i = 0 ; i < values.length ; i++ ) { out.print(values[i] + " "); } } %>
Java
๋ณต์‚ฌ

response ๋‚ด์žฅ ๊ฐ์ฒด

๋ฉ”์„œ๋“œ/์†์„ฑ
์„ค๋ช…
setCharacterEncoding(String charset)
์‘๋‹ต์˜ ๋ฌธ์ž ์ธ์ฝ”๋”ฉ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
getWriter()
ํด๋ผ์ด์–ธํŠธ๋กœ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•œ PrintWriter ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
getOutputStream()
ํด๋ผ์ด์–ธํŠธ๋กœ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•œ ServletOutputStream ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
setContentType(String type)
์‘๋‹ต์˜ ์ปจํ…์ธ  ํƒ€์ž… (MIME ํƒ€์ž…)์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
setContentLength(int len)
์‘๋‹ต์˜ ์ปจํ…์ธ  ๊ธธ์ด๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
addHeader(String name, String value)
HTTP ์‘๋‹ต ํ—ค๋”๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
sendRedirect(String location)
ํด๋ผ์ด์–ธํŠธ๋ฅผ ์ง€์ •๋œ URL๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธํ•ฉ๋‹ˆ๋‹ค.
setStatus(int sc)
HTTP ์‘๋‹ต ์ƒํƒœ ์ฝ”๋“œ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
sendError(int sc, String msg)
HTTP ์˜ค๋ฅ˜ ์‘๋‹ต์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค.
setHeader(String name, String value)
์ง€์ •๋œ ์ด๋ฆ„์˜ HTTP ์‘๋‹ต ํ—ค๋”๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
getContentType()
ํ˜„์žฌ ์‘๋‹ต์˜ ์ปจํ…์ธ  ํƒ€์ž… (MIME ํƒ€์ž…)์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
getCharacterEncoding()
ํ˜„์žฌ ์‘๋‹ต์˜ ๋ฌธ์ž ์ธ์ฝ”๋”ฉ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

ํŽ˜์ด์ง€ ์ด๋™ - ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ redirect

<% request.setCharacterEncoding("UTF-8"); String id = request.getParameter("id"); String pw = request.getParameter("pw"); if( id.equals("aloha") && pw.equals("123456") ) { // ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ๋ฐฉ์‹์œผ๋กœ ํŽ˜์ด์ง€ ์ด๋™ response.sendRedirect("success.jsp"); } else { response.sendRedirect("failed.jsp"); } %>
Java
๋ณต์‚ฌ

์ปจํ…์ธ  ํƒ€์ž… ์ง€์ •ํ•˜๊ธฐ

โ€ข
์ด๋ฏธ์ง€ ์‘๋‹ตํ•˜๊ธฐ (์ธ๋„ค์ผ)
โ€ข
ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ

์ด๋ฏธ์ง€ ์‘๋‹ตํ•˜๊ธฐ

โ€ข
java
โ—ฆ
file
โ–ช
ImgServlet.java
โ€ข
webapp
โ—ฆ
file
โ–ช
img.jsp

ImgServlet.java

package file; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.net.URL; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/img") public class ImgServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String fileName = request.getParameter("fileName"); // ์ƒํ’ˆ ์ด๋ฏธ์ง€๊ฐ€ ์—†์„ ๋•Œ, ๊ธฐ๋ณธ ์ด๋ฏธ์ง€๋กœ ์‘๋‹ต if( fileName == null || fileName.equals("") ) fileName = "no-image.png"; // ํ˜„์žฌ ํด๋ž˜์Šค์˜ ํด๋ž˜์Šค ๋กœ๋”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ์†Œ์Šค URL ๊ฐ€์ ธ์˜ค๊ธฐ String className = getClass().getName().substring( getClass().getName().lastIndexOf(".") + 1 ); URL resourceUrl = getClass().getResource(className+".class"); System.out.println("class : " + className); String root = ""; if (resourceUrl != null) { String path = resourceUrl.getFile(); int lastIndex = path.lastIndexOf("WEB-INF"); root = path.substring(0, lastIndex); System.out.println("ํŒŒ์ผ ์œ„์น˜: " + resourceUrl.getFile()); System.out.println("ํ”„๋กœ์ ํŠธ ์œ„์น˜: " + root); } else { System.err.println("๋ฆฌ์†Œ์Šค๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."); } System.out.println("/img ์š”์ฒญ..."); String filePath = root + "/static/img/" + fileName; // ํŒŒ์ผ ๊ฒฝ๋กœ String ext = filePath.substring( filePath.lastIndexOf(".") + 1 ).toUpperCase(); System.out.println("ํ™•์žฅ์ž : " + ext); String mediaType = "image/jpeg"; switch (ext) { case "JPG": case "JPEG": mediaType = "image/jpeg"; break; case "PNG" : mediaType = "image/png"; break; case "GIF" : mediaType = "image/gif"; break; case "WEBP" : mediaType = "image/webp"; break; } response.setContentType( mediaType ); // ์ด๋ฏธ์ง€ ํŒŒ์ผ๋กœ ์ปจํ…์ธ  ํƒ€์ž… ์ง€์ • try (FileInputStream fis = new FileInputStream(filePath); OutputStream out = response.getOutputStream()) { byte[] buffer = new byte[1024]; int data; while ((data = fis.read(buffer)) != -1) { out.write(buffer, 0, data); } } catch (IOException e) { e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
Java
๋ณต์‚ฌ

img.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>์ด๋ฏธ์ง€ ์‘๋‹ต๋ฐ›๊ธฐ - ContentType : image/xxx</title> </head> <body> <!-- ํŒŒ์ผ๋ช…์ด ์—†์„ ๋•Œ ๊ธฐ๋ณธ ์ด๋ฏธ์ง€ --> <h1>๊ธฐ๋ณธ ์ด๋ฏธ์ง€</h1> <img src="/Test/img" /> <hr> <!-- ํŒŒ์ผ๋ช…์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ง€์ •ํ•ด์„œ ์ด๋ฏธ์ง€ ๊ฐ€์ ธ์˜ค๊ธฐ --> <h1>์ง€์ • ์ด๋ฏธ์ง€</h1> <img src="/Test/img?fileName=sample.png" /> </body> </html>
HTML
๋ณต์‚ฌ

๊ฒฐ๊ณผ ํ™”๋ฉด

ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ

โ€ข
java
โ—ฆ
file
โ–ช
FileServlet.java
โ€ข
webapp
โ—ฆ
file
โ–ช
download.jsp

FileServlet.java

package file; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.net.URL; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/file") public class FileServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String fileName = request.getParameter("fileName"); // ๋‹ค์šด๋กœ๋“œํ•  ํŒŒ์ผ์ด ์—†์„ ๋•Œ if( fileName == null || fileName.equals("") ) { return; } // ํ˜„์žฌ ํด๋ž˜์Šค์˜ ํด๋ž˜์Šค ๋กœ๋”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ์†Œ์Šค URL ๊ฐ€์ ธ์˜ค๊ธฐ String className = getClass().getName().substring( getClass().getName().lastIndexOf(".") + 1 ); URL resourceUrl = getClass().getResource(className+".class"); System.out.println("class : " + className); String root = ""; if (resourceUrl != null) { String path = resourceUrl.getFile(); int lastIndex = path.lastIndexOf("WEB-INF"); root = path.substring(0, lastIndex); System.out.println("ํŒŒ์ผ ์œ„์น˜: " + resourceUrl.getFile()); System.out.println("ํ”„๋กœ์ ํŠธ ์œ„์น˜: " + root); } else { System.err.println("๋ฆฌ์†Œ์Šค๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."); } System.out.println("/file ์š”์ฒญ..."); String filePath = root + "file/" + fileName; // ํŒŒ์ผ ๊ฒฝ๋กœ System.out.println("filePath : " + filePath); String ext = filePath.substring( filePath.lastIndexOf(".") + 1 ).toUpperCase(); System.out.println("ํ™•์žฅ์ž : " + ext); // ์ปจํ…์ธ  ํƒ€์ž…(Content-Type) ํ—ค๋” ์ง€์ • String mediaType = "application/octet-stream"; response.setContentType( mediaType ); // ๋‹ค์šด๋กœ๋“œ ํŒŒ์ผ๋ช… ์ง€์ • - Content-Disposition ํ—ค๋” ์„ค์ • response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); try (FileInputStream fis = new FileInputStream(filePath); OutputStream out = response.getOutputStream()) { byte[] buffer = new byte[1024]; int data; while ((data = fis.read(buffer)) != -1) { out.write(buffer, 0, data); } } catch (IOException e) { e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
Java
๋ณต์‚ฌ

download.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ- ContentType : application/octet-stream</title> </head> <body> <!-- ์ด๋ฏธ์ง€ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ --> <h1>sample.png ๋‹ค์šด๋กœ๋“œ</h1> <a href="/Test/file?fileName=sample.png">๋‹ค์šด๋กœ๋“œ</a> <hr> <!-- ํ…์ŠคํŠธ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ --> <h1>sample.txt ๋‹ค์šด๋กœ๋“œ</h1> <a href="/Test/file?fileName=sample.txt">๋‹ค์šด๋กœ๋“œ</a> </body> </html>
HTML
๋ณต์‚ฌ

๊ฒฐ๊ณผ ํ™”๋ฉด

out ๋‚ด์žฅ ๊ฐ์ฒด

๋ฉ”์„œ๋“œ
์„ค๋ช…
void clear()
๋ฒ„ํผ๋ฅผ ์ง€์šฐ๊ณ  ์ดˆ๊ธฐ ์ƒํƒœ๋กœ ๋Œ๋ฆฝ๋‹ˆ๋‹ค.
void clearBuffer()
์ถœ๋ ฅ ๋ฒ„ํผ๋ฅผ ๋น„์›๋‹ˆ๋‹ค.
void close()
์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์„ ๋‹ซ์Šต๋‹ˆ๋‹ค.
int getBufferSize()
์ถœ๋ ฅ ๋ฒ„ํผ์˜ ํฌ๊ธฐ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
boolean isAutoFlush()
์ž๋™ ํ”Œ๋Ÿฌ์‹œ ๋ชจ๋“œ๊ฐ€ ํ™œ์„ฑํ™”๋˜์–ด ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
void flush()
์ถœ๋ ฅ ๋ฒ„ํผ๋ฅผ ๋น„์šฐ๊ณ  ๋‚ด์šฉ์„ ํด๋ผ์ด์–ธํŠธ๋กœ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.
void print(boolean b)
boolean ๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
void print(char c)
๋ฌธ์ž(char)๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
void print(char[] s)
๋ฌธ์ž ๋ฐฐ์—ด(char[])์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
void print(double d)
double ๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
void print(float f)
float ๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
void print(int i)
int ๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
void print(long l)
long ๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
void print(Object obj)
๊ฐ์ฒด(Object)๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
void print(String s)
๋ฌธ์ž์—ด(String)์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
void println()
๊ฐœํ–‰ ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ ์ค„์„ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.
void println(boolean b)
boolean ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ณ  ์ค„์„ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.
void println(char c)
๋ฌธ์ž(char)๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์ค„์„ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.
void println(char[] s)
๋ฌธ์ž ๋ฐฐ์—ด(char[])์„ ์ถœ๋ ฅํ•˜๊ณ  ์ค„์„ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.
void println(double d)
double ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ณ  ์ค„์„ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.
void println(float f)
float ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ณ  ์ค„์„ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.
void println(int i)
int ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ณ  ์ค„์„ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.
void println(long l)
long ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ณ  ์ค„์„ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.
void println(Object obj)
๊ฐ์ฒด(Object)๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์ค„์„ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.
void println(String s)
๋ฌธ์ž์—ด(String)์„ ์ถœ๋ ฅํ•˜๊ณ  ์ค„์„ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.
<% out.print("์ด๋ฆ„ : "); out.println("๊น€ํ•˜๋‚˜"); out.println("๋‚˜์ด : 20"); %>
Java
๋ณต์‚ฌ

์บ์‹œ ์„ค์ •ํ•˜๊ธฐ

(์ด์–ด์„œ ์ž‘์„ฑ)