์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

[Day23 | WebUI] Servlet

๊น€์œ ๋‹ˆ์ฝ˜ 2021. 12. 13. 17:52

์–ดํŒŒ์น˜ ํ†ฐ์บฃ ์„ค์ • 


 

 

 

static final : ์ƒ์ˆ˜ 

 

 

 

์™œ ํ†ฐ์บฃ์— ์˜ฌ๋ผ๊ฐ€? 

url ๋งคํ•‘

ํ†ฐ์บฃ์—์„œ ๋Œ๋•Œ์—๋Š” init๊ฐ€ ํ˜ธ์ถœ์ด ๋จ. 

์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ์š”์ฒญํ•˜๋Š” ๊ฒƒ์€ ์ง€๊ธˆ ํ˜„์žฌ get๋ฐ–์— ์—†์Œ --> get ์‹คํ–‰

๊ณ„์† ์ƒˆ๋กœ๊ณ ์นจ์„ ํ•  ๋•Œ๋งˆ๋‹ค, 

์›น๋ธŒ๋ผ์šฐ์ €์— doGet์ด ํ˜ธ์ถœ๋˜์–ด์„œ count๊ฐ€ ํ•˜๋‚˜์”ฉ ์ฆ๊ฐ€. 

 

์„œ๋ฒ„ ์ข…๋ฃŒํ•˜๋ฉด destory๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด์„œ

ํ†ฐ์บฃ์—์„œ ๋‚ด๋ ค๊ฐ€๊ฒŒ ๋จ. 

 

 

 

OS๊ฐ€ ์žˆ๋‹ค. (Java)

์ด๊ฑธ JVM์— ์˜ฌ๋ฆฐ๋‹ค. (class๋กœ ์˜ฌ๋ผ๊ฐ„๋‹ค, class๋Š” ์‹ค์งˆ์ ์œผ๋กœ ํž™ ์Šคํƒ ๋“ฑ์œผ๋กœ ๊ตฌ์„ฑ. ๋ผ์ดํ”„ ์‚ฌ์ดํด์ด ํ˜ธ์ถœ์ด ๋˜์–ด ํž™ ๊ณต๊ฐ„์— ๋ฌด์งˆ์„œํ•˜๊ฒŒ ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ.)

 

 

 

 

 

LifeCycle.java


import java.io.IOException;
import javax.servlet.ServletConfig;
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 LifeCycle
 */
@WebServlet("/LifeCycle")
public class LifeCycle extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	int initCount = 1; 
	int doGetCount = 1; 
	int destroyCount = 1; 
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LifeCycle() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		System.out.println("init ํ˜ธ์ถœ: "+ initCount++);
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		System.out.println("destroy ํ˜ธ์ถœ: "+ destroyCount++);	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("LifeCycle Servlet Test").append(request.getContextPath());
		System.out.println("doGet ํ˜ธ์ถœ: "+ doGetCount++);	}

	}

 

 

 

 

 

 

 

 

SumtestServlet.java



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;

/**
 * Servlet implementation class SumTestServlet
 */
@WebServlet("/SumTestServlet")
public class SumTestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SumTestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		int n1 = Integer.parseInt(request.getParameter("a")); // ์›น์—์„œ ๋“ค์–ด์˜ค๋Š” ๊ฒƒ์€ ๋Œ€๋ถ€๋ถ„string, ๊ทธ๋Ÿฌ๋ฏ€๋กœ int๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” integer.parseInt
		int n2 = Integer.parseInt(request.getParameter("b"));
		
		int nSum = n1 + n2; 
		
		response.setContentType("text/html; charset=UTF-8");
		
		//web์„œ๋ฒ„์— ๋„์šฐ๋Š” ๋ช…๋ น 
		PrintWriter out = response.getWriter();
		out.println("<h1><font color=\"blue\"> get ๋ฐฉ์‹์˜ [๋ง์…ˆ ๊ฒฐ๊ณผ] : " + nSum + "</font>");
		out.close(); 
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		int n3 = Integer.parseInt(request.getParameter("c")); // ์›น์—์„œ ๋“ค์–ด์˜ค๋Š” ๊ฒƒ์€ ๋Œ€๋ถ€๋ถ„string, ๊ทธ๋Ÿฌ๋ฏ€๋กœ int๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” integer.parseInt
		int n4 = Integer.parseInt(request.getParameter("d"));
		
		int nSub = n3 - n4; 
		
		response.setContentType("text/html; charset=UTF-8");
		
		//web์„œ๋ฒ„์— ๋„์šฐ๋Š” ๋ช…๋ น 
		PrintWriter out = response.getWriter();
		out.println("<h1><font color=\"red\"> post ๋ฐฉ์‹์˜ [๋บ„์…ˆ ๊ฒฐ๊ณผ] : " + nSub + "</font>");
		out.close(); 
	}

}

 

SumTest.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<center>
	<form method="get" action="SumTestServlet">
	<ul> 
		<li> <label> ์ฒซ๋ฒˆ์งธ ์ˆซ์ž : </label> <input name ="a"/> </li>
		<li> <label> ๋‘๋ฒˆ์งธ ์ˆซ์ž : </label> <input name ="b"/> </li>
	</ul>
	<input type="submit" value = "๋ง ์…ˆ">
	</form>
	
	<form method="post" action="SumTestServlet">	
	<ul> 
		<li> <label> ์ฒซ๋ฒˆ์งธ ์ˆซ์ž : </label> <input name ="c"/> </li>
		<li> <label> ๋‘๋ฒˆ์งธ ์ˆซ์ž : </label> <input name ="d"/> </li>
	</ul>
	<input type="submit" value = "๋บ„ ์…ˆ">
	</center>
	</form>
	
	
</body>
</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

๋ˆˆ์—์„œ ํ•œ ์ค„ ํ•œ ์ค„ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๋ฉฐ, 

์ด๊ฑด ์ด๋ฏธ ์•ž ์ „3์ฃผ๋™์•ˆ์˜ ์ˆ˜์—…์—์„œ ์ต์ˆ™ํ•ด์กŒ์–ด์•ผ ํ•œ๋‹ค๊ณ  ํ•˜๋Š”๋ฐ

 

์ด๊ฑฐ ์™„์ „ ํ•œ์ž ์‚ผ๋งŒ๊ฐœ ์•Œ๋ ค์ฃผ๊ณ  

3์ฃผ ๋’ค์— ์™œ ํ•œ์ž ์•Œ๋ ค์คฌ๋Š”๋ฐ ์ค‘๊ตญ์–ด ์ฑ… ์ˆ ์ˆ  ๋ชป์ฝ๋ƒ๊ณ  ํ•˜๋Š” ๊ฒƒ๊ณผ ์ง„๋ฐฐ์—†๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ..^-ใ… 

์‹œ๊ฐ„์ด ํ•„์š”ํ•ด์š”... 

 

 

 

 

๋‚˜๋Š” ์˜ค๋Š˜ ์‚ฌ๋žŒ๋“ค์ด ํ—ค๋งธ๋˜ ๋ถ€๋ถ„์˜ ์˜ค๋ฅ˜๋“ค์„ 

๋ชจ๋‘ ๊ฐ€์ง€๊ณ  ์žˆ์—ˆ๊ณ , 

์งˆ๋ฌธ์„ ํ•ด์ฃผ์–ด์„œ ๊ณ ๋งˆ์› ๊ณ 

ํ˜ผ์ž์„œ ์‚ฝ์งˆ๋„ ๋งŽ์ด ํ–ˆ๋‹ค

 

 

 

 

๊ณผ์ œ1. : 

- Servlet ๊ฐ์ฒด์™€ JSP ๋‚ด์žฅ๊ฐ์ฒด (๋จผ์ €์กฐ์‚ฌ)

๊ณผ์ œ2 :

- Param.html

- ParamServlet.java

- Param.js 


 

์ดํ•˜ ์„ค์ •์—์„œ ์ž๊พธ ์˜ค๋ฅ˜๊ฐ€ ๋‚ฌ์„ ๋•Œ ๋„์™€์ค€ ๊ตฌ๊ธ€็ฅž ์‚ฌ๋ž‘ํ•ด์š”♥๏ธ: 

 

์ดํด๋ฆฝ์Šค์—์„œ Dynamic Web Project ๊ฐ€ ์•ˆ๋ณด์ผ ๋•Œ: 

https://ssimplay.tistory.com/204

 

[Java] ์ดํด๋ฆฝ์Šค์— Dynamic Web Project ์—†์„ ๋•Œ

์•ˆ๋…•ํ•˜์„ธ์š”. ํ”Œ์ž…๋‹ˆ๋‹ค. ์ดํด๋ฆฝ์Šค์—์„œ ์›น ํ”„๋กœ์ ํŠธ( Dynamic Web Project )๊ฐ€ ์—†์„ ๋•Œ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณผ๊ฒŒ์š”.  1. Help ์›น ํ”„๋กœ์ ํŠธ๋Š” ์„ค์น˜ํ•ด์•ผ์ง€ ๋ณด์ธ๋‹ค๊ณ  ํ•˜๋„ค์š”. Help > Instal New Software๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”.

ssimplay.tistory.com

 

์ดํด๋ฆฝ์Šค์—์„œ ํ†ฐ์บฃ ์„œ๋ฒ„ ๋ชฉ๋ก์ด ์•ˆ๋ณด์ผ ๋•Œ: 

https://zawook.postype.com/post/7212270

 

Tip : Eclipse์—์„œ Tomcat ์„œ๋ฒ„ ๋ชฉ๋ก์ด ์•ˆ ๋ณด์ผ ๋•Œ

Issue ํ†ฐ์บฃ ์„œ๋ฒ„๋ฅผ ์ƒˆ๋กœ ์„ค์น˜ํ•˜๊ณ  ๋‚˜๋ฉด, ์ดํด๋ฆฝ์Šค์™€ ์—ฐ๋™์„ ์‹œ๋„ํ•  ๋•Œ ์„œ๋ฒ„ ๋ฆฌ์ŠคํŠธ์—์„œ ์•„ํŒŒ์น˜ ๊ด€๋ จ ๋ชฉ๋ก์ด ์‚ฌ๋ผ์ง€๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. Solution ์ด ๊ฒฝ์šฐ JTS Server Adapters๋ฅผ ๊ฐฑ์‹ ํ•ด ์ค˜์•ผ ํ•œ๋‹ค. ์ดํด๋ฆฝ์Šค ๋ฉ”

zawook.postype.com

 

 

 

 

์•„๋ž˜ ๋ถ€๋ถ„ ์—๋Ÿฌ ํ•ด๊ฒฐํ•˜๊ธฐ: 

 

https://blog.csdn.net/HYD696/article/details/104674742

 

“javax.servlet.http.HttpServlet” was not found on the Java Build Path_่‰บๅšไธœ็š„ๅšๅฎข-CSDNๅšๅฎข

 

blog.csdn.net

 

 

 

์‚ฌ์šฉ์ค‘์ธ port ์ฐพ์•„์„œ kill ํ•˜๊ธฐ :
https://88240.tistory.com/475

 

[MAC] ์‚ฌ์šฉ์ค‘์ธ ํฌํŠธ ์ฐพ์•„์„œ, Kill ํ•˜๊ธฐ

์ฐพ๊ธฐ lsof -i :ํฌํŠธ๋ฒˆํ˜ธ ex. lsof -i :8000 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME node 1234 shaking 15u IPv4 0x1f23462a48d69d65 0t0 TCP localhost:cslistener (LISTEN) Kill ํ•˜๊ธฐ kill -9 ex...

88240.tistory.com