MapleStory Finger Point

๐ŸŸค JAVA/๐ŸŸค JSP

[JSP] Java Servlet

HYEJU01 2024. 6. 27. 23:58

[์„œ๋ธ”๋ฆฟ servlet]
์ž๋ฐ” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•  ๋•Œ
์„œ๋ฒ„์ธก์—์„œ ์‹คํ–‰๋˜๋Š” ์ž๋ฐ” ํ”„๋กœ๊ทธ๋žจ์„ ๋œปํ•œ๋‹ค.

ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ณ , ๋™์  ์›น ์ฝ˜ํ…์ธ ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

https://www.developier.com/2022/06/servlet.html#google_vignette




[์„œ๋ธ”๋ฆฟ servlet ํŠน์ง•]
1. ๋™์  ์›น์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปดํฌ๋„ŒํŠธ (์ˆœ์ˆ˜ ์ž๋ฐ”์ฝ”๋“œ)
2. .java ํ™•์žฅ์ž
3. ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์— ๋™์ ์œผ๋กœ ์ž‘๋™ํ•˜๊ณ , ์‘๋‹ต์€ html์„ ์ด์šฉ.
4. java thread๋ฅผ ์ด์šฉํ•˜์—ฌ ๋™์ž‘.
5. MVCํŒจํ„ด์—์„œ Controller๋กœ ์ด์šฉ๋จ

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException {
    response.setContentType("text/html; charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println("<body>");
    out.println("์˜ค ์ด๊ฒŒ ์„œ๋ธ”๋ฆฟ์ด์•ผ?");
    out.println("</body>");
  }

 

 

[ ์„œ๋ธ”๋ฆฟ ๊ตฌ๋ถ„ ๋ฐฉ๋ฒ• ]

์„œ๋ธ”๋ฆฟ์€ ์—ฌ๋Ÿฌ๊ฐœ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—,  ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์–ด์•ผํ•œ๋‹ค.

 

 


1) URL-Mapping
url ๋งคํ•‘์€ url ์ฃผ์†Œ ๊ธธ์–ด์ง ๋ฐฉ์ง€ / ๋ณด์•ˆ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

 

//์–ด๋…ธํ…Œ์ด์…˜์„ ์ด์šฉํ•˜์—ฌ ํด๋ž˜์Šค ์„ ์–ธ๋ถ€ ๋ฐ”๋กœ ์œ„์— ์ž‘์„ฑ
ex) @WebServlet("/HelloWorld")

 



2) web.xml ์„ค์ •ํŒŒ์ผ ์ˆ˜์ •

 

 


[ HttpRequest ๋ฐฉ์‹ ]

ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„์˜ ์ƒํ˜ธ์ž‘์šฉ ๋ฐฉ์‹์œผ๋กœ get ๋ฐฉ์‹๊ณผ post ๋ฐฉ์‹์ด ์žˆ๋‹ค.

 

 


[ GET ๋ฐฉ์‹ ]

  • ์„œ๋ฒ„์— ๋ฐ์ดํ„ฐ ์š”์ฒญ ์šฉ๋„
  • ๋ฐ์ดํ„ฐ๊ฐ€ ์ฃผ์†Œ์— ๋ฌป์–ด๊ฐ„๋‹ค. => ๋ณด์•ˆ ์ทจ์•ฝ
  • formํƒœ๊ทธ ๊ฐ€ ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•˜์ง€๋Š” ์•Š์Œ
    ์ „์†กํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ํฌ๊ธฐ๋Š” ๋ธŒ๋ผ์šฐ์ €๋ณ„๋กœ ๋‹ค๋ฅด์ง€๋งŒ ํฌ๊ธฐ๊ฐ€ ์ •ํ•ด์ง
  • ๊ฒŒ์‹œํŒ ๊ธ€ ์กฐํšŒ๋‚˜ ๊ฒ€์ƒ‰ ๊ฐ™์ด ์„œ๋ฒ„์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ฌ ํ•„์š”์„ฑ์ด ์žˆ์„ ๋•Œ ์‚ฌ์šฉ

 


[ POST ๋ฐฉ์‹ ]

  • ์„œ๋ฒ„์— ๋ฐ์ดํ„ฐ ์ „์†ก ์šฉ๋„
  • ๋ฐ์ดํ„ฐ๊ฐ€ ์ฃผ์†Œ์— ๋ฌป์ง€ ์•Š๊ณ , ๊ฐ์ฒด์˜ ๋ฐ”๋””๋ฅผ ํ†ตํ•ด ์ „๋‹ฌ๋จ. => ๋ณด์•ˆ ๊ฐ•ํ•จ
  • form ํƒœ๊ทธ ๊ฐ€ ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•จ
  • ๋ฐ์ดํ„ฐ ์–‘์˜ ์ œํ•œ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†ก
  • ๋น„๋ฐ€๋ฒˆํ˜ธ๋‚˜ ์ฃผ๋ฏผ๋ฒˆํ˜ธ ๋“ฑ privateํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„์— ์ „์†กํ•ด์•ผ ํ•  ๋•Œ ์‚ฌ์šฉ

 

 

[ GET/ POST ๋ฐฉ์‹ ๋ธŒ๋ผ์šฐ์ € ํ•œ๊ธ€์ฒ˜๋ฆฌ ]