๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
JSP

[JSP] Servlet์„ ์ด์šฉํ•œ ํด๋ผ์ด์–ธํŠธ์—์„œ ์ „์†ก๋˜๋Š” ์š”์ฒญ ์ฒ˜๋ฆฌ

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 9.
๋ฐ˜์‘ํ˜•

โ–ถ ๊ฐ ์žฅ์—์„œ ์˜ˆ์ œ ์†Œ์Šค๋ฅผ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•œ ์›น ํ”„๋กœ์ ํŠธ ์ƒ์„ฑํ•˜๊ธฐ

โ€‹

1. New→Dynamic Web Project๋กœ ๋“ค์–ด๊ฐ„๋‹ค.

โ€‹

โ€‹โ€‹

2. Project name์— "Chapter3" ์ด๋ผ๊ณ  ์ž…๋ ฅ ํ›„ <Next>๋ฅผ ๋ˆ„๋ฅธ๋‹ค.

โ€‹

โ€‹

3. <Next>๋ฅผ ๋ˆ„๋ฅธ๋‹ค.

โ€‹

โ€‹

4. ์ฒดํฌ๋ฐ•์Šค๋ฅผ ์ฒดํฌํ•œ ํ›„ <Finish>๋ฅผ ๋ˆ„๋ฅธ๋‹ค. ์ดํด๋ฆฝ์Šค์—์„œ web.xml ํŒŒ์ผ์ด ํ”„๋กœ์ ํŠธ์— ์ž๋™ ์ƒ์„ฑ๋˜๊ฒŒ ํ•˜๋ ค๋ฉด ์ฒดํฌ ๋ฐ•์Šค๋ฅผ ์ฒดํฌํ•˜์—ฌ์•ผ ํ•œ๋‹ค.

โ€‹

โ€‹โ€‹

โ€‹

โ–ถ Form ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•œ GET ๋ฐฉ์‹์˜ ์š”์ฒญ ์ฒ˜๋ฆฌ

โ€‹

1. Chapter3์˜ WebContent์— ๋งˆ์šฐ์Šค ์˜ค๋ฅธ์ชฝ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๊ณ  HTML File์„ ์ƒ์„ฑํ•œ๋‹ค.

โ€‹โ€‹

โ€‹

2. File name์— "login"์„ ์ž…๋ ฅํ•˜๊ณ  <Next>๋ฅผ ๋ˆ„๋ฅธ๋‹ค.

โ€‹โ€‹

โ€‹

3. ์ƒ์„ฑํ•  HTML ํŒŒ์ผ์˜ ๋ฒ„์ „์„ "html 5"๋กœ ์„ ํƒํ•˜๊ณ  <Finish>๋ฅผ ๋ˆ„๋ฅธ๋‹ค.

โ€‹

โ€‹โ€‹

โ€‹

โ–ถ ํด๋ผ์ด์–ธํŠธ ํŽ˜์ด์ง€ ์ƒ์„ฑํ•˜๊ธฐ

๋‹ค์Œ ์ฝ”๋“œ๋ฅผ login.html์— ์ž‘์„ฑํ•œ๋‹ค.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
#loginFormArea {
	text-align: center;
	width: 260px;
	margin: auto;
	border: 1px solid red
}

h1 {
	text-align: center;
}
</style>
</head>
<body>
<h1>๋กœ๊ทธ์ธ</h1>
<section id = "loginFormArea">
<form action="login" method="get">
	<label for="id">์•„์ด๋”” :</label><input type="text" name="id" id="id"/><br>
	<label for="passwd">๋น„๋ฐ€๋ฒˆํ˜ธ :</label> <input type="password" name="passwd" id="passwd"><br><br>
	<input type="submit" value="๋กœ๊ทธ์ธ"/>
</form>
</section>
</body>
</html>

 

 

 

โ–ถ ์„œ๋ธ”๋ฆฟ ์ƒ์„ฑํ•˜๊ธฐ

โ€‹

1. New→Servlet์„ ํด๋ฆญํ•œ๋‹ค.

โ€‹

โ€‹

