Search

Servlet & JSP 2์ผ์ฐจ @2024-10-11

ํ•™์Šต ๋…ธํŠธ ์š”์•ฝ

1. Servlet

โ€ข
์ •์˜: Servlet์€ Java๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›น ์„œ๋ฒ„์—์„œ ์‹คํ–‰๋˜๋Š” ์„œ๋ฒ„ ์ธก ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ, ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‘๋‹ต์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
โ€ข
์š”์ฒญ ์ฒ˜๋ฆฌ:
โ—ฆ
HttpServletRequest: ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์— ๋ณด๋‚ด๋Š” ์š”์ฒญ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. ์š”์ฒญ์˜ URL, ํŒŒ๋ผ๋ฏธํ„ฐ, ํ—ค๋” ๋“ฑ์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.
โ€ข
์‘๋‹ต ์ฒ˜๋ฆฌ:
โ—ฆ
HttpServletResponse: ์„œ๋ฒ„๊ฐ€ ํด๋ผ์ด์–ธํŠธ์— ๋ณด๋‚ด๋Š” ์‘๋‹ต ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. ์‘๋‹ต์˜ ์ƒํƒœ ์ฝ”๋“œ, ์ฝ˜ํ…์ธ  ํƒ€์ž…, ์‹ค์ œ ๋ฐ์ดํ„ฐ ๋“ฑ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โ€ข
ํŽ˜์ด์ง€ ์ด๋™ ๋ฐฉ์‹:
โ—ฆ
forward: ์„œ๋ฒ„ ๋‚ด์—์„œ ์š”์ฒญ์„ ๋‹ค๋ฅธ ์„œ๋ธ”๋ฆฟ์ด๋‚˜ JSP๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ํด๋ผ์ด์–ธํŠธ๋Š” URL์ด ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๊ณ , ์š”์ฒญ ๋ฐ ์‘๋‹ต ๊ฐ์ฒด๋ฅผ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค.
RequestDispatcher dispatcher = request.getRequestDispatcher("nextPage.jsp"); dispatcher.forward(request, response);
Java
๋ณต์‚ฌ
โ—ฆ
redirect: ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋‹ค๋ฅธ URL๋กœ ์ด๋™ํ•˜๋ผ๋Š” ๋ช…๋ น์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค. URL์ด ๋ณ€๊ฒฝ๋˜๋ฉฐ ์ƒˆ๋กœ์šด ์š”์ฒญ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
response.sendRedirect("nextPage.jsp");
Java
๋ณต์‚ฌ
โ€ข
ํŒŒ์ผ ์—…๋กœ๋“œ: Multipart/form-data ํ˜•์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ํŒŒ์ผ์„ ์„œ๋ฒ„์— ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋„๋ก ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
โ€ข
ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ: ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์— ์ €์žฅ๋œ ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋„๋ก ์‘๋‹ต์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
โ€ข
์ธ๋„ค์ผ ์ด๋ฏธ์ง€: ์›๋ณธ ์ด๋ฏธ์ง€๋ฅผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ ์šฉ์œผ๋กœ ์ถ•์†Œํ•˜์—ฌ ํ‘œ์‹œํ•˜๋Š” ์ด๋ฏธ์ง€์ž…๋‹ˆ๋‹ค.

2. JSP

