๐Ÿ’ป์›น(Web)

Servlet

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

๐Ÿ“Œ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ธฐ๋ณธ ์•„ํ‚คํ…์ฒ˜

https://velog.io/@y_dragonrise/Web-%EC%9B%B9-%EC%95%A0%ED%94%8C%EB%A6%AC%EC%BC%80%EC%9D%B4%EC%85%98-%EC%95%84%ED%82%A4%ED%85%8D%EC%B2%98%EC%9D%98-%ED%8A%B9%EC%A7%95

1. Client(ํด๋ผ์ด์–ธํŠธ)

Web Browser(์›น ๋ธŒ๋ผ์šฐ์ €)

์‚ฌ์šฉ์ž๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ฑฐ๋‚˜ ์š”์ฒญ(Request)๋ฅผ ๋ณด๋‚ผ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์œผ๋กœ, ํด๋ผ์ด์–ธํŠธ๋Š” ์›น ๋ธŒ๋ผ์šฐ์ €๋ฅผ ํ†ตํ•ด ์„œ๋ฒ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•˜๊ณ , ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์‘๋‹ต(Repsonse)๋ฅผ ๋ฐ›๋Š”๋‹ค.

2. Server(์„œ๋ฒ„)

์„œ๋ฒ„๋Š” Web Server์™€ Web Application Server๋กœ ๋‚˜๋‰˜๋ฉฐ, ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

(1) Web Server

ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ์š”์ฒญ์„ ๋ฐ›์•„ ์‘๋‹ต์„ ์ „๋‹ฌํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•œ ๋’ค, WAS๋กœ ์ „๋‹ฌํ•œ๋‹ค.

์ฃผ๋กœ ์ •์  ๋ฆฌ์†Œ์Šค(HTML, CSS, JS ํŒŒ์ผ)์„ ์ฒ˜๋ฆฌํ•˜๊ณ , ๋™์  ์š”์ฒญ์€ WAS๋กœ ๋„˜๊ธด๋‹ค.

(2) Web Application Server

ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์— ๋Œ€ํ•œ ์‹ค์ œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ณณ์ด๋‹ค.

- Presentation(ํ”„๋ ˆ์  ํ…Œ์ด์…˜): ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์งˆ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๊ณตํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

- Business Login(๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง): ์š”์ฒญ์— ๋”ฐ๋ผ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ์ž‘์—…์„ ์ •์˜ํ•˜๊ณ  ์‹คํ–‰ํ•œ๋‹ค.

- Persistence Login(๋ฐ์ดํ„ฐ ์˜์†์„ฑ ๋กœ์ง): ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ํ†ต์‹ (JDBC)์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ์กฐํšŒํ•œ๋‹ค.

(3) Database

RDBMS (๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค)
์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„์˜ Persistence Logic์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.

  • JDBC(Java Database Connectivity) API๋ฅผ ํ†ตํ•ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„์™€ ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค.
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญํ•œ ๋ฐ์ดํ„ฐ์˜ ์˜๊ตฌ ์ €์žฅ ๋ฐ ๊ด€๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿ“ŒServlet

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

 

Servlet Life-Sycle

Servlet class๋Š” javaSE์—์„œ์˜ class์™€ ๋‹ค๋ฅด๊ฒŒ main method๊ฐ€ ์—†๋‹ค. ์ฆ‰, ๊ฐ์ฒด์˜ ์ƒ์„ฑ๋ถ€ํ„ฐ ์‚ฌ์šฉ(method call)์˜ ์ฃผ์ฒด๊ฐ€ ์‚ฌ์šฉ์ž๊ฐ€ ์•„๋‹Œ Servlet Container์— ์žˆ๋‹ค. Client๊ฐ€ ์š”์ฒญํ•˜๊ฒŒ ๋˜๋ฉด Servlet Container๋Š” Servlet ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์ดˆ๊ธฐํ™”ํ•˜๋ฉฐ ์š”์ฒญ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ฒŒ ๋œ๋‹ค. ๋˜ํ•œ Servlet ๊ฐ์ฒด๊ฐ€ ํ•„์š” ์—†๊ฒŒ ๋˜๋ฉด ์ œ๊ฑฐํ•˜๋Š” ์ผ๊นŒ์ง€ Container๊ฐ€ ๋‹ด๋‹นํ•˜๊ฒŒ ๋œ๋‹ค.

- Init() : ์„œ๋ธ”๋ฆฟ์ด ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋“œ๋  ๋•Œ ํ•œ๋ฒˆ๋งŒ ํ˜ธ์ถœ (์ฝ”๋“œ ์ˆ˜์ • ์‹œ ๋‹ค์‹œ ํ˜ธ์ถœ) ๐Ÿ”ด

- doGet() : GET๋ฐฉ์‹์œผ๋กœ data์ „์†ก ์‹œ ํ˜ธ์ถœ ๐ŸŸก

- doPost() : POST๋ฐฉ์‹์œผ๋กœ data์ „์†ก ์‹œ ํ˜ธ์ถœ ๐ŸŸก

- service() : ๋ชจ๋“  ์š”์ฒญ์€ service()๋ฅผ ํ†ตํ•ด doXXX()๋ฉ”์†Œ๋“œ๋กœ ์ด๋™ ๐ŸŸก

- destroy() : ์„œ๋ธ”๋ฆฟ์ด ๋ฉ”๋ชจ๋ฆฌ์—์„œ ํ•ด์ œ๋˜๋ฉด ํ˜ธ์ถœ๐Ÿ”ด

๐Ÿ”ด : ์ตœ์ดˆ ์š”์ฒญ ์‹œ ํ•œ๋ฒˆ๋งŒ ์‹คํ–‰ / ๐ŸŸก : ์š”์ฒญ์‹œ๋งˆ๋‹ค ๋ฐ˜๋ณต ์‹คํ–‰

 

*javaSE๋Š” Java ํ”Œ๋žซํผ์˜ ๊ธฐ๋ณธ ๋ฒ„์ „์œผ๋กœ, ์ผ๋ฐ˜์ ์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ์— ํ•„์š”ํ•œ ํ•ต์‹ฌ ๊ธฐ๋Šฅ๊ณผ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. JavaSE๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ Java ํ”Œ๋žซํผ์œผ๋กœ, ๋ชจ๋“  Java ๊ฐœ๋ฐœ์˜ ๊ธฐ๋ฐ˜์ด ๋œ๋‹ค. ์ด ๋ฒ„์ „์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์Šคํฌํ†ฑ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜, ์ฝ˜์†” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜, ์„œ๋ฒ„ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋“ฑ์„ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋‹ค.

 

SingleParamServlet.java

package com.ssafy.basic;

import java.io.IOException;
import java.io.PrintWriter;

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("/singleparam.ssafy")		
public class SingleParamServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("doGet call!!!!");
//		1. data get
		String name = request.getParameter("username");
		String id = request.getParameter("userid");
		int area = Integer.parseInt(request.getParameter("area"));
		
//		2. logic >> ์ธ์‚ฌ์˜ ๊ธ€์ž์ƒ‰์„ ์„œ์šธ : ํŒŒ๋ž‘, ๋Œ€์ „ : ์˜ค๋ Œ์ง€, ๊ตฌ๋ฏธ : ํ•‘ํฌ, ๊ด‘์ฃผ : ์ดˆ๋ก, ๋ถ€์šธ๊ฒฝ : ๋ณด๋ผ
		String color[] = {"blue", "orange", "pink", "green", "purple"};
		
//		3. response page
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("	<body>");
		out.println("	<h2 style=\"color:" + color[area] + "\">์•ˆ๋…•ํ•˜์„ธ์š”. " + name + "(" + id + ")๋‹˜!!!</h2>");
		out.println("	</body>");
		out.println("</html>");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("doPost call!!!!");
//		1. data get
		request.setCharacterEncoding("utf-8");		//doGet๋ถ€๋ถ„์ด๋ž‘ ์œ ์ผํ•˜๊ฒŒ ๋‹ค๋ฅธ ๊ฒƒ ๋‚ด๋ถ€์ ์œผ๋กœio๊ฐ€ ์ผ์–ด๋‚˜๊ธฐ๋•Œ๋ฌธ์— ์ธ์ฝ”๋”ฉ์„ค์ •, get์€ url๋กœ ๋„˜์–ด์˜ค๊ธฐ ๋•Œ๋ฌธ์— ใ„ฑใ…Š
		String name = request.getParameter("username");			//name์œผ๋กœ ๊ฐ€์ ธ์˜จ๋‹ค
		System.out.println(">>>>>>>>>>>>" + name);
		String id = request.getParameter("userid");
		int area = Integer.parseInt(request.getParameter("area"));
		
//		2. logic >> ์ธ์‚ฌ์˜ ๊ธ€์ž์ƒ‰์„ ์„œ์šธ : ํŒŒ๋ž‘, ๋Œ€์ „ : ์˜ค๋ Œ์ง€, ๊ตฌ๋ฏธ : ํ•‘ํฌ, ๊ด‘์ฃผ : ์ดˆ๋ก, ๋ถ€์šธ๊ฒฝ : ๋ณด๋ผ
		String color[] = {"blue", "orange", "pink", "green", "purple"};
		
//		3. response page
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("	<body>");
		out.println("	<h2 style=\"color:" + color[area] + "\">์•ˆ๋…•ํ•˜์„ธ์š”. " + name + "(" + id + ")๋‹˜!!!</h2>");
		out.println("	</body>");
		out.println("</html>");
	}

}

single.html

<!DOCTYPE html>
<html lang="ko">
<head>
	<title>SSAFY-ํŒŒ๋ผ๋ฏธํ„ฐ์ „๋‹ฌ</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>
	<div class="container">
		<div class="col-lg-5">
			<h5 class="text-center">Servlet์„ ์ด์šฉํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ ์ „๋‹ฌ(Single)</h5>
			<form name="paramform" method="post" action="/bw/singleparam.ssafy">
				<div class="form-group">
					<label for="userid">์•„์ด๋””:</label> <input type="text"
						class="form-control" placeholder="์•„์ด๋”” ์ž…๋ ฅ" name="userid">
				</div>
				<div class="form-group">
					<label for="username">์ด๋ฆ„:</label> <input type="text"
						class="form-control" placeholder="์ด๋ฆ„ ์ž…๋ ฅ" name="username">
				</div>
				<div class="form-group">
					<label for="area">์ง€์—ญ:</label> <select name="area"
						class="custom-select">
						<option value="0">์„œ์šธ</option>
						<option value="1" selected>๋Œ€์ „</option>
						<option value="2">๊ตฌ๋ฏธ</option>
						<option value="3">๊ด‘์ฃผ</option>
						<option value="4">๋ถ€์šธ๊ฒฝ</option>
					</select>
				</div>
				<button type="submit" class="btn btn-primary">์ „์†ก</button>
			</form>
		</div>
	</div>
</body>
</html>

 

 

 

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

 

Servlet (์„œ๋ธ”๋ฆฟ)์ดํ•ดํ•˜๊ธฐ + ์‹ค์Šต์˜ˆ์ œ

Servlet Servlet์ด๋ž€ ์ž๋ฐ”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›นํŽ˜์ด์ง€๋ฅผ ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•˜๋Š” ์„œ๋ฒ„์ธก ํ”„๋กœ๊ทธ๋žจ ํ˜น์€ ๊ทธ ์‚ฌ์–‘์„ ๋งํ•˜๋ฉฐ, ํ”ํžˆ "์„œ๋ธ”๋ฆฟ"์ด๋ผ ๋ถˆ๋ฆฐ๋‹ค. ์ž๋ฐ” ์„œ๋ธ”๋ฆฟ์€ ์›น ์„œ๋ฒ„์˜ ์„ฑ๋Š˜์„ ํ–ฅ์ƒํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š”

java-is-happy-things.tistory.com

 

728x90