2. Class name์— "LoginServlet"์„ ์ ๊ณ  <Next>๋ฅผ ๋ˆ„๋ฅธ๋‹ค. ์ด ๋ถ€๋ถ„์€ ์‹ค์งˆ์ ์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ์ž๋ฐ” ํŒŒ์ผ๋ช…์„ ์ง€์ •ํ•˜๋Š” ๋ถ€๋ถ„์œผ๋กœ web.xml ์„ค์ •์—์„œ <servlet-class> ํ•ญ๋ชฉ์— ์„ค์ •๋˜๋Š” ๊ฐ’์ด๋‹ค.

โ€‹

โ€‹

3. ํด๋ผ์ด์–ธํŠธ ํผ ํƒœ๊ทธ์˜ ์š”์ฒญ ๊ฒฝ๋กœ๊ฐ€ login(<form action="login">)์œผ๋กœ ์„ค์ •๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ URL mapping ๊ฐ’์„ /login์œผ๋กœ ์ˆ˜์ •ํ•œ๋‹ค. ์ด ๋ถ€๋ถ„์€ web.xml ์„ค์ • ํŒŒ์ผ์—์„œ <url-pattern> ํ•ญ๋ชฉ์˜ ๋‚ด์šฉ์œผ๋กœ ์ถ”๊ฐ€๋˜๋Š” ๋ถ€๋ถ„์ด๋‹ค.

โ€‹

โ€‹

4. ๋ณธ ์˜ˆ์ œ์˜ ์š”์ฒญ ๋ฐฉ์‹์ด GET ๋ฐฉ์‹์ด๋ฏ€๋กœ "Which method stubs would you like to create?" ๋ถ€๋ถ„์˜ ์ฒดํฌ ๋ฐ•์Šค์—์„œ doGet ๋ฉ”์„œ๋“œ๋งŒ ์ฒดํฌํ•œ ํ›„ <Finish>๋ฅผ ํด๋ฆญํ•œ๋‹ค.

โ€‹

โ€‹

5. ๋‹ค์Œ ์†Œ์Šค๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

โ€‹

 

LoginServlet.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 LoginServlet
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		String id = request.getParameter("id");
		String passwd = request.getParameter("passwd");
		response.setContentType("text/html;charset=euc-kr");
		PrintWriter out = response.getWriter();
		out.println("์•„์ด๋””=" + id + "<br>");
		out.println("๋น„๋ฐ€๋ฒˆํ˜ธ=" + passwd + "<br>");	
	}
}

 

 

6. ๊ฒฐ๊ณผ ํ™•์ธํ•˜๊ธฐ

โ€‹

โ€‹

โ€‹

โ–ถ a ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•œ GET ๋ฐฉ์‹์˜ ์š”์ฒญ ์ฒ˜๋ฆฌ

โ€‹

1. ํด๋ผ์ด์–ธํŠธ ํŽ˜์ด์ง€ ์ฝ”๋”ฉ

โ€‹

 

boardList.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>2ํŽ˜์ด์ง€ ๋ชฉ๋ก ์š”์ฒญํ•˜๊ธฐ</h1>
	<a href="boardList?page=2">2page</a>
</body>
</html>

 

 

2. ์„œ๋ธ”๋ฆฟ ํŽ˜์ด์ง€ ์ฝ”๋”ฉ

โ€‹

 

BoardListServlet.java

import java.io.PrintWriter;
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;

/**
 * Servlet implementation class BoardListServlet
 */
@WebServlet("/boardList")
public class BoardListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BoardListServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String page = request.getParameter("page");
		response.setContentType("text/html;charset=euc-kr");
		PrintWriter out = response.getWriter();
		out.println(page + " ํŽ˜์ด์ง€ ๊ฒŒ์‹œํŒ ๋ชฉ๋ก ์ถœ๋ ฅ");
	}
}

 

 

3. ๊ฒฐ๊ณผ ํ™•์ธํ•˜๊ธฐ

 

์œ„์˜ 2page๋ฅผ ๋ˆ„๋ฅด๋ฉด

์ด๋ ‡๊ฒŒ ๋œฌ๋‹ค.

โ€‹

โ€‹โ€‹

โ€‹

