JiwonDev

#14. JSP์™€ ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ

by JiwonDev

2021.07.14 - [Backend/Java] - # 13 JSP (Java Server Pages)

 

# 13 JSP (Java Server Pages)

# Web Server ์›น ํ‘œ์ค€ ํ”„๋กœํ† ์ฝœ(HTTP)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Clinet-Server ๊ตฌ์กฐ๋กœ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์— ๋”ฐ๋ผ ์ •์ ์ธ ๋ฐ์ดํ„ฐ (HTML, CSS, JS, ์ด๋ฏธ์ง€ ๋ฆฌ์†Œ์Šค ๋“ฑ)๋ฅผ ์‘๋‹ตํ•œ๋‹ค. ์‚ฌ์šฉ์ž์™€ ์ž…๋ ฅ๊ฐ’์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง€๋Š” ๋™์ ์ธ ๋ฐ์ด

jiwondev.tistory.com

์ตœ๊ทผ(2021)์˜ Java๋ฅผ ํ†ตํ•œ ์›น ๊ฐœ๋ฐœ์€ ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ํ‘œ์ค€์— ๊ฐ€๊น๊ฒŒ ์‚ฌ์šฉ๋œ๋‹ค. ์ฆ‰ JSP๋Š” ์‚ฌ์žฅ(deprecated) ๋˜์–ด๊ฐ€๋Š” ๊ธฐ์ˆ ์ด ๋˜์—ˆ๊ณ  ์„œ๋ธ”๋ฆฟ์„ ์ง์ ‘ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ•  ์ผ์€ ๋งŽ์ด ์—†๋‹ค. ํ•˜์ง€๋งŒ ์Šคํ”„๋ง MVC๋„ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ณด์ด์ง€ ์•Š์„ ๋ฟ์ด์ง€ ๋‚ด๋ถ€์ ์œผ๋กœ๋Š” ์„œ๋ธ”๋ฆฟ์„ ์ด์šฉํ•˜์—ฌ ๋งŒ๋“ค์–ด์ ธ์žˆ๊ธฐ์— ๊ฐœ๋ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์„œ๋ธ”๋ฆฟ ์ŠคํŽ™์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ํ•„์ˆ˜์ ์ด๋‹ค.

 

๊ทธ๋Ÿฌ๋ฏ€๋กœ ๊ธฐ์กด์— JSP๋ฅผ ์ด์šฉํ•œ ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ์–ด๋–ป๊ฒŒ ๊ฐœ๋ฐœ๋˜์—ˆ๋Š”์ง€ ๋Œ€๋žต์ ์œผ๋กœ ์•Œ์•„๋ณด๋ฉฐ ์ž๋ฐ”์™€ ์„œ๋ธ”๋ฆฟ์— ๋Œ€ํ•œ ๊ธฐ์ดˆ์ง€์‹์„ ์Œ“์•„๋ณด์ž.

 

#0 JSP์˜ ์ดํ•ด (21.7.18์ถ”๊ฐ€)

JSP์— ์ ํ˜€์žˆ๋Š” ์ž๋ฐ”์ฝ”๋“œ๋Š” WAS๊ฐ€ ๋”ฐ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ๋งŒ๋“ ๋‹ค๋Š” ๊ฑธ ์•ž์—์„œ ๋ฐฐ์› ๋‹ค.

1. ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์›น ์„œ๋ฒ„์— ์š”์ฒญ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

2. ์›น ์„œ๋ฒ„๊ฐ€ WAS(์ปจํ…Œ์ด๋„ˆ)์— ์š”์ฒญ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•˜๊ณ  JSP๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

3. JSP ์— ์žˆ๋Š” ์ž๋ฐ” ์ฝ”๋“œ๋Š” ์ปจํ…Œ์ด๋„ˆ์— ์˜ํ•ด ์„œ๋ธ”๋ฆฟ ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜๋œ๋‹ค. (์ด๋ฏธ ๋ณ€ํ™˜ํ–ˆ๋‹ค๋ฉด ์žˆ๋Š”๊ฑธ ์‚ฌ์šฉํ•œ๋‹ค.)