โ€ข
์ •์˜: JSP (JavaServer Pages)๋Š” HTML๊ณผ Java ์ฝ”๋“œ๋ฅผ ๊ฒฐํ•ฉํ•˜์—ฌ ๋™์ ์ธ ์›น ํŽ˜์ด์ง€๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค.
โ€ข
JSP ํƒœ๊ทธ:
โ—ฆ
์Šคํฌ๋ฆฝํŠธ ํƒœ๊ทธ: Java ์ฝ”๋“œ๋ฅผ JSP ํŽ˜์ด์ง€์— ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค.
<% String message = "Hello, World!"; %>
Plain Text
๋ณต์‚ฌ
โ—ฆ
๋””๋ ‰ํ‹ฐ๋ธŒ ํƒœ๊ทธ: JSP ํŽ˜์ด์ง€์˜ ์„ค์ •์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Plain Text
๋ณต์‚ฌ
โ—ฆ
์•ก์…˜ ํƒœ๊ทธ: JSP ๋‚ด์—์„œ ๋‹ค๋ฅธ ์ž์›์„ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.
<jsp:include page="header.jsp" />
Plain Text
๋ณต์‚ฌ
โ€ข
๋‚ด์žฅ ๊ฐ์ฒด: JSP์—์„œ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ๊ฐ์ฒด๋กœ, ์š”์ฒญ ๋ฐ ์„ธ์…˜ ๊ด€๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ฃผ์š” ๋‚ด์žฅ ๊ฐ์ฒด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:
โ—ฆ
request: ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๊ฐ์ฒด.
โ—ฆ
response: ์„œ๋ฒ„๊ฐ€ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ตํ•˜๋Š” ๊ฐ์ฒด.
โ—ฆ
session: ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„์˜ ์„ธ์…˜ ์ •๋ณด๋ฅผ ์œ ์ง€ํ•˜๋Š” ๊ฐ์ฒด.
โ—ฆ
application: ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ „์—ญ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๊ฐ์ฒด.

์ดํ•ดํ•˜์ง€ ๋ชปํ•œ ๋‚ด์šฉ

1. ํŽ˜์ด์ง€ ์ด๋™ ๋ฐฉ์‹

โ€ข
Forward vs Redirect:
โ—ฆ
Forward๋Š” ์„œ๋ฒ„ ๋‚ด๋ถ€์—์„œ ์š”์ฒญ์„ ์ „๋‹ฌํ•˜๋ฏ€๋กœ URL์ด ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ฐ™์€ ์š”์ฒญ๊ณผ ์‘๋‹ต ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ๊ณต์œ ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
โ—ฆ
Redirect๋Š” ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋‹ค๋ฅธ URL๋กœ ์ด๋™ํ•˜๋ผ๋Š” ์ง€์‹œ๋ฅผ ํ•˜๋ฉฐ, ์ƒˆ๋กœ์šด ์š”์ฒญ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ URL์ด ๋ณ€๊ฒฝ๋˜๊ณ  ์ด์ „ ์š”์ฒญ์˜ ๋ฐ์ดํ„ฐ๋Š” ์ „๋‹ฌ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

2. ๋‚ด์žฅ ๊ฐ์ฒด

โ€ข
๋‚ด์žฅ ๊ฐ์ฒด๋Š” JSP ํŽ˜์ด์ง€์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ์ฒด๋“ค์ž…๋‹ˆ๋‹ค. ๊ฐ ๊ฐ์ฒด์˜ ์šฉ๋„๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:
๋‚ด์žฅ ๊ฐ์ฒด
์„ค๋ช…
request
ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ ์ •๋ณด
response
ํด๋ผ์ด์–ธํŠธ์— ๋Œ€ํ•œ ์‘๋‹ต ์ •๋ณด
session
์‚ฌ์šฉ์ž์˜ ์„ธ์…˜ ์ •๋ณด
application
์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ „์—ญ ์ •๋ณด

๋” ๊ถ๊ธˆํ•œ ๋‚ด์šฉ

ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ๋ฅผ ์—ฌ๋Ÿฌ ํŒŒ์ผ์„ ์••์ถ• ๋‹ค์šด๋กœ๋“œํ•˜๊ธฐ

์—ฌ๋Ÿฌ ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•  ๋•Œ, ํŒŒ์ผ์„ ZIP ํ˜•์‹์œผ๋กœ ์••์ถ•ํ•œ ํ›„ ํด๋ผ์ด์–ธํŠธ์— ์ „์†กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ์‹œ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:
import java.io.*; import java.util.zip.*; import javax.servlet.*; import javax.servlet.http.*; public class ZipDownloadServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // ํŒŒ์ผ ๋ชฉ๋ก String[] files = {"file1.txt", "file2.txt", "file3.txt"}; // ZIP ํŒŒ์ผ ์ด๋ฆ„ String zipFileName = "download.zip"; // ์‘๋‹ต ํ—ค๋” ์„ค์ • response.setContentType("application/zip"); response.setHeader("Content-Disposition", "attachment; filename=\\"" + zipFileName + "\\""); // ZIP ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ ์ƒ์„ฑ try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) { for (String file : files) { File inputFile = new File(file); try (FileInputStream fis = new FileInputStream(inputFile)) { // ZIP ํ•ญ๋ชฉ ์ถ”๊ฐ€ ZipEntry zipEntry = new ZipEntry(inputFile.getName()); zos.putNextEntry(zipEntry); // ํŒŒ์ผ ๋ฐ์ดํ„ฐ๋ฅผ ZIP์— ์“ฐ๊ธฐ byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) >= 0) { zos.write(buffer, 0, length); } zos.closeEntry(); } } } } }
Java
๋ณต์‚ฌ
์ด ์ฝ”๋“œ๋Š” ZipOutputStream์„ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ํŒŒ์ผ์„ ํ•˜๋‚˜์˜ ZIP ํŒŒ์ผ๋กœ ์••์ถ•ํ•œ ํ›„ ํด๋ผ์ด์–ธํŠธ์— ์ „์†กํ•ฉ๋‹ˆ๋‹ค.