โ–ถ ์ฃผ์†Œ ํ‘œ์‹œ์ค„์— URL์„ ์ง์ ‘ ์ž…๋ ฅํ•˜์—ฌ ์š”์ฒญํ•˜๋Š” GET ๋ฐฉ์‹์˜ ์š”์ฒญ ๋ฐฉ์‹

โ€‹

โ€‹

DirectURLServlet.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 DirectURLServlet
 */
@WebServlet("/directURL")
public class DirectURLServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DirectURLServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.println("์ด๋ฆ„ : " + name + "<br>");
		out.println("๋‚˜์ด : " + age + "<br>");
	}
}

 

 

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

์œ„์˜ URL ๋’ค์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ „์†กํ•  ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ?name=aaa&age=23 ์„ ์ถ”๊ฐ€ํ•œ ํ›„ ๋‹ค์‹œ ์š”์ฒญํ•˜๋ฉด name๊ณผ age ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์ด GET ๋ฐฉ์‹์œผ๋กœ ์„œ๋ธ”๋ฆฟ์— ์ „์†ก๋œ๋‹ค.

โ€‹

โ€‹โ€‹

โ€‹

โ–ถ POST ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญ์ด ์ „์†ก๋˜์–ด ์˜ฌ ๊ฒฝ์šฐ

POST ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  ๊ฒฝ์šฐ๋„ GET ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒฝ์šฐ์™€ ๊ฑฐ์˜ ์œ ์‚ฌํ•˜๋‹ค. ๋‹จ์ง€ ์„œ๋ธ”๋ฆฟ ์ชฝ์—์„œ ์š”์ฒญ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ doGet์ด ์•„๋‹Œ doPost ๋ฉ”์„œ๋“œ์—์„œ ์š”์ฒญ์ด ์ฒ˜๋ฆฌ๋œ๋‹ค๋Š” ์ฐจ์ด์ ์ด ์กด์žฌํ•œ๋‹ค.

โ€‹

1. ํด๋ผ์ด์–ธํŠธ ํŽ˜์ด์ง€ ์ฝ”๋”ฉ

โ€‹

 

memReg.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>ํšŒ์› ๊ฐ€์ž…</h1>
	<form action="memReg" method="post">
		ํšŒ์›๋ช… :<input type="text" name="name"><br> 
		์ฃผ์†Œ :<input type="text" name="addr"><br> 
		์ „ํ™”๋ฒˆํ˜ธ :<input type="text" name="tel"><br> 
		์ทจ๋ฏธ :<input type="text" name="hobby"><br>
		<input type="submit" value="ํšŒ์› ๊ฐ€์ž…" />
	</form>
</body>
</html>

ํด๋ผ์ด์–ธํŠธ ํŽ˜์ด์ง€ ์†Œ์Šค๋ฅผ ๋ณด๋ฉด ํผ ํƒœ๊ทธ์˜ method ์†์„ฑ๋งŒ post๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ๊ณ , ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์€ GET ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญ์„ ๋ณด๋‚ผ ๋•Œ์™€ ๊ฑฐ์˜ ์œ ์‚ฌํ•˜๋‹ค.

โ€‹

โ€‹

2. ์„œ๋ธ”๋ฆฟ ํŽ˜์ด์ง€ ์ฝ”๋”ฉ

โ€‹

 

MemRegServlet.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 MemRegServlet
 */
@WebServlet("/memReg")
public class MemRegServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public MemRegServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setContentType("text/html;charset=euc-kr");
		PrintWriter out = response.getWriter();
		String name = request.getParameter("name");
		String addr = request.getParameter("addr");
		String tel = request.getParameter("tel");
		String hobby = request.getParameter("hobby");
		out.println("์ด๋ฆ„ = " + name + "<br>");
		out.println("์ฃผ์†Œ = " + addr + "<br>");
		out.println("์ „ํ™”๋ฒˆํ˜ธ = " + tel + "<br>");
		out.println("์ทจ๋ฏธ = " + hobby + "<br>");
	}
}

