๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ์–ธ์–ด/JSP

[JSP] ์„œ๋ธ”๋ฆฟ(Servlet) ์ด๋ž€?

by yunamom 2022. 3. 23.
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” yunamom ์ž…๋‹ˆ๋‹ค :D

 

์ด๋•Œ๊นŒ์ง€ ์„œ๋ธ”๋ฆฟ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  JSP ์— ๊ตญ๋น„์ง€์› ํ”„๋กœ์ ํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•˜์˜€๋Š”๋ฐ์š” (์˜ˆ: board.jsp → boardWrite.jsp)

 

์ค‘๋ณต์ฝ”๋“œ๋ฅผ ์—†์• ๊ณ , ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด ์•ž์œผ๋กœ์˜ ํ”„๋กœ์ ํŠธ๋Š” MVC ํŒจํ„ด์„ ํ†ตํ•ด ๊ตฌํ˜„ํ•  ๊ณ„ํš์ž…๋‹ˆ๋‹ค :)

 

JAVA๋ฅผ ์ด์šฉํ•˜์—ฌ ์›น ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐœ๋ฐœํ• ๋•Œ JSP(Java Server Pages)์™€ servlet(์„œ๋ธ”๋ฆฟ)์„ ๋งŽ์ด ์ด์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

์ด๋ฒˆํฌ์ŠคํŒ…์—์„œ๋Š” ์ดˆ๋ณด์ž์˜ ๊ด€์ ์œผ๋กœ ์„œ๋ธ”๋ฆฟ์—๊ด€ํ•˜์—ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 


โˆ™ servlet(์„œ๋ธ”๋ฆฟ) ์ด๋ž€?

์„œ๋ธ”๋ฆฟ์€ ์„œ๋ฒ„์— ๋™์ ์ธ ์ฝ˜ํ…์ธ ๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค.

 

Java ์„œ๋ฒ„์—์„œ ์›€์ง์ด๋Š” ์ž‘์€ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ JSP์˜ ๊ธฐ๋ฐ˜์ด ๋ฉ๋‹ˆ๋‹ค.

 

JSP๋Š” Java ์„œ๋ฒ„๊ฐ€ JSP ์ฝ”๋“œ๋ฅผ ์ฝ์–ด๋“ค์—ฌ ๊ทธ๊ฒƒ์„ ์„œ๋ธ”๋ฆฟ ์†Œ์Šค ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

HTML ํƒœ๊ทธ ๋“ฑ๋„ ๋ชจ๋‘ println์œผ๋กœ ์ถœ๋ ฅํ•˜๋„๋ก ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์ƒ์„ฑํ•œ ์„œ๋ธ”๋ฆฟ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ์ปดํŒŒ์ผํ•˜๊ณ , ์„œ๋ธ”๋ฆฟ์„ ์ƒ์„ฑํ•ด์„œ ๋ถˆ๋Ÿฌ์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

๐Ÿฅบ์ดˆ๋ณด์ž์˜ ๊ด€์ ์—์„œ(๋ณธ์ธ) ์ด๋ ‡๊ฒŒ ๊ธ€๋กœ๋งŒ ๋ณด์•„์„œ๋Š” ์„œ๋ธ”๋ฆฟ์„ ์ดํ•ดํ•˜๊ธฐ๊ฐ€ ํž˜๋“ ๋ฐ์š”

 

์•„๋ž˜์—์„œ ์„œ๋ธ”๋ฆฟ์„ ์–ด๋–ป๊ฒŒ ๋งŒ๋“œ๋Š”์ง€ ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


โˆ™ ์„œ๋ธ”๋ฆฟ ๋งŒ๋“ค๊ธฐ

 

package controller;

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;

/**
 * Servlet implementation class TestServlet
 */
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public TestServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html; charset=utf-8");
		System.out.println("yunamom");		
		response.getWriter().print("<h1>Hello World</h1>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}

๐Ÿ’ก์„œ๋ธ”๋ฆฟ ์ฃผ์˜์ 

์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด๋„ ๋ฐ”๋กœ ๋ฐ˜์˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

Eclipse์—์„œ๋Š” ์ˆ˜์ • ๋’ค์— ์กฐ๊ธˆ ๊ธฐ๋‹ค๋ฆฌ๋ฉด ์ฝ˜์†”์ฐฝ์— [๋กœ๋“œ๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.]๋ผ๊ณ  ๋‚˜์˜ต๋‹ˆ๋‹ค.

 

์ฝ˜์†”์ฐฝ์— ๋ฉ”์„ธ์ง€๊ฐ€ ๋‚˜์™”๋‹ค๋ฉด ๋ฐ˜์˜์ด ๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

์•„๋‹ˆ๋ฉด ์„œ๋ฒ„๋ฅผ ์žฌ๊ธฐ๋™ ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค

 

์˜ˆ) JSP

/* jsp์˜ ๊ฒฝ์šฐ */
<%
String name = "ํ™๊ธธ๋™";
%>
์•ˆ๋…•ํ•˜์„ธ์š”. <%= name %> ๋‹˜!

์˜ˆ) Servlet

/* ์„œ๋ธ”๋ฆฟ์˜ ๊ฒฝ์šฐ */
String name = "ํ™๊ธธ๋™";
/*  jsp์—์„œ ์“ธ๋•Œ๋Š” ์ž๋™์ƒ์„ฑ๋ฌ์ง€๋งŒ ์„œ๋ธ”๋ฆฟ์„ ์ด์šฉํ•  ๋•Œ๋Š” ๋ฐ‘์— ์„ธ๋ฌธ์žฅ์„ ์จ์ค˜์•ผํ•จ */
/* ์‘๋‹ต๋ฐฉ์‹๊ณผ ์‚ฌ์šฉํ•  ๋ฌธ์ž์…‹์„ ์„ค์ • */
response.setContentType("text/html");
/* html ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•œ ์ŠคํŠธ๋ฆผ์„ ์„ค์ • */
response.setCharacterEncoding("UTF-8");

PrintWriter out = response.getWriter();
out.print("์•ˆ๋…•" + name + "๋‹˜");
out.print("<hr>");
out.close();

์ด๋ ‡๊ฒŒ ์„œ๋ธ”๋ฆฟ์— ๊ด€ํ•ด ์•Œ์•„๋ณด์•˜๋Š”๋ฐ์š”,

 

์•„๋ฌด๋ž˜๋„ ์ฒ˜์Œ๋ถ€ํ„ฐ ์™„๋ฒฝํ•˜๊ฒŒ ์ดํ•ดํ•˜๊ธฐ๋Š” ํž˜๋“  ์•„์ด์ธ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป๊พธ์ค€ํ•˜๊ฒŒ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ํฌ์ŠคํŒ… ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ž์ฃผ ๋†€๋Ÿฌ์™€์ฃผ์„ธ์š” :D

300x250

์ฝ”๋“œ