์ „์ฒด ๋‚ด์šฉ ์ •๋ฆฌ ํ‘œ

์ฃผ์ œ
๋‚ด์šฉ
Servlet
์›น ์„œ๋ฒ„์—์„œ ์‹คํ–‰๋˜๋Š” ์„œ๋ฒ„ ์ธก ํ”„๋กœ๊ทธ๋žจ
์š”์ฒญ ์ฒ˜๋ฆฌ
HttpServletRequest - ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ ์ •๋ณด ๋‹ด๊ธฐ
์‘๋‹ต ์ฒ˜๋ฆฌ
HttpServletResponse - ์„œ๋ฒ„ ์‘๋‹ต ์ •๋ณด ๋‹ด๊ธฐ
ํŽ˜์ด์ง€ ์ด๋™ ๋ฐฉ์‹
forward - ๋‚ด๋ถ€ ์š”์ฒญ ์ „๋‹ฌ, redirect - ํด๋ผ์ด์–ธํŠธ ์ด๋™ ์ง€์‹œ
ํŒŒ์ผ ์—…๋กœ๋“œ
Multipart/form-data ํ˜•์‹ ์‚ฌ์šฉ
ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ
์„œ๋ฒ„์˜ ํŒŒ์ผ์„ ํด๋ผ์ด์–ธํŠธ์— ๋‹ค์šด๋กœ๋“œ
์ธ๋„ค์ผ ์ด๋ฏธ์ง€
์›๋ณธ ์ด๋ฏธ์ง€๋ฅผ ์ถ•์†Œํ•˜์—ฌ ๋ฏธ๋ฆฌ๋ณด๊ธฐ ์šฉ์œผ๋กœ ํ‘œ์‹œ
JSP
HTML๊ณผ Java ๊ฒฐํ•ฉํ•˜์—ฌ ๋™์  ์›น ํŽ˜์ด์ง€ ์ƒ์„ฑ
JSP ํƒœ๊ทธ
์Šคํฌ๋ฆฝํŠธ, ๋””๋ ‰ํ‹ฐ๋ธŒ, ์•ก์…˜ ํƒœ๊ทธ
๋‚ด์žฅ ๊ฐ์ฒด
์ž๋™ ์ƒ์„ฑ๋˜๋Š” ์š”์ฒญ ๋ฐ ์„ธ์…˜ ๊ด€๋ จ ๊ฐ์ฒด
์ดํ•ดํ•˜์ง€ ๋ชปํ•œ ๋‚ด์šฉ
ํŽ˜์ด์ง€ ์ด๋™ ๋ฐฉ์‹ ์„ค๋ช…, ๋‚ด์žฅ ๊ฐ์ฒด ์šฉ๋„ ์„ค๋ช…
๋” ๊ถ๊ธˆํ•œ ๋‚ด์šฉ
ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ๋ฅผ ์—ฌ๋Ÿฌ ํŒŒ์ผ์„ ZIP์œผ๋กœ ์••์ถ•ํ•˜๋Š” ๋ฐฉ๋ฒ•
์ด ํ•™์Šต ๋…ธํŠธ๊ฐ€ ๋„์›€์ด ๋˜๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค! ์ถ”๊ฐ€ ์งˆ๋ฌธ์ด ์žˆ๋‹ค๋ฉด ์–ธ์ œ๋“ ์ง€ ๋ง์”€ํ•ด ์ฃผ์„ธ์š”.