์„œ๋ธ”๋ฆฟ ํŽ˜์ด์ง€ ์†Œ์Šค๋ฅผ ๋ณด๋ฉด ์ „์†ก๋˜์–ด ์˜ค๋Š” ์š”์ฒญ์„ doPost ๋ฉ”์„œ๋“œ์—์„œ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋‹ค. ํ•œ๊ธ€ ๋ฌธ์ œ ์ฒ˜๋ฆฌ๋Š” ํ•˜์ง€ ์•Š์•˜๋‹ค.

โ€‹

โ€‹

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

โ€‹โ€‹

โ€‹

โ–ถ ํ•œ๊ธ€ ์ฒ˜๋ฆฌ์šฉ ํŽ˜์ด์ง€ ์ž‘์„ฑ

โ€‹

1. ํด๋ผ์ด์–ธํŠธ ํŽ˜์ด์ง€ ์ž‘์„ฑ

โ€‹

 

hangul.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="hangul" method="get">
		ํ•œ๊ธ€ ์ด๋ฆ„ : <input type="text" name="name" /><br> 
		<input type="submit" value="ํ™•์ธ" />
	</form>
</body>
</html>

 

 

2. ์„œ๋ธ”๋ฆฟ ํŽ˜์ด์ง€ ์ž‘์„ฑํ•˜๊ธฐ

โ€‹

 

HangulServlet.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 HangulServlet
 */
@WebServlet("/hangul")
public class HangulServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public HangulServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	
		String name = request.getParameter("name");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.println("hangul Process = " + name);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		String name = request.getParameter("name");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.println("hangul Process = " + name);
	}
}

 

 

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

โ€‹โ€‹

โ€‹

โ–ถ POST ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญ์ด ์ „์†ก๋˜์–ด ์˜ฌ ๊ฒฝ์šฐ

โ€‹

1. hangul.html ํŽ˜์ด์ง€์˜ ์ฝ”๋“œ ๋‚ด์šฉ์—์„œ form ํƒœ๊ทธ์˜ method ์†์„ฑ์„ POST๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.

โ€‹

 

hangul.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="hangul" method="post">
		ํ•œ๊ธ€ ์ด๋ฆ„ : <input type="text" name="name" /><br> 
		<input type="submit" value="ํ™•์ธ" />
	</form>
</body>
</html>

 

 

2. ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์ด POST ๋ฐฉ์‹์œผ๋กœ ์ „์†ก๋˜์–ด ์˜ค๊ธฐ ๋•Œ๋ฌธ์— HangulServlet์˜ doGet ๋ฉ”์„œ๋“œ์— ์ •์˜๋˜์–ด ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ doPost ๋ฉ”์„œ๋“œ๋กœ ๊ทธ๋Œ€๋กœ ๋ณต์‚ฌํ•œ๋‹ค.

โ€‹

 

HangulServlet.java

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

		String name = request.getParameter("name");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.println("hangul Process(get) = " + name);
}

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

		request.setCharacterEncoding("UTF-8");
		String name = request.getParameter("name");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.println("hangul Process(post) = " + name);
}

 

 

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

โ€‹โ€‹

โ€‹

โ–ถ ํ•˜๋‚˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์œผ๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์ด ์ „์†ก๋˜์–ด ์˜ฌ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌํ•˜๊ธฐ

์•ž์—์„œ ์†Œ๊ฐœํ•œ ์„œ๋ธ”๋ฆฟ ์˜ˆ์ œ๋“ค์€ ํ•˜๋‚˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์œผ๋กœ ํ•˜๋‚˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์ด ์ „์†ก๋˜๋Š” ๊ฒฝ์šฐ์˜ ์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•์ด์—ˆ๊ธฐ ๋•Œ๋ฌธ์— HttpServletRequest์—์„œ ์ œ๊ณต๋˜๋Š” String getParameter(String paramName)์œผ๋กœ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ–ˆ์œผ๋‚˜, ํ•˜๋‚˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์œผ๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ’์ด ์ „์†ก๋˜์–ด ์˜ฌ ๋•Œ๋Š” HttpServletRequest ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ์ œ๊ณต๋˜๋Š” String[] getParameterValues(String paramName) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ฒ˜๋ฆฌํ•ด์•ผ ๋œ๋‹ค.

