๐Ÿ’ป

JSP ํ”„๋กœ๊ทธ๋ž˜๋ฐ - [Servlet] Servlet(์„œ๋ธ”๋ฆฟ) (1) ๋ณธ๋ฌธ

KITRI/JSP

JSP ํ”„๋กœ๊ทธ๋ž˜๋ฐ - [Servlet] Servlet(์„œ๋ธ”๋ฆฟ) (1)

๋˜ํšจ๋‹ˆ 2020. 6. 5. 13:35

Servlet

 

์„œ๋ธ”๋ฆฟ์ด๋ž€? ์ž๋ฐ” ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ตฌ์„ฑ์š”์†Œ ์ค‘ ๋™์ ์ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์˜ ์—ญํ• 

  • ์„œ๋ธ”๋ฆฟ์€ WAS์— ๋™์ž‘ํ•˜๋Š” JAVA ํด๋ž˜์Šค์ด๋‹ค.
  • ์„œ๋ธ”๋ฆฟ์€ HttpServlet ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„์•ผ ํ•œ๋‹ค.
  • ์›น ํŽ˜์ด์ง€๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ํ™”๋ฉด(HTML)์€ JSP๋กœ ํ‘œํ˜„ํ•˜๊ณ , ๋ณต์žกํ•œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ๊ตฌํ˜„ํ•œ๋‹ค.

https://javaee.github.io/javaee-spec/javadocs/

 

Java(TM) EE 8 Specification APIs

 

javaee.github.io

 

Q. ์–ธ์ œ ์„œ๋ฒ„๋ฉ”๋ชจ๋ฆฌ์—์„œ ํŒŒ์ผ์ด ๋‚ด๋ ค์˜ฌ๊นŒ?! 

 

1. ์„œ๋ฒ„๋ฅผ ๊บผ๋ฒ„๋ ธ์„ ๋•Œ

2. ์†Œ์Šค๋ฅผ ๋ณ€๊ฒฝํ•ด์„œ ์—…๋ฐ์ดํŠธ ํ• ๋•Œ

 

์„œ๋ฒ„๋Š” ํŒŒ์ผ๋‹จ์œ„์ด๋‹ค. ํ•˜๋‚˜์˜ ์ปจํ…Œ์ด๋„ˆ ์•ˆ์— ํ•˜๋‚˜์˜ ์ปจํ…์ŠคํŠธ(Context)๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. servlet, java, html, css, javascript ... ๋“ฑ๋“ฑ ํŒŒ์ผ์ด ์˜ฌ๋ผ๊ฐ€์žˆ๋‹ค.

 

 


Servlet ์ƒ๋ช…์ฃผ๊ธฐ

 

  • ์ƒ์„ฑ init() -> ์„œ๋น„์Šค service() -> ์†Œ๋ฉธ destroy()
  • WAS๋Š” ์„œ๋ธ”๋ฆฟ ์š”์ฒญ์„ ๋ฐ›์œผ๋ฉด ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ์ด ๋ฉ”๋ชจ๋ฆฌ์— ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

https://javaee.github.io/javaee-spec/javadocs/

 

Java(TM) EE 8 Specification APIs

 

javaee.github.io

 

 

