๊ด€๋ฆฌ ๋ฉ”๋‰ด

JiYoung Dev ๐Ÿ–ฅ

[JSP/Servlet] Servlet ๊ฐœ์š”, ๋ฐ์ดํ„ฐ ์ „์†ก (2023.05.10) ๋ณธ๋ฌธ

full stack/Back-End

[JSP/Servlet] Servlet ๊ฐœ์š”, ๋ฐ์ดํ„ฐ ์ „์†ก (2023.05.10)

Shinjio 2023. 5. 12. 18:23

๐ŸŽˆ request์™€ response

 

 

๐ŸŽˆ ๋ฐ์ดํ„ฐ ์ „์†ก

request : ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์„ ์–ด๋–ป๊ฒŒ ๋ณด๋‚ผ๊ฒƒ์ธ๊ฐ€๊ฐ€ ์ค‘์š”

 

 

action ์–ด๋””๋กœ ์š”์ฒญํ• ๊ฑด์ง€

method ์š”์ฒญ ๋ฐฉ์‹ (foam ํƒœ๊ทธ๋Š” get / post ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ, default ๋Š” get์œผ๋กœ ์ƒ๋žต ๊ฐ€๋Šฅ)

name input ํƒœ๊ทธ๋“ค์˜ ๋ช…์นญ, ๊ฐ๊ฐ์˜ ๊ฐ’์„ ๊ตฌ๋ณ€ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์ •ํ•ด์ฃผ๋Š” ์ด๋ฆ„ (์ด๋ฆ„์ด ์—†์œผ๋ฉด ํƒœ๊ทธ๋ฅผ ๊ตฌ๋ณ„ํ•  ์ˆ˜ ์—†์Œ) 

submit ์ œ์ถœ > ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋Š” ์ˆœ๊ฐ„ action์˜ ๊ฒฝ๋กœ๋กœ ์š”์ฒญ

 

๐ŸŽˆ request.getParameter()

 

 

package com.smhrd.servlet;

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;


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


	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
//		์š”์ฒญ ๋ฐ์ดํ„ฐ(name = data) ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ
//		getParameter(String : foam์˜ name๊ฐ’) 
//		์š”์ฒญ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ >> ๋ฐ˜ํ™˜ํƒ€์ž… : String
		String data = request.getParameter("data");
		System.out.println(data);
		
//		์‘๋‹ต๋ฐ์ดํ„ฐ ์ธ์ฝ”๋”ฉ ์„ค์ • ์ถ”๊ฐ€
		response.setCharacterEncoding("UTF-8"); //์ƒ๋žต๊ฐ€๋Šฅ
//		์›น๋ธŒ๋ผ์šฐ์ €๊ฐ€ ํ•œ๊ธ€ ์ธ์ฝ”๋”ฉ์„ ํ•  ์ˆ˜ ์žˆ๋„๋ก ์•Œ๋ ค์ฃผ๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•จ!
//		>> ๋ธŒ๋ผ์šฐ์ €์—๊ฒŒ ์‘๋‹ต๋ฐ์ดํ„ฐ์˜ ํ˜•์‹๊ณผ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์„ ์•Œ๋ ค์ฃผ๋Š” ์ฝ”๋“œ
		response.setContentType("text/html; charset=UTF-8");
		
//		์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’ ๊ทธ๋Œ€๋กœ html ๋ฌธ์„œ๋กœ ๋งŒ๋“ค์–ด์„œ ์‘๋‹ตํ•˜๊ธฐ
		PrintWriter out = response.getWriter(); //ํ…์ŠคํŠธ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ ๋งŒ๋“ค๊ธฐ
		out.print(data);
		
	}

}

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<!-- action : ์–ด๋””๋กœ ์š”์ฒญํ•  ๊ฑด์ง€(๊ฒฝ๋กœ)
	method : http ์š”์ฒญ ๋ฉ”์†Œ๋“œ (get/post) -->
	<!-- action : ๋ธŒ๋ผ์šฐ์ € ์ฃผ์†Œ์ฐฝ์— ๋œจ๋Š” ์ฃผ์†Œ ๊ฒฝ๋กœ ๊ธฐ์ค€ ์ƒ๋Œ€๊ฒฝ๋กœ -->
	<form action="Ex01" method="get">
		<input type="text" name="data"> <!-- name : ์„œ๋ฒ„์—์„œ ๊ฐ’์„ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ๋„๋ก -->
		<input type="submit" value="์ „์†ก">
	</form>
</body>
</html>

 

๐Ÿ“– ์‘๋‹ต ๋ฐ์ดํ„ฐ ์ธ์ฝ”๋”ฉํ•˜๊ธฐ

 

 

๐ŸŽˆ ์‹ค์Šต

HTML ํŒŒ์ผ์€ webapp ํด๋” ๋‚ด์— ์ €์žฅ!

 

 

์‹ค์Šต1. 

 

package com.smhrd.servlet;

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;

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


	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String number = request.getParameter("number");
		System.out.println(number);
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print("์ž…๋ ฅํ•œ ์ˆซ์ž๋Š” <b>" + number + "</b>์ž…๋‹ˆ๋‹ค");
		
	}

}

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="Ex02Number" method="get">
		<input type="number" name="number">
		<input type="submit" value="SEND">
	</form>
</body>
</html>

 

์‹ค์Šต2.

 