์ฒดํฌ ๋ฐ•์Šค์˜ ๊ฒฝ์šฐ ๊ฐ™์€ ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์œผ๋กœ ๊ฐ’์ด ์—ฌ๋Ÿฌ ๊ฐœ๊ฐ€ ์ „์†ก๋˜์–ด ์˜ฌ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ String[] getParameterValues(String paramValue)๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ „์†ก๋ฐ›์•„์•ผ ํ•œ๋‹ค.

โ€‹

โ€‹

1. ํด๋ผ์ด์–ธํŠธ ํŽ˜์ด์ง€

โ€‹

 

dog.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>๋‹น์‹ ์ด ์ข‹์•„ํ•˜๋Š” ๊ฐ•์•„์ง€๋ฅผ ์„ ํƒํ•˜์„ธ์š”</h1>
<form action="choiceDog" method="post">
	<input type="checkbox" name="dog" value="pu.jpg"/>ํ‘ธ๋“ค
	<input type="checkbox" name="dog" value="jin.jpg"/>์ง„๋—๊ฐœ
	<input type="checkbox" name="dog" value="pung.jpg"/>ํ’์‚ฐ๊ฐœ 
	<input type="checkbox" name="dog" value="sap.jpg"/>์‚ฝ์‚ด๊ฐœ
	<input type="submit" value="์„ ํƒ"/>
</form>
</body>
</html>

 

 

2. ์„œ๋ธ”๋ฆฟ ํŽ˜์ด์ง€

โ€‹

 

ChoiceDogServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String[] dog = request.getParameterValues("dog");
		out.println("<html>");
		out.println("<head>");
		out.println("</head>");
		out.println("<body bgcolor='black'>");
		out.println("<table align='center' bgcolor='yellow'>");
		out.println("<tr>");
		for (int i = 0; i < dog.length; i++) {
			out.println("<td>");
			out.println("<img src='" + dog[i] + "'/>");
			out.println("</td>");
		}
		out.println("</tr>");
		out.println("</table>");
		out.println("</body>");
		out.println("</html>");
	}

 

 

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

โ€‹โ€‹

โ€‹

์‹ค์Šต) ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์˜ค๊ฒŒ ํ•˜์„ธ์š”.

์ด๋ ‡๊ฒŒ ๋‚˜์˜ค๊ฒŒ!!!

 

 

<๋‚ด๊ฐ€ ํ•œ ๊ฒƒ>

โ€‹

 

delivery.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
	#deliveryFormArea {
		text-align: center;
		width: 270px;
		margin: auto;
		border: 1px solid red
	}

	h1 {
		text-align: center;
	}
</style>
</head>
<body>
<h1>๋ฐฐ์†ก์ง€</h1>
<section id="deliveryFormArea">
<form action="delivery" method="post">
	<label>๋ฐฐ์†ก์ง€</label><br>
	<label for="addr">๋ฐ›์„ ์ฃผ์†Œ :</label> 
	<input type="text" name="addr" id="addr"/><br>
	<label for="name">๋ฐ›๋Š” ์‚ฌ๋žŒ :</label> 
	<input type="text" name="name" id="name"/><br><br>
	<input type="submit" value="์ž…๋ ฅ">
</form><br>
	<label>์ „ํ™”</label><br>
<form action="delivery" method="get">
	<label for="tel">์ „ํ™”๋ฒˆํ˜ธ :</label> 
	<input type="text" name="tel" id="tel"/><br>
	<input type="submit" value="์ „์†ก">
</form>
</section>
</body>
</html>

 

 

DeliveryServlet.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 DeliveryServlet
 */
@WebServlet("/delivery")
public class DeliveryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DeliveryServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	
		request.setCharacterEncoding("UTF-8");
		String addr = request.getParameter("addr");
		String name = request.getParameter("name");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.println("(post):" + "<br>");
		out.println("์ฃผ์†Œ : " + addr + "<br>");
		out.println("์ด๋ฆ„ : " + name + "<br>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String tel = request.getParameter("tel");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.println("(get):" + "<br>");
		out.println("์ „ํ™”๋ฒˆํ˜ธ : " + tel + "<br>");
	}

}
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€