๐Ÿ’ป์›น(Web)

JSP(Jakarta Server Pages)

stonesy 2024. 12. 1. 22:59
728x90

๐Ÿ“ŒJSP(Jakarta Server Pages, ์ด์ „๋ช…์นญ: Java Server Pages)

JSP๋Š” HTML ๋‚ด์— Java ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•˜์—ฌ ์›น ์„œ๋ฒ„์—์„œ ๋™์ ์œผ๋กœ ์›น ํŽ˜์ด์ง€๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์›น ๋ธŒ๋ผ์šฐ์ €์— ๋Œ๋ ค์ฃผ๋Š” ์„œ๋ฒ„ ์‚ฌ์ด๋“œ ์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด์ด๋‹ค. Java EE ์ŠคํŽ™ ์ค‘ ์ผ๋ถ€๋กœ WAS์—์„œ ๋™์ž‘ํ•œ๋‹ค. JSP๋Š” ์‹คํ–‰์‹œ์—๋Š” Java Servlet์œผ๋กœ ๋ณ€ํ™˜๋œ ํ›„ ์‹คํ–‰๋˜๋ฏ€๋กœ ์„œ๋ธ”๋ฆฟ๊ณผ ๊ฑฐ์˜ ์œ ์‚ฌํ•˜๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ, ์„œ๋ธ”๋ฆฟ๊ณผ๋Š” ๋‹ฌ๋ฆฌ HTML ํ‘œ์ค€์— ๋”ฐ๋ผ ์ž‘์„ฑ๋˜๋ฏ€๋กœ ์›น ๋””์ž์ธํ•˜๊ธฐ์— ํŽธ๋ฆฌํ•˜๋‹ค.
 

JSP ์Šคํฌ๋ฆฝํŒ… ์š”์†Œ

JSP๋Š” HTML ์ฝ”๋“œ ๋‚ด์— Java ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•˜๊ธฐ ์œ„ํ•ด ์—ฌ๋Ÿฌ ์Šคํฌ๋ฆฝํŒ… ์š”์†Œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค:

  • ์„ ์–ธ๋ฌธ(Declaration): ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    jsp
    ์ฝ”๋“œ ๋ณต์‚ฌ
    <%! int counter = 0; %> <%! public int getCounter() { return counter; } %>
  • ์Šคํฌ๋ฆฝํ‹€๋ฆฟ(Scriptlet): ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ ์‹œ๋งˆ๋‹ค ํ˜ธ์ถœ๋˜๋ฉฐ, ์„œ๋ธ”๋ฆฟ์˜ service() ๋ฉ”์„œ๋“œ์— ํ•ด๋‹นํ•˜๋Š” ์˜์—ญ์œผ๋กœ, ์ฃผ๋กœ ์š”์ฒญ๊ณผ ์‘๋‹ต์— ๊ด€๋ จ๋œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
    jsp
    ์ฝ”๋“œ ๋ณต์‚ฌ
    <% counter++; %>
  • ํ‘œํ˜„์‹(Expression): ๋ฐ์ดํ„ฐ๋ฅผ ๋ธŒ๋ผ์šฐ์ €์— ์ถœ๋ ฅํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    jsp
    ์ฝ”๋“œ ๋ณต์‚ฌ
    <%= counter %>
  • ์ฃผ์„(Comment): ์ฝ”๋“œ์— ๋Œ€ํ•œ ์„ค๋ช…์„ ์ž‘์„ฑํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉฐ, ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ๋Š” ์ „๋‹ฌ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    jsp
    ์ฝ”๋“œ ๋ณต์‚ฌ
    <%-- This is a JSP comment --%>
     

JSP ์ง€์‹œ์–ด(Directive)

JSP ์ง€์‹œ์–ด๋Š” ํŽ˜์ด์ง€์˜ ์ „์—ญ์ ์ธ ์†์„ฑ์„ ์„ค์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค:

  • <%@ page %>: JSP ํŽ˜์ด์ง€์˜ ์†์„ฑ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  • <%@ include %>: ๋‹ค๋ฅธ JSP ํŒŒ์ผ์„ ์ •์ ์œผ๋กœ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.
  • <%@ taglib %>: ์ปค์Šคํ…€ ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.

 

JSP ๊ธฐ๋ณธ ๊ฐ์ฒด

JSP๋Š” ๊ฐœ๋ฐœ ํŽธ์˜๋ฅผ ์œ„ํ•ด ์—ฌ๋Ÿฌ ๊ธฐ๋ณธ ๊ฐ์ฒด๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค:

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

 

JSP ๊ธฐ๋ณธ ๊ฐ์ฒด์˜ ์˜์—ญ(Scope)