4. ๋ณ€ํ™˜๋œ ์„œ๋ธ”๋ฆฟ ์ฝ”๋“œ๋Š” ์ปดํŒŒ์ผ๋˜์–ด ์‹คํ–‰๊ฐ€๋Šฅํ•œ bytecode(.class)๋กœ ๋ณ€ํ™˜๋˜์–ด WAS์— ๋กœ๋”ฉ๋œ๋‹ค.

5. ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ํ•„์š”ํ•œ ์š”์ฒญ์„ ์ฒ˜๋ฆฌ ํ›„ ์›น์„œ๋ฒ„์— ๋ณด๋‚ผ ์‘๋‹ต์„ ์ƒ์„ฑํ•œ๋‹ค.

 

Java์—์„œ JSP๋Š” Sevlet ํด๋ž˜์Šค (HTTPServlet)๋ฅผ ์ƒ์†๋ฐ›์•„ ๋งŒ๋“ค์–ด์ง„ ํด๋ž˜์Šค์ด๋‹ค. WAS์—์„œ๋Š” HTML๊ณผ Java์ฝ”๋“œ๊ฐ€ ํ•ฉ์ณ์ง„ JSP๋ฅผ ์ฝ์–ด๋“ค์—ฌ HTML ๋ถ€๋ถ„๊ณผ Java ๋ถ€๋ถ„์„ ๋ถ„๋ฆฌํ•˜๊ณ , ์ž๋ฐ”์ฝ”๋“œ๋ฅผ JSP ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค์–ด ์ฒ˜๋ฆฌํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

 

#1. ๊ฐœ๋ฐœํ™˜๊ฒฝ

IntelliJ(ํŽธ์ง‘๊ธฐ)์™€ Tomcat(WAS + Web Server)์„ ์‚ฌ์šฉํ•œ๋‹ค. ๋งํฌ์— ๋“ค์–ด๊ฐ€ ๋‹ค์šด๋กœ๋“œ ๋ฐ›๋„๋ก ํ•˜์ž.

ํ†ฐ์บฃ์€ ์ž˜ ์ฝ์–ด๋ณด๊ณ  ์„ค์น˜ํ•˜๋ฉด ๋˜๊ณ , Intellij์—์„œ๋Š” ์ž๋ฐ” ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“  ๋’ค์— ํ”„๋กœ์ ํŠธ๋ช… ์šฐํด๋ฆญ -> Add Framework Support๋ฅผ ๋ˆŒ๋Ÿฌ ์›น ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ ์„ธํŒ…ํ•˜์ž.

์ž์„ธํ•œ ์„ค๋ช…

๋”๋ณด๊ธฐ

Tomcat์€ ์•„ํŒŒ์น˜(Apache)์žฌ๋‹จ์—์„œ ๋งŒ๋“  ๋ฌด๋ฃŒ WAS๋กœ ์•„ํŒŒ์น˜ ์„œ๋ฒ„๋ฅผ ๋‚ด์žฅํ•˜๊ณ  ์žˆ๋‹ค. ์„ค์น˜ํ•  ๋•Œ ์—ฌ๋Ÿฌ ์˜ต์…˜์„ ๋ฌผ์–ด๋ณผ๊ฑด๋ฐ, ๊ทธ๋ƒฅ ์ฝ์–ด๋ณด๊ณ  Next๋งŒ ๋ˆŒ๋Ÿฌ์ฃผ๋ฉด ๋˜๊ธฐ์— ์–ด๋ ต์ง€ ์•Š๊ฒŒ ์„ค์น˜ ํ•  ์ˆ˜ ์žˆ๋‹ค.

Tomcat ๋‹ค์šด๋กœ๋“œ ๋ฐฉ๋ฒ•

intellJ๋ฅผ ์„ค์น˜ํ•ด์„œ ๋นˆ ์ž๋ฐ” ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“  ํ›„ JavaEE ์›น ์„œ๋น„์Šค ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค.

ํ”„๋กœ์ ํŠธ๋ช…์„ ์šฐํด๋ฆญํ•˜์—ฌ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. ์˜์–ด๋กœ [ Add Framework Support ] ์ด๋‹ค.
Java EE์— ์žˆ๋Š” Web Service๋ฅผ ๋ˆŒ๋Ÿฌ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ธํŒ…ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  IntelliJ์— ์„ค์น˜ํ•œ ํ†ฐ์บฃ์„ ์—ฐ๊ฒฐํ•˜์ž.

์šฐ์ธก ์ƒ๋‹จ์— ์‹คํ–‰ํ™˜๊ฒฝ์„ ์„ค์ •ํ•ด์ฃผ์ž.
์™ผ์ชฝ ์œ„ [ + ] ๋ฅผ ๋ˆŒ๋Ÿฌ Tomcat Server - Local์„ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
์ดํ›„ ์„ค์ •(configure)๋ฅผ ๋ˆŒ๋Ÿฌ Tomcat ์„ค์น˜๊ฒฝ๋กœ๋ฅผ ๋“ฑ๋กํ•ด์ฃผ์ž.
์„ค์ •์„ ์™„๋ฃŒํ•˜๋ฉด web ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์ƒ์„ฑ๋˜๋Š”๋ฐ, server-config.wsdd๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์‚ญ์ œํ•ด๋„ ๊ดœ์ฐฎ๋‹ค.

web ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ๋งŒ๋“ค์–ด์กŒ๋‹ค๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์„œ ์›น ์•ฑ์„ ํ†ฐ์บฃ์— ์‹คํ–‰์‹œ์ผœ๋ณด์ž. ๋งŒ์•ฝ ๊ฐœ๋ฐœํ™˜๊ฒฝ ๊ตฌ์„ฑ์— ์‹คํŒจํ–ˆ๋‹ค๋ฉด ํ•ด๋‹น ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•˜๋„๋ก ํ•˜์ž.

// web.xml ํŒŒ์ผ

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
</web-app>
// index.jsp์˜ ํŒŒ์ผ. Tomcat WAS๋ฅผ ํ†ตํ•ด ์‹คํ–‰์‹œ์ผœ์•ผ ํ•œ๋‹ค.

<%--
  Created by IntelliJ IDEA.
  User: -
  Date: 2021-07-15
  Time: ์˜คํ›„ 11:13
  To change this template use File | Settings | File Templates.
--%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>$Title$</title>
</head>
<body>
<%
    String bookTitle = "JSP ์ฑ…";
    String author = "์ง€์›ํ‚ด";
%>

<b><%= bookTitle %></b>์˜ ์ €์ž <%=author%> ์ž…๋‹ˆ๋‹ค.
</body>
</html>

 


#2. JSP์˜ ๊ตฌ์กฐ

๊ธฐ๋ณธ์ ์œผ๋กœ HTML ์ฝ”๋“œ ๋‚ด์— <% %> ๋ชจ์–‘์˜ ํƒœ๊ทธ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค. ์ฐธ๊ณ ๋กœ ์ฃผ์„์€ <%-- ๋‚ด์šฉ --%> ์œผ๋กœ ์ž‘์„ฑ ๊ฐ€๋Šฅํ•˜๋‹ค. 

#2-1 ๋””๋ ‰ํ‹ฐ๋ธŒ (Directive)

JSP ํŽ˜์ด์ง€์— ๋Œ€ํ•œ ์„ค์ • ์ •๋ณด๋ฅผ ์ง€์ •ํ•œ๋‹ค. 

๋””๋ ‰ํ‹ฐ๋ธŒ ์„ค๋ช…
page JSP ํŽ˜์ด์ง€์— ๋Œ€ํ•œ ์ •๋ณด. JSP๊ฐ€ ์ƒ์„ฑํ•˜๋Š” ๋ฌธ์„œ์˜ ํƒ€์ž…, ์ถœ๋ ฅ๋ฒ„ํผ ํฌ๊ธฐ, ์—๋ŸฌํŽ˜์ด์ง€๋“ฑ์„ ์ง€์ •ํ•œ๋‹ค.
include JSP ํŽ˜์ด์ง€์˜ ํŠน์ • ์˜์—ญ์— ๋‹ค๋ฅธ ๋ฌธ์„œ๋ฅผ ํฌํ•จ์‹œํ‚จ๋‹ค.
taglib JSP ํŽ˜์ด์ง€์—์„œ ์‚ฌ์šฉํ•  ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ง€์ •ํ•œ๋‹ค. prefix๋กœ ๋ณ„๋ช…์„ ๋‹ฌ์•„ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

<%@ ๋””๋ ‰ํ‹ฐ๋ธŒ์ด๋ฆ„ ์†์„ฑ1="๊ฐ’1" ์†์„ฑ2="๊ฐ’2" ... %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file = "test_url">
<%@ taglib   uri=http://java.sun.com/jsp/jstl/core    prefix=“c"  %>

...
<c:out value="Hello World"> </c:out>

 


#2-2 ์Šคํฌ๋ฆฝํŠธ (Sciprt)

<% %> ์•ˆ์— Java ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ํ•ด๋‹น ํƒœ๊ทธ๋Š” 3๊ฐ€์ง€ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋‹ค.

  • Scriptlet <% ์ฝ”๋“œ %>
    ์ž๋ฐ” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๊ตฌ์„ฑ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Expression <%= ๋‚ด์šฉ or ํ‘œํ˜„์‹ %>
    out ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. 1+2+3 ๊ฐ™์€ ํ‘œํ˜„์‹์„ ์ฃผ๋ฉด ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
  • Declarations <%! ๋ฉ”์„œ๋“œ %>
    JSP์—์„œ ์‚ฌ์šฉํ•  ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•œ๋‹ค. Expression์„ ์ด์šฉํ•ด ๋ฐ˜ํ™˜๊ฐ’์„ HTML๋กœ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.

 


#2-3 JSP Action

XML ๋ฌธ๋ฒ•์„ ์ด์šฉํ•˜์—ฌ ์„œ๋ธ”๋ฆฟ ์—”์ง„์„ ์ปจํŠธ๋กคํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค. ์ด๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ์˜ˆ์ œ์ฝ”๋“œ๋งŒ ๋ณด๊ณ  ๋„˜์–ด๊ฐ€๋„๋ก ํ•˜์ž.

ํ˜„์žฌ JSP ํŽ˜์ด์ง€์— ๋‹ค๋ฅธ ๋ฆฌ์†Œ์Šค๋ฅผ ํฌํ•จ์‹œํ‚ค๋Š”๋ฐ ์‚ฌ์šฉ.
<jsp:include page="/jsp/common/uppermenu.jsp" flush="true"/>

๋‹ค๋ฅธ ๋ฆฌ์†Œ์Šค(JSP, html ๋˜๋Š” Servlet)๋กœ ์š”์ฒญ์„ ์ „๋‹ฌํ•˜๋Š”๋ฐ ์‚ฌ์šฉ.
<jsp:forward page=“/display.jsp" />

์ž๋ฐ”์˜ Bean(๊ฐ์ฒด)๋ฅผ ์ƒ์„ฑ.
<jsp:useBean id="customer" class=“package.class”  scope="request"/>

 


#3. JSP ์ž‘์„ฑํ•˜๊ธฐ

์‹คํ–‰๊ฒฐ๊ณผ

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--page ๋””๋ ‰ํ‹ฐ๋ธŒ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž๋ฐ” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ import ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.--%>
<%@ page import="java.util.Date" %>

<%-- ์ž๋ฐ” ์ฝ”๋“œ๋Š” <% %> ์•ˆ์— ์ž‘์„ฑํ•œ๋‹ค. --%>
<%
    Date now = new Date(); // Date ํด๋ž˜์Šค
%>

<%-- ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ• ๋•Œ์—๋Š” <%! %> ์•ˆ์— ์ž‘์„ฑํ•œ๋‹ค. --%>
<%-- ์„ ์–ธ๋ถ€๊ฐ€ ์•„๋‹Œ ์ผ๋ฐ˜ ์ž๋ฐ”์ฝ”๋“œ๋ฅผ ์—ฌ๊ธฐ ์ž‘์„ฑํ•ด๋„ ๋™์ž‘ํ•˜์ง€๋งŒ, ๋˜๋„๋ก์ด๋ฉด ๊ตฌ๋ถ„ํ•ด์„œ ์“ฐ์ž. --%>
<%!
    public int multiply(int a, int b) {
        return a * b;
    }
%>

<html>
<head>
    <title>ํ˜„์žฌ ์‹œ๊ฐ„</title>
</head>

<body>
    <%--๋ณ€์ˆ˜, ๋ฉ”์„œ๋“œ, ๊ณ„์‚ฐ์‹์„ HTML๋กœ ์ถœ๋ ฅํ•˜๋ ค๋ฉด <%= %> ๋ฅผ ์ด์šฉํ•œ๋‹ค.--%>
    1+1 = <%= 1 + 1 %><br/>
    3*4 = <%= multiply(3,4) %><br/>
    <strong> ํ˜„์žฌ์‹œ๊ฐ : <%= now %> </strong>
</body>
</html>

#3-1 Requset ๊ฐ์ฒด

์›น ์„œ๋ฒ„์—์„œ ํด๋ผ์ด์–ธํŠธ์˜ ์—ฐ๊ฒฐ ์ •๋ณด๋ฅผ ๋ฐ›์œผ๋ ค๋ฉด JSP ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” requset ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.

ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

<%@ page contentType="text/html; charset=utf-8" %>

<!DOCTYPE html>
<html>
<head>
    <title>ํด๋ผ์ด์–ธํŠธ ๋ฐ ์„œ๋ฒ„ ์ •๋ณด</title>
</head>
<body>
ํด๋ผ์ด์–ธํŠธ IP <%=request.getRemoteAddr() %><br/>
์š”์ฒญ์ •๋ณด ์ปจํ…์ธ ๊ธธ์ด <%=request.getContentLength() %><br/>
์š”์ฒญ์ •๋ณด ์ธ์ฝ”๋”ฉ <%=request.getCharacterEncoding() %><br/>
์š”์ฒญ์ •๋ณด ์ปจํ…์ธ ํƒ€์ž… <%=request.getContentType() %><br/>
์š”์ฒญ์ •๋ณด ํ”„๋กœํ† ์ฝœ <%=request.getProtocol() %><br/>
์š”์ฒญ์ •๋ณด ์ „์†ก๋ฐฉ์‹ <%=request.getMethod() %><br/>
์š”์ฒญ URI <%=request.getRequestURI() %><br/>
์ปจํ…์ŠคํŠธ ๊ฒฝ๋กœ <%=request.getContextPath() %><br/>
์„œ๋ฒ„ ์ด๋ฆ„<%=request.getServerName() %><br/>
์„œ๋ฒ„ ํฌํŠธ<%=request.getServerPort() %><br/>
</body>
</html>

 

๋ฌผ๋ก  request ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด์„œ ์ •๋ณด ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ธŒ๋ผ์šฐ์ €(ํด๋ผ์ด์–ธํŠธ)๊ฐ€ ์ „์†กํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ๋‹ค.

Requset ๊ฐ์ฒด ๋ฉ”์„œ๋“œ ์„ค๋ช…
String getParameter(String name) ํŒŒ๋ผ๋ฏธํ„ฐ ๋ช…์„ ํ†ตํ•ด ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’ ์ฝ๊ธฐ -> ์—†์„๊ฒฝ์šฐ null
String[] getParameterValues(String name) ์ด๋ฆ„์ด name์ธ ๊ฐ’๋“ค์„ ๋ฐฐ์—ด๋กœ ๋ฐ›๊ธฐ -> ์—†์„๊ฒฝ์šฐ null
Enumeration getParameterNames() ํŒŒ๋ผ๋ฏธํ„ฐ ๋ชฉ๋ก ์ฝ๊ธฐ
Map getParameterMap() ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค์˜ <์ด๋ฆ„, ๊ฐ’> ์„ Map ํ˜•ํƒœ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ

form.jsp

<%@ page contentType="text/html; charset=utf-8" %>

<!DOCTYPE html>
<html>
<head>
    <title>๋ณด๋‚ด๊ธฐ</title>
</head>
<body>

    <form action="/viewParameter.jsp" method="post">
        <label for="name">์ด๋ฆ„ </label>
        <input id="name" type="text" name="name" size="10"><br>

        <label for="addr">์ฃผ์†Œ</label>
        <input id="addr" type="text" name="addr" size="30"><br>

        <input type="checkbox" name="pet" value="dog">๊ฐ•์•„์ง€
        <input type="checkbox" name="pet" value="cat">๊ณ ์–‘์ด
        <input type="checkbox" name="pet" value="pig">๋ผ์ง€

        <input type="submit" value="์ „์†ก">

        <br>
        <br>
    </form>

</body>

viewParameter.jsp

<%@ page contentType="text/html; charset=utf-8" %>
<%@ page import="java.util.Enumeration" %>
<%@ page import="java.util.Map" %>

<%
    request.setCharacterEncoding("utf-8");
%>

<!DOCTYPE html>
<html>
<head>
    <title>์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ ์ถœ๋ ฅ</title>
</head>
<body>

<b>getParameter() ๋ฉ”์†Œ๋“œ</b><br>

name= <%= request.getParameter("name") %><br>
address= <%= request.getParameter("addr") %><br>

<br>
<b>getParameterValues() ๋ฉ”์†Œ๋“œ</b><br>
<%
    String[] values = request.getParameterValues("pet");
    if (values != null) {
        for (int i = 0; i < values.length; i++) {
%>
<%= values[i] %><br>
<%
        }//์ž๋ฐ”์ฝ”๋“œ ์ค‘๊ฐ„์— ๊ฐ’์„ ์ถœ๋ ฅํ•ด์•ผํ•œ๋‹ค๋ฉด, ์ด๋Ÿฐ์‹์œผ๋กœ ๋‚˜๋ˆ„์–ด ์‚ฌ์šฉํ•ด๋„๋œ๋‹ค.
    }
%>

<br>
<b>getParameterNames() ๋ฉ”์†Œ๋“œ</b><br>

<%
    Enumeration enums = request.getParameterNames();
    while (enums.hasMoreElements()) {
%>
<%= enums.nextElement() %><br>
<%
    }
%>

<br>
<b>getParameterMap() ๋ฉ”์†Œ๋“œ</b><br>

<%
    Map paramMap = request.getParameterMap();
%>

<%= ((String[]) paramMap.get("name"))[0] %><br>
<%= ((String[]) paramMap.get("addr"))[0] %><br>
<%= ((String[]) paramMap.get("pet"))[0] %><br>
<%= ((String[]) paramMap.get("pet"))[1] %><br>

</body>
</html>

 


# 3-2 Respone ๊ฐ์ฒด

JSP์—์„œ๋„ Respone๊ฐ€ ์ œ๊ณต๋˜๋‚˜, ๊ฑฐ์˜ ์‚ฌ์šฉํ•  ์ผ์ด ์—†๋‹ค.

  • ๊ฑฐ์˜ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ๊ฐ์ฒด
    • HTTP ํ—ค๋”์— ๋‚ด์šฉ ์ถ”๊ฐ€
    • ๋‹ค๋ฅธ ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ(redirect)
  • ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ์‹œํ‚ค๊ธฐ
    • response.sendRedirect() ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ
<%@ page contentType="text/html; charset=utf-8" %>

<!DOCTYPE html>
<html>
<head>
    <title>๋ฆฌ๋‹ค์ด๋ ‰ํŠธํŽ˜์ด์ง€</title>
</head>
<body>

<%
    String redirect = request.getParameter("redirect");
    if(redirect != null && redirect.equals("yes")) {
        response.sendRedirect("/chap03/form.jsp");
    }
%>
hello

</body>
</html>

 


#4. GET๊ณผ POST

์œ„์˜ form.jsp์—์„œ๋Š” POST ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค. (์˜ต์…˜์„ ์•ˆ์ฃผ๋ฉด ๊ธฐ๋ณธ๊ฐ’ GET์œผ๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค.)

<form action="jspํŽ˜์ด์ง€ ๊ฒฝ๋กœ" method="post">
    ...
</form>

get์€ Body๊ฐ€ ์—†๋‹ค. ์ตœ์‹  ์ŠคํŽ™์—์„œ๋Š” ๋ฐ”๋”” ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‚˜ ๋ณดํ†ต ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

# 4-1 GET

  • URL ๊ฒฝ๋กœ ๋’ค์— ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ถ™์—ฌ์„œ ์ „์†ก
    - http://localhost:8080/abc/asdf.jsp?age=23&gender=woman&address=seoul
    - ๋ฌผ์Œํ‘œ(?) ๋’ค์— ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ถ™์—ฌ์„œ ์ „์†ก
    - ํŒŒ๋ผ๋ฏธํ„ฐ ์‚ฌ์ด๋ฅผ & ๊ธฐํ˜ธ๋กœ ๊ตฌ๋ถ„
    - ํŒŒ๋ผ๋ฏธํ„ฐ์ด๋ฆ„=ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’
  • ํŒŒ๋ผ๋ฏธํ„ฐ ๊ธธ์ด์— ์ œํ•œ์ด ์žˆ๋‹ค.
    - ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๊ธธ์–ด์ง€๋ฉด URL๋„ ๊ธธ์–ด์ง„๋‹ค.
    - ๊ธธ์ด ์ œํ•œ์€ ์›น ๋ธŒ๋ผ์šฐ์ €๋‚˜ ์„œ๋ฒ„์— ๋”ฐ๋ผ ๋‹ค๋ฅด๋‹ค.
  • Idempotent(๋ฉฑ๋“ฑ)์˜ ์„ฑ์งˆ์„ ๊ฐ€์ง„๋‹ค.
    - ์š”์ฒญ์„ ์—ฌ๋Ÿฌ๋ฒˆ ์ˆ˜ํ–‰ํ•˜๋”๋ผ๋„ ํ•ญ์ƒ ๊ฐ™์€ ๊ฒฐ๊ณผ ๋ณด์žฅ
    - ๋”ฐ๋ผ์„œ ์ฃผ๋กœ '์กฐํšŒ' ๊ธฐ๋Šฅ์— ์‚ฌ์šฉ๋œ๋‹ค.
  • ์ธ์ฝ”๋”ฉ ๊ทœ์น™
    - <a>ํƒœ๊ทธ : ์›น ํŽ˜์ด์ง€์™€ ๊ฐ™์€ ์ธ์ฝ”๋”ฉ ์‚ฌ์šฉ
    - <form>ํƒœ๊ทธ : ์›น ํŽ˜์ด์ง€์™€ ๊ฐ™์€ ์ธ์ฝ”๋”ฉ ์‚ฌ์šฉ
    - URL์— ์ฟผ๋ฆฌ ๋ฌธ์ž์—ด ๋ถ™์—ฌ์„œ ์ „์†ก : ์›น ๋ธŒ๋ผ์šฐ์ €๋งˆ๋‹ค ๋‹ค๋ฆ„
  • ๋””์ฝ”๋”ฉ ์ฒ˜๋ฆฌ
    - WAS๋งˆ๋‹ค ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋ณธ ์บ๋ฆญํ„ฐ์…‹์ด ๋‹ค๋ฅด๋‹ค.
    - ex) ํ†ฐ์บฃ8 ์ดํ›„๋Š” utf-8, ํ†ฐ์บฃ7์€ ISO-8859-1 ์‚ฌ์šฉ
  • HTTP ํ—ค๋” ์˜ˆ์‹œ
GET /home.html HTTP/1.1
Host: localhost:8080/example_page.jsp?name=steve&message=hello
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/ *;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
...

#4-2 POST

  • ์š”์ฒญ ํ—ค๋”์˜ ๋ฐ์ดํ„ฐ ์˜์—ญ์— ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„ฃ์–ด์„œ ์ „์†ก
    - URL ๋’ค์— ํŒŒ๋ผ๋ฏธํ„ฐ ์ •๋ณด๊ฐ€ ๋ถ™์ง€ ์•Š๋Š”๋‹ค.
  • GET๋ฐฉ์‹๊ณผ ๋‹ฌ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ธธ์ด์— ์ œํ•œ์ด ์—†๋‹ค.
  • Non-Idempotent์˜ ์„ฑ์งˆ์„ ๊ฐ€์ง„๋‹ค.
    - ์„œ๋ฒ„์— ์—ฌ๋Ÿฌ๋ฒˆ ์š”์ฒญํ•จ์—๋”ฐ๋ผ ๋‹ค๋ฅธ ์‘๋‹ต์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
    - ๋”ฐ๋ผ์„œ ์„œ๋ฒ„์˜ ์ƒํƒœ๋‚˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝ์‹œํ‚ฌ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
    - ex) ๋ธ”๋กœ๊ทธ๋“ฑ์— ๊ธ€์„ ๊ฒŒ์‹œํ•  ๋•Œ ๊ธ€์˜ ๋‚ด์šฉ์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ณด๋‚ด๊ธฐ
  • ์ธ์ฝ”๋”ฉ ๊ทœ์น™
    - POST ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญํ•  ๋•Œ ์›น ๋ธŒ๋ผ์šฐ์ €๋Š” ์‘๋‹ตํ™”๋ฉด์— ์ง€์ •๋œ ์บ๋ฆญํ„ฐ์…‹์„ ์‚ฌ์šฉํ•ด์„œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ธ์ฝ”๋”ฉํ•œ๋‹ค.
  • ๋””์ฝ”๋”ฉ ์ฒ˜๋ฆฌ
    - request.setCharacterEncoding()๋Š” POST์˜ ๋ฐ์ดํ„ฐ ์˜์—ญ์— ๋Œ€ํ•œ ๋””์ฝ”๋”ฉ ๋ฐฉ์‹์„ ์„ค์ •ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค.
    - ๋”ฐ๋ผ์„œ GET ๋ฐฉ์‹์œผ๋กœ ๊ฐ€์ ธ์˜จ ํŒŒ๋ผ๋ฏธํ„ฐ์—๋Š” ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.(ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ URL์— ๋ถ™์–ด์„œ ์˜ค๊ธฐ๋•Œ๋ฌธ)
    - ์‘๋‹ต ํŽ˜์ด์ง€์—์„œ request ๊ฐ์ฒด๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ์ฝ์–ด ๋””์ฝ”๋”ฉ ํ•˜๊ธฐ์ „์— ์บ๋ฆญํ„ฐ์…‹(ํ•ด๋‹น ํŽ˜์ด์ง€์˜ contentType์—์„œ ์ง€์ •ํ•œ charset)์„ ์ง€์ •ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
  • HTTP ํ—ค๋” ์˜ˆ์‹œ (์š”์ฒญ ํ—ค๋”์˜ ๋ฐ์ดํ„ฐ์˜์—ญ์— ํŒŒ๋ผ๋ฉ”ํƒ€๊ฐ€ ์ˆจ๊ฒจ์ ธ์žˆ๋‹ค.)
POST /myform.html HTTP/1.1
Host: localhost:8080/example_page.jsp
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0
Content-Length: 128
...
name=steve&message=hello

'๐ŸŒฑBackend > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

#16 Servlet ์ดํ•ด  (1) 2021.07.18
#15 JSP ์ฟ ํ‚ค์™€ ์„ธ์…˜  (0) 2021.07.16
#13 JSP (Java Server Pages)  (0) 2021.07.14
#12. Optional API (Null๊ฐ’ ์ฒ˜๋ฆฌ)  (0) 2021.07.12
#11 Stream API  (0) 2021.07.12

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

JiwonDev

JiwonDev

ํ™œ๋™ํ•˜๊ธฐ