package com.smhrd.servlet;

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;


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


	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		์š”์ฒญ ๋ฐ์ดํ„ฐ 2๊ฐœ(num1, num2) ๋ฐ›๊ธฐ
		String num1 = request.getParameter("num1");
//		String์„ ์ •์ˆ˜ํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ• 
//		1) Integer (intํ˜•์˜ ํด๋ž˜์Šค ํƒ€์ž…์œผ๋กœ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ ๋ณด์œ )
//			>> parsInt(String) : ๋ฌธ์ž์—ด์„ ์ •์ˆ˜ํ˜•์œผ๋กœ ๋ณ€ํ™˜
//			>> ์ˆซ์ž๋กœ ๋ณ€ํ™˜ ๋ถˆ๊ฐ€๋Šฅํ•œ ๋ฌธ์ž์—ด์„ ๋„ฃ๋Š” ๊ฒฝ์šฐ(ex.ใ„ฑ, ใ„ด) ์˜ค๋ฅ˜ ๋ฐœ์ƒ
//			>> ์‚ฌ์šฉ์‹œ ๋ฌธ์ž์—ด ์•ˆ์— ์ˆซ์ž๋งŒ ํฌํ•จ๋˜๋„๋ก ์ฃผ์˜!
		int int_num1 = Integer.parseInt(num1);
		int int_num2 = Integer.parseInt(request.getParameter("num2"));
		
//		์‘๋‹ต
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print(int_num1 + " + " + int_num2 + " = " + (int_num1 + int_num2));
		
	}

}

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="Ex03">
		<input type="number" name="num1">
		+
		<input type="number" name="num2">
		<input type="submit" value="๋”ํ•˜๊ธฐ!">
	</form>
</body>
</html>

 

์‹ค์Šต3.

 

package com.smhrd.servlet;

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;


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


	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int int_num1 = Integer.parseInt(request.getParameter("num1"));
		String ope = request.getParameter("ope");
		int int_num2 = Integer.parseInt(request.getParameter("num2"));
		
		System.out.println(ope);
//		HTML์—์„œ val๊ฐ’์„ ์ž‘์„ฑํ•˜๋ฉด value๊ฐ’์„ ๋ฐ›์•„์˜ด
		
		response.setContentType("text/html; charset=UTF-8");
		int result = 0;
		String operator;
		if(ope.equals("plus")) {
			result = int_num1 + int_num2;
			operator = "+";
		}else if(ope.equals("minus")) {
			result = int_num1 - int_num2;
			operator = "-";
		}else if(ope.equals("mul")) {
			result = int_num1 * int_num2;
			operator = "*";
		}else {
			result = int_num1 / int_num2;
			operator = "/";
		}
		String str = int_num1 + " " + operator + " " + int_num2 + " = " + result ;
		PrintWriter out = response.getWriter();
		out.print(str);
		
	}

}

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="Ex04">
		<input type="number" name="num1">
		<select name="ope">
			<option value="plus">+</option>
			<option value="minus">-</option>
			<option value="mul">*</option>
			<option value="div">/</option>
		</select>
		<input type="number" name="num2">
		<input type="submit" value="SEND">
	</form>
</body>
</html>

 

์‹ค์Šต4. 

 

package com.smhrd.servlet;

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;


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


	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		 ์š”์ฒญ
		
//		์‘๋‹ต
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		out.print("<table border=1>");
		out.print("<tr>");
		for(int i = 1; i <= 6 ; i++) {
			out.print("<td>" + i + "</td>");
		}
//		out.print("<td>1</td>");
//		out.print("<td>2</td>");
//		out.print("<td>3</td>");
//		out.print("<td>4</td>");
//		out.print("<td>5</td>");
//		out.print("<td>6</td>");
		out.print("</tr>");
		out.print("</table>");
	}

}

 

 

์˜ค๋ฅ˜ ์˜ˆ. 404

 

 

WEB-INF ๋Š” ํด๋ผ์ด์–ธํŠธ(์›น๋ธŒ๋ผ์šฐ์ €)๊ฐ€ ์ ˆ๋Œ€ ์ ‘๊ทผํ•  ์ˆ˜ ์—†์Œ > ๊ฒฝ๋กœ๋Š” ๋งž์ง€๋งŒ ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ๊ฒฝ๊ณ  ๋ฐœ์ƒ

๋”ฐ๋ผ์„œ htmlํŒŒ์ผ์ด WEB-INF ํด๋” ๋‚ด์— ๋“ค์–ด๊ฐ€์ง€ ์•Š๋„๋ก ์ฃผ์˜!

 

์˜ค๋ฅ˜ ์˜ˆ. 500

 

์˜ค๋ฅ˜๋Š” ๋‚ด๊ฐ€ ์“ด ์ฝ”๋“œ์—์„œ ํ™•์ธ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค!

์ž…๋ ฅํ•œ ๊ฐ’์ด ์—†์ด ์‹คํ–‰ >> ์ž…๋ ฅํ•œ ๊ฐ’์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— null์„ ๊ฐ€์ง >> ์˜ˆ์™ธ์ƒํ™ฉ ๋ฐœ์ƒ

 

๋Ÿฐํƒ€์ž„์˜ค๋ฅ˜ : ์‹คํ–‰ํ–ˆ์„ ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜ 

try ~ catch๋กœ ์˜ˆ์™ธ์ฒ˜๋ฆฌ