๊ฐ ๊ธฐ๋ณธ ๊ฐ์ฒด๋Š” ํŠน์ •ํ•œ ๋ฒ”์œ„(Scope)๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค:

  • pageContext: ํ•˜๋‚˜์˜ JSP ํŽ˜์ด์ง€ ๋‚ด์—์„œ๋งŒ ์œ ํšจํ•ฉ๋‹ˆ๋‹ค.
  • request: ํ•˜๋‚˜์˜ HTTP ์š”์ฒญ ๋™์•ˆ ์œ ํšจํ•˜๋ฉฐ, ์š”์ฒญ์ด ์™„๋ฃŒ๋˜๋ฉด ์‚ฌ๋ผ์ง‘๋‹ˆ๋‹ค.
  • session: ๋™์ผํ•œ ํด๋ผ์ด์–ธํŠธ์˜ ์—ฌ๋Ÿฌ ์š”์ฒญ ๊ฐ„์— ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์„ธ์…˜์ด ์ข…๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ์œ ํšจํ•ฉ๋‹ˆ๋‹ค.
  • application: ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹คํ–‰๋˜๋Š” ๋™์•ˆ ์ „์—ญ์ ์œผ๋กœ ์œ ํšจํ•ฉ๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ๊ฐ์ฒด๋“ค์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ๊ณต์œ ํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค:

  • setAttribute(String name, Object value): ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
  • getAttribute(String name): ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
  • removeAttribute(String name): ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

 

์›น ํŽ˜์ด์ง€ ์ด๋™

JSP์—์„œ๋Š” ๋‹ค๋ฅธ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๊ธฐ ์œ„ํ•ด forward์™€ sendRedirect๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค:

  • forward(request, response):
    • ์‚ฌ์šฉ ๋ฐฉ๋ฒ•: RequestDispatcher dispatcher = request.getRequestDispatcher(path); dispatcher.forward(request, response);
    • ์ด๋™ ๋ฒ”์œ„: ๋™์ผํ•œ ์„œ๋ฒ„ ๋‚ด์˜ ๊ฒฝ๋กœ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
    • URL ํ‘œ์‹œ: ๋ธŒ๋ผ์šฐ์ €์˜ ์ฃผ์†Œ์ฐฝ์— ๊ธฐ์กด URL์ด ์œ ์ง€๋ฉ๋‹ˆ๋‹ค.
    • ๊ฐ์ฒด ์ „๋‹ฌ: ๊ธฐ์กด์˜ request์™€ response ๊ฐ์ฒด๊ฐ€ ๊ทธ๋Œ€๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.
    • ์†๋„: ๋น„๊ต์  ๋น ๋ฆ…๋‹ˆ๋‹ค.
    • ๋ฐ์ดํ„ฐ ์œ ์ง€: request ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • sendRedirect(location):
    • ์‚ฌ์šฉ ๋ฐฉ๋ฒ•: response.sendRedirect(location);
    • ์ด๋™ ๋ฒ”์œ„: ๋™์ผํ•œ ์„œ๋ฒ„๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ์„œ๋ฒ„์˜ URL๋กœ๋„ ์ด๋™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • URL ํ‘œ์‹œ: ๋ธŒ๋ผ์šฐ์ €์˜ ์ฃผ์†Œ์ฐฝ์— ์ด๋™ํ•œ ํŽ˜์ด์ง€์˜ URL์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.
    • ๊ฐ์ฒด ์ „๋‹ฌ: ์ƒˆ๋กœ์šด request์™€ response ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.
    • ์†๋„: forward์— ๋น„ํ•ด ๋Š๋ฆฝ๋‹ˆ๋‹ค.
    • ๋ฐ์ดํ„ฐ ์œ ์ง€: request ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ์ „๋‹ฌ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉฐ, session์ด๋‚˜ cookie๋ฅผ ์ด์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 
input.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>์ง€์—ญ ์„ ํƒ</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
    <div class="container mt-5">
        <div class="col-lg-6 mx-auto">
            <h3 class="text-center">์ง€์—ญ ์„ ํƒ์— ๋”ฐ๋ฅธ ๋ฐฐ๊ฒฝ์ƒ‰ ๋ณ€๊ฒฝ</h3>
            <form action="result.jsp" method="post">
                <div class="form-group">
                    <label for="username">์ด๋ฆ„:</label>
                    <input type="text" id="username" name="username" class="form-control" placeholder="์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”" required>
                </div>
                <div class="form-group">
                    <label for="region">์ง€์—ญ ์„ ํƒ:</label>
                    <select id="region" name="region" class="form-control">
                        <option value="์„œ์šธ">์„œ์šธ</option>
                        <option value="๋Œ€์ „">๋Œ€์ „</option>
                        <option value="๊ตฌ๋ฏธ">๊ตฌ๋ฏธ</option>
                        <option value="๊ด‘์ฃผ">๊ด‘์ฃผ</option>
                        <option value="๋ถ€์šธ๊ฒฝ">๋ถ€์šธ๊ฒฝ</option>
                    </select>
                </div>
                <button type="submit" class="btn btn-primary btn-block">์ œ์ถœ</button>
            </form>
        </div>
    </div>
</body>
</html>

result.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>๊ฒฐ๊ณผ ํŽ˜์ด์ง€</title>
</head>
<body>
<%
    // ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ
    String name = request.getParameter("username");
    String region = request.getParameter("region");

    // ์ง€์—ญ์— ๋”ฐ๋ผ ๋ฐฐ๊ฒฝ์ƒ‰ ์„ค์ •
    String backgroundColor;
    switch (region) {
        case "์„œ์šธ":
            backgroundColor = "lightblue";
            break;
        case "๋Œ€์ „":
            backgroundColor = "orange";
            break;
        case "๊ตฌ๋ฏธ":
            backgroundColor = "pink";
            break;
        case "๊ด‘์ฃผ":
            backgroundColor = "green";
            break;
        case "๋ถ€์šธ๊ฒฝ":
            backgroundColor = "purple";
            break;
        default:
            backgroundColor = "white"; // ๊ธฐ๋ณธ๊ฐ’
    }
%>
    <style>
        body {
            background-color: <%= backgroundColor %>;
            color: #333;
            text-align: center;
            font-family: Arial, sans-serif;
        }
    </style>
    <h1>์•ˆ๋…•ํ•˜์„ธ์š”, <%= name %>๋‹˜!</h1>
    <p>์„ ํƒํ•œ ์ง€์—ญ: <%= region %></p>
</body>
</html>

RegionColorServlet.java

package com.example;

import java.io.IOException;
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์œผ๋กœ URL ๋งคํ•‘
@WebServlet("/regionColor")
public class RegionColorServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 1. ํด๋ผ์ด์–ธํŠธ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘
        String name = request.getParameter("username");
        String region = request.getParameter("region");

        // 2. ์ง€์—ญ์— ๋”ฐ๋ผ ๋ฐฐ๊ฒฝ์ƒ‰ ๊ฒฐ์ •
        String backgroundColor;
        switch (region) {
            case "์„œ์šธ":
                backgroundColor = "lightblue";
                break;
            case "๋Œ€์ „":
                backgroundColor = "orange";
                break;
            case "๊ตฌ๋ฏธ":
                backgroundColor = "pink";
                break;
            case "๊ด‘์ฃผ":
                backgroundColor = "green";
                break;
            case "๋ถ€์šธ๊ฒฝ":
                backgroundColor = "purple";
                break;
            default:
                backgroundColor = "white"; // ๊ธฐ๋ณธ๊ฐ’
        }

        // 3. ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ๋ฅผ JSP๋กœ ์ „๋‹ฌ
        request.setAttribute("name", name);
        request.setAttribute("region", region);
        request.setAttribute("backgroundColor", backgroundColor);

        // 4. JSP๋กœ ํฌ์›Œ๋”ฉ
        request.getRequestDispatcher("/result.jsp").forward(request, response);
    }
}

 
 
https://java-is-happy-things.tistory.com/26

JSP(Java Server  Page)

JSP JSP๋ž€? ์ž๋ฐ”์„œ๋ฒ„ํŽ˜์ด์ง€(JSP)๋Š” HTML ๋‚ด ์ž๋ฐ”์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•˜์—ฌ ์›น ์„œ๋ฒ„์—์„œ ๋™์ ์œผ๋กœ ์›น ํŽ˜์ด์ง€๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์›น ๋ธŒ๋ผ์šฐ์ €์— ๋Œ๋ ค์ฃผ๋Š” ์–ธ์–ด์ด๋‹ค. Java EE ์ŠคํŽ™ ์ค‘ ์ผ๋ถ€๋กœ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„œ๋ฒ„์—์„œ ๋™

java-is-happy-things.tistory.com

 

728x90

'๐Ÿ’ป์›น(Web)' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

์ฝ”๋“œ ๋ฆฌํŒฉํ† ๋ง  (1) 2024.12.20
Spring  (0) 2024.12.02
Servlet  (0) 2024.12.01
[๊ณตํ†ตํ”„๋กœ์ ํŠธ]5์ฃผ์ฐจ: React Openvidu ๊ตฌํ˜„  (0) 2024.08.11
[React]์ดˆ๊ธฐ์„ธํŒ…  (0) 2024.07.18