Request, Response ๊ฐ์ฒด ์ดํ•ดํ•˜๊ธฐ

 

  • ์š”์ฒญ๊ณผ ์‘๋‹ต

    • WAS๋Š” ์›น ๋ธŒ๋ผ์šฐ์ €๋กœ๋ถ€ํ„ฐ Servlet์š”์ฒญ์„ ๋ฐ›์œผ๋ฉด,

    • ์š”์ฒญํ•  ๋•Œ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ •๋ณด๋ฅผ HttpServletRequest๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์ €์žฅํ•œ๋‹ค.

    • ์›น ๋ธŒ๋ผ์šฐ์ €์—๊ฒŒ ์‘๋‹ต์„ ๋ณด๋‚ผ ๋•Œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•˜์—ฌ HttpServletResponse๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

    • ์ƒ์„ฑ๋œ HttpServletRequest, HttpServletResponse ๊ฐ์ฒด๋ฅผ ์„œ๋ธ”๋ฆฟ์—๊ฒŒ ์ „๋‹ฌํ•œ๋‹ค.

    • ์›น ๋ธŒ๋ผ์šฐ์ €๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด request, response ๋ฅผ ์‚ฌ์šฉ
  • HttpServletRequest

    • httpํ”„๋กœํ† ์ฝœ์˜ request์ •๋ณด๋ฅผ ์„œ๋ธ”๋ฆฟ์—๊ฒŒ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

    • ํ—ค๋”์ •๋ณด, ํŒŒ๋ผ๋ฏธํ„ฐ, ์ฟ ํ‚ค, URI, URL ๋“ฑ์˜ ์ •๋ณด๋ฅผ ์ฝ์–ด ๋“ค์ด๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

    • Body์˜ Stream์„ ์ฝ์–ด ๋“ค์ด๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

  • HttpServletResponse

    • WAS๋Š” ์–ด๋–ค ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญ์„ ๋ณด๋ƒˆ๋Š”์ง€ ์•Œ๊ณ  ์žˆ๊ณ , ํ•ด๋‹น ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ต์„ ๋ณด๋‚ด๊ธฐ ์œ„ํ•œ HttpServleResponse๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์„œ๋ธ”๋ฆฟ์—๊ฒŒ ์ „๋‹ฌํ•œ๋‹ค.

    • ์„œ๋ธ”๋ฆฟ์€ ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ content type, ์‘๋‹ต์ฝ”๋“œ, ์‘๋‹ต ๋ฉ”์‹œ์ง€๋“ฑ์„ ์ „์†กํ•œ๋‹ค.


web.xml ํŒŒ์ผ ์„ค์ • : ํ”„๋กœ์ ํŠธํด๋” -> ์˜ค๋ฅธ์ชฝ ๋งˆ์šฐ์Šค -> Java EE tools -> generate... ์„ ํƒํ•˜๋ฉด ์ƒ์„ฑ๋œ๋‹ค. 

 

 

 

01_URL.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
   <!-- GET ๋ฐฉ์‹ -->
   <a href="http://localhost:8181/webTesting/com/java/servlet/Example01">
 	Example01
   </a>
   <br /><br />
   <a href="http://localhost:8181/webTesting/com/java/servlet/Example02?su=10">
   	Example02
   </a>
</body>
</html>

 

 

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
  <display-name>webTesting</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
	<servlet-name>Example01</servlet-name>
	<servlet-class>com.java.servlet.Example01</servlet-class>	
  </servlet>
  
  <servlet>
	<servlet-name>Example02</servlet-name>
	<servlet-class>com.java.servlet.Example02</servlet-class>	
  </servlet>

  <servlet-mapping>
	<servlet-name>Example01</servlet-name>
	<url-pattern>/com/java/servlet/Example01</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
	<servlet-name>Example02</servlet-name>
	<url-pattern>/com/java/servlet/Example02</url-pattern>
  </servlet-mapping>
</web-app>

mapping์—ฐ๊ฒฐ html๊ณผ servlet์„ ์—ฐ๊ฒฐ

 

 

 

Example01.java

package com.java.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Example01 extends HttpServlet{
	private static final long serialVersionUID = 1L;
	
	// ์„œ๋ฒ„ ๋ฉ”๋ชจ๋ฆฌ : Content ๋‹จ์œ„๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค. ๊ฐ๊ฐ์˜ Content ์•ˆ์— Context(webTestingํ”„๋กœ์ ํŠธ)๊ฐ€ ์žˆ๋‹ค.
	public Example01() {
		super();
	}
	
	// ์ƒ๋ช…์ฃผ๊ธฐ - ์ดˆ๊ธฐํ™” init() -> ์„œ๋น„์Šค service() -> ์†Œ๋ฉธ destroy() 
	public void init() {
		System.out.println("์„œ๋ธ”๋ฆฟ ํŒŒ์ผ ์ดˆ๊ธฐํ™”(์ฒ˜์Œ ์š”์ฒญ ์‹œ ๋‹จ ํ•œ๋ฒˆ)");
	}
	
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		// ์„œ๋น„์Šค - ์š”์ฒญ๋ฐฉ์‹์— ์˜ํ•ด์„œ get, post ํ•จ์ˆ˜ ํ˜ธ์ถœ
		doGet(request, response);
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse response) {
		System.out.println("...์„œ๋ธ”๋ฆฟ ๋‚ด์šฉ doGet ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.");
	}
	
	public void destroy() {
		System.out.println("์„œ๋ธ”๋ฆฟ ํŒŒ์ผ ์†Œ๋ฉธ");
	}	
}

javaํŒŒ์ผ ์ˆ˜์ • ํ›„์— ๋‹ค์‹œ ๋กœ๋“œํ•˜๋ฉด์„œ destroy() ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜์–ด์„œ ํŒŒ์ผ์ด ์†Œ๋ฉธ๋œ๋‹ค.

์›น ํŽ˜์ด์ง€์˜ ์š”์ฒญ์€ ๋‚ด๋ถ€ ์“ฐ๋ ˆ๋“œ, service() ๋‚ด๋ถ€์ ์œผ๋กœ ์“ฐ๋ ˆ๋“œ ์ฒ˜๋ฆฌ -> ํ†ฐ์บฃ์ด ์•Œ์•„์„œ ์ฒ˜๋ฆฌ

init(), service(), destroy() ๋Š” ์ž‘์„ฑํ•˜์ง€ ์•Š์œผ๋ฉด ๋””ํดํŠธ๋กœ ๋‚ด๋ถ€์ ์œผ๋กœ ์‹คํ–‰๋œ๋‹ค.

init() ๋ฉ”์†Œ๋“œ์˜ ๊ฒฝ์šฐ ์ดˆ๊ธฐํ™”ํ• ๋•Œ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด ์ž‘์„ฑํ•œ๋‹ค.

 

 

 

Example02.java

package com.java.servlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Example02 extends HttpServlet{
	private static final long serialVersionUID = 1L;

	// init()-service()-doGet()-destroy() 
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		String su = request.getParameter("su");
		System.out.println("su:" + su);
		
		// ๋ฌธ์ž์—ด์„ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜
		int value = Integer.parseInt(su);
		String a = String.valueOf(value);
		
		// ์ •์ˆ˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜
		Integer i = value;
		String b = i.toString();
		
		System.out.println(a +"\t" + b);
	}
}

 

 


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
  <display-name>webTesting</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
	<servlet-name>Example01</servlet-name>
	<servlet-class>com.java.servlet.Example01</servlet-class>	
  </servlet>

  <servlet-mapping>
	<servlet-name>Example01</servlet-name>
	<url-pattern>/com/java/servlet/Example01</url-pattern>
  </servlet-mapping>
  
  <servlet>
	<servlet-name>Example02</servlet-name>
	<servlet-class>com.java.servlet.Example02</servlet-class>	
  </servlet>

  <servlet-mapping>
	<servlet-name>Example02</servlet-name>
	<url-pattern>/com/java/servlet/Example02</url-pattern>
  </servlet-mapping>
  
  
  <servlet>
	<servlet-name>Example03</servlet-name>
	<servlet-class>com.java.servlet.Example03</servlet-class>	
  </servlet>

  <servlet-mapping>
	<servlet-name>Example03</servlet-name>
	<url-pattern>/com/java/servlet/Example03</url-pattern>
  </servlet-mapping>
  
  <servlet>
	<servlet-name>Example04</servlet-name>
	<servlet-class>com.java.servlet.Example04</servlet-class>	
  </servlet>

  <servlet-mapping>
	<servlet-name>Example04</servlet-name>
	<url-pattern>/com/java/servlet/Example04</url-pattern>
  </servlet-mapping>
  
  
</web-app>

 

 

 

03_example.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="http://localhost:8181/webTesting/com/java/servlet/Example03" method="post">
		<label>๋ฉ”์„ธ์ง€</label>
		<input type="text" name="msg" />
		<br /><br />
		
		<input type="submit" value="์ „์†ก"/>
		<input type="reset" value="์ทจ์†Œ" />
	</form>
</body>
</html>

 

 

 

Example03.java

package com.java.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Example03 extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public Example03() {
		super();
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String msg = req.getParameter("msg");
		System.out.println(msg);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String msg = req.getParameter("msg");
		System.out.println(msg);
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("service() ์ƒ๋žต๊ฐ€๋Šฅ, ์Šค๋ ˆ๋“œ ์ฒ˜๋ฆฌ๋˜์„œ ๋ฐ˜๋ณต ์‹คํ–‰");
		//doGet(req, resp);
		
		doPost(req, resp);
	}

	@Override
	public void destroy() {
		System.out.println("destroy() ์ƒ๋žต๊ฐ€๋Šฅ, ์„œ๋ฒ„์ข…๋ฃŒ ๋ฐ ์—…๋ฐ์ดํŠธ ์‹œ");
	}

	@Override
	public void init() throws ServletException {
		System.out.println("init() ์ƒ๋žต๊ฐ€๋Šฅ, ์„œ๋ฒ„๋ฉ”๋ชจ๋ฆฌ์— ์ฒ˜์Œ์—๋งŒ ์‹คํ–‰๋œ๋‹ค.");
	}
	

}

 

 

04_example.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="http://localhost:8181/webTesting/com/java/servlet/Example04" method="get">
		<label>์ด๋ฆ„</label>
		<input type="text" name="name"/>
		<br /><br />
		
		<label>์ „ํ™”๋ฒˆํ˜ธ</label>
		<input type="text" name="phone"/>
		<br /><br />
		
		<label>์ฃผ์†Œ</label>
		<input type="text" name="addr" size="100"/>
		<br /><br />
		
		<input type="submit" value="์ „์†ก"/>
		<input type="reset" value="์ทจ์†Œ" />
	</form>
</body>
</html>

 

 

 

Example04.java

package com.java.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Example04 extends HttpServlet{
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		
		String name = req.getParameter("name");
		String phone = req.getParameter("phone");
		String addr = req.getParameter("addr");
		
		System.out.println(name+ "\t" + phone + "\t" + addr);
	}
	
}

 


05_example.html

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	function checkForm(obj){
		if(obj.buho.value != "+" & obj.buho.value !="-" & obj.buho.value !="*" & obj.buho.value !="/"){
			alert("๋ถ€ํ˜ธ๋ฅผ ์ž˜๋ชป ์ž…๋ ฅํ•˜์…จ์Šต๋‹ˆ๋‹ค.");
			obj.buho.focus();
			return false;
		}
		
		if(obj.buho.value == "/"){
			if(obj.value.value==0){
				alert("0์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
				obj.value.focus();
				return false;
			}
		}
	}
</script>
</head>
<body>
	<form action="http://localhost:8181/webTesting/com/java/servlet/Example05" 
		method="post" onsubmit="return checkForm(this)">
		<label>์ˆ˜</label>
		<input type="text" name="su"/>
		<br /><br />
		
		<label>๋ถ€ํ˜ธ</label>
		<input type="text" name="buho"/>
		<br /><br />
		
		<label>์ˆ˜</label>
		<input type="text" name="value"/>
		<br /><br />
		
		<input type="submit" value="์ „์†ก"/>
		<input type="reset" value="์ทจ์†Œ" />
	</form>
</body>
</html>

 

 

 

Example05.java

package com.java.servlet;

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Example05 extends HttpServlet{
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int su = Integer.parseInt(req.getParameter("su"));
		
		String buho = req.getParameter("buho");
		//char ch = buho.charAt(0); //if(ch =='+'){}
		
		int value = Integer.parseInt(req.getParameter("value"));
		System.out.println(su + "\t" + buho + "\t" + value);
		
		String result = "";
		if(buho.equals("+")) {
			/*
			 * Integer r = su+value;
			 * result = r.toString();
			 */
			result = String.valueOf(su+value); 
		}else if(buho.equals("-")){
			result = String.valueOf(su-value); 
		}else if(buho.equals("*")){
			result = String.valueOf(su*value); 
		}else if(buho.equals("/")){
			result = String.valueOf(su/value); 
		}
		System.out.println(result);
		
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out = resp.getWriter();
		out.print("<html>");
		out.print("<head><title>์‚ฌ์น™์—ฐ์‚ฐ</title></head>");
		out.print("<body>");
		out.print("<h3> ๊ฒฐ๊ณผ:" + result + "</h3>");
		out.print("</body>");
		out.print("</html>");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}

 

 

๋ฐ˜์‘ํ˜•
Comments