View

[Web] Servlet ๊ธฐ์ดˆ 1

์ฑ…์ฝ๋Š” ๊ฐ์ž 2022. 9. 15. 19:05
๋ฐ˜์‘ํ˜•

 ๐Ÿ’ก Servlet์ด๋ž€?

์ž๋ฐ” ์„œ๋ธ”๋ฆฟ์€ ์ž๋ฐ”๋ฅผ ์ด์šฉํ•ด์„œ ์›นํŽ˜์ด์ง€๋ฅผ ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•˜๋Š” ์„œ๋ฒ„์ธก ํ”„๋กœ๊ทธ๋žจ์„ ๋งํ•˜๊ณ 

์ž๋ฐ” ์„œ๋ธ”๋ฆฟ์€ ์›น ์„œ๋ฒ„์˜ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ์ž๋ฐ” ํด๋ž˜์Šค์˜ ์ผ์ข…์ด๋‹ค

์„œ๋ธ”๋ฆฟ์€ JSP์™€ ๋‹ฌ๋ฆฌ ์ž๋ฐ” ์ฝ”๋“œ ์•ˆ์— HTML์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค(JSP๋Š” ๊ทธ ๋ฐ˜๋Œ€์ž„ )

 


 

 

public class myServlet extends HttpServlet {

    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("init method ํ˜ธ์ถœ!");
    }
    
    @Override
    public void destroy() {
        System.out.println("destroy method ํ˜ธ์ถœ!");
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        System.out.println("doGet service method ํ˜ธ์ถœ!");		
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        System.out.println("doPost service method ํ˜ธ์ถœ!");		
    }
	
}

init()

์„œ๋ธ”๋ฆฟ์ด ์ฒ˜์Œ์œผ๋กœ ์š”์ฒญ๋  ๋•Œ ์ดˆ๊ธฐํ™”๋ฅผ ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค. ํด๋ž˜์Šค๋ฅผ new ํ•ด์„œ ์‚ฌ์šฉํ•˜๋“ฏ ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๋„ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ์–ด์•ผ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ด๋ ‡๊ฒŒ ์ดˆ๊ธฐํ™”๋œ ์„œ๋ธ”๋ฆฟ์€ ์‹ฑ๊ธ€ํ†ค์œผ๋กœ ๊ด€๋ฆฌ๋˜์–ด ๋‹ค์Œ์— ํ•œ๋ฒˆ ๋” ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์ดˆ๊ธฐํ™”๊ฐ€ ๋‹ค์‹œ ์ผ์–ด๋‚˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ธฐ์กด์— ์žˆ๋˜ ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

service()

์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์š”์ฒญ์„ ๋ฐ›๊ณ  ์‘๋‹ต์„ ๋‚ด๋ ค์ค„ ๋•Œ ํ•„์š”ํ•œ ์„œ๋ธ”๋ฆฟ์˜ service ๋ฉ”์„œ๋“œ์ด๋‹ค. Servlet interface๋ฅผ ๊ตฌํ˜„ํ•œ HttpServlet ํด๋ž˜์Šค์˜ doGet, doPost ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋“ค์ด ํ˜ธ์ถœ๋œ๋‹ค.

destroy()

๋” ์ด์ƒ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๋Š” ์ฃผ๊ธฐ์ ์œผ๋กœ ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ destory() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ œ๊ฑฐํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ์ œ๊ฑฐ๋œ ์„œ๋ธ”๋ฆฟ์€ service ๋ฉ”์„œ๋“œ๋“ค์— ํ•ด๋‹นํ•˜๋Š” ๋ชจ๋“  ์Šค๋ ˆ๋“œ๋“ค์ด ์ข…๋ฃŒ๋˜๊ฑฐ๋‚˜ ์‚ฌ์šฉ์‹œ๊ฐ„์ด ์˜ค๋ž˜๋˜์–ด ํƒ€์ž„์•„์›ƒ๋œ ๊ฒฝ์šฐ์—๋Š” ์ด ํด๋ž˜์Šค๋ฅผ ๋‹ค์‹œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” init()์„ ๋‹ค์‹œ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

์ถœ์ฒ˜: https://coding-factory.tistory.com/742


๐Ÿ’ก ์‹ค์Šต1

package com.ssafy.hello;

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("/Hello")
public class HelloServelt extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}

}

 

@WebServlet("/Hello"): url ๋ณ€๊ฒฝ์‹œ ๋ฐ”๋กœ ์ ์šฉ์ด ์•ˆ๋˜๊ธฐ ๋–„๋ฌธ์— ๋ญ˜ ํ™•์ธํ•ด์•ผํ•˜๋ƒ๋ฉด

๋‹ค์‹œ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ์„ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค 

๋‹ค์‹œ๋ณด๋ฉด 404ํŽ˜์ด์ง€ ์—ด๋‹ค๊ฐ€ ์ผ์ • ์‹œ๊ฐ„ ์ง€๋‚˜๋ฉด ๋‹ค์‹œ ์—ด๋ฆผ 

๋น ๋ฅธ ์ ์šฉ์„ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์„œ๋ฒ„ ๊ป๋‹ค ์ผœ๋Š” ๊ฒƒ์„ ์ถ”์ฒœ

 


Java ์ฝ”๋“œ ์•ˆ์—์„œ HTML์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด -> ์ถœ๋ ฅ ๊ฐ์ฒด๋ฅผ ์–ป์–ด์™€์•ผํ•จ

 

๊ทธ๋ ‡๋‹ค๋ฉด ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ์ƒ์„ฑ ํ›„ ๋„˜๊ฒจ์ค„ ์ˆ˜ ์žˆ๋‚˜? ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ํ•œ๊ธ€์€ ๊นจ์ง€๊ฒŒ ๋œ๋‹ค

์ด์ชฝ์—์„œ ๋„˜๊ฒจ์ค„ ๋•Œ๋Š” " text(๋ฌธ์ž์—ด)๋กœ ๋„˜๊ฒจ์ฃผ๊ณ , ๋ธŒ๋ผ์šฐ์ €๋Š” html๋กœ ํ•ด์„ํ•˜๋Š”๋ฐ ๋ฌธ์ž๋“ค์€ utf-8๋กœ ๋„˜๊ธด๋‹ค " ๋ผ๋Š” ๋œป

 

๋ฐ˜์‘ํ˜•
Share Link
reply
๋ฐ˜์‘ํ˜•
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30