danbibibi
article thumbnail
Published 2023. 3. 21. 00:03
Servlet๊ณผ JSP WEB/back-end
๐Ÿ’ก Servlet, JSP ์™œ ์“ธ๊นŒ?
     ๋™์  ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ œ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”
     ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ์ด์šฉํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— Servlet์ด๋‚˜ JSP๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค!

  =  ํ•œ ๋งˆ๋””๋กœ, Java๋กœ ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋Š”๋ฐ, Java๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ!!

  * ๋™์  ์›น ํŽ˜์ด์ง€ : ๋“ฑ๋ก๋œ ์ž์›๋“ค ์ค‘ ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๋“ค์ด ๋‚˜์˜ค๋Š” ํŽ˜์ด์ง€

 

Servlet

  • Java ์ฝ”๋“œ ๋‚ด HTML ์ฝ”๋“œ
  • ์ž๋ฐ”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›น ์„œ๋ฒ„์—์„œ ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•œ ์›น ํŽ˜์ด์ง€๋ฅผ ์›น ๋ธŒ๋ผ์šฐ์ €์— ๋Œ๋ ค์คŒ

 

Servlet Life-Cycle

  • ๋‹จ์ˆœํ•œ ํด๋ž˜์Šค๊ฐ€ ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๊ฐ€ ๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์„œ๋ธ”๋ฆฟ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๊ฐ€์ ธ์•ผ ํ•จ
  • init(), service(), destroy() ์ˆœ์„œ์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๋กœ ๊ฐ–๋Š” ์ธํ„ฐํŽ˜์ด์Šค = servlet ์ธํ„ฐํŽ˜์ด์Šค

 

init()

:์„œ๋ธ”๋ฆฟ์ด ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋“œ ๋  ๋•Œ ํ•œ๋ฒˆ ํ˜ธ์ถœ (์ฝ”๋“œ ์ˆ˜์ •์œผ๋กœ ๋‹ค์‹œ ๋กœ๋“œ ๋˜๋ฉด ๋‹ค์‹œ ํ˜ธ์ถœ)

public void init(ServletConfig config)
          throws ServletException

 

service()

๋ชจ๋“  ์š”์ฒญ์€ service()๋ฅผ ํ†ตํ•ด์„œ doXXX() ๋ฉ”์†Œ๋“œ๋กœ ์ด๋™ (์š”์ฒญ๋งˆ๋‹ค ๋ฐ˜๋ณต ํ˜ธ์ถœ)

* service()๋ฅผ ์žฌ์ •์˜ ํ•˜๋Š” ๊ฒฝ์šฐ doXXX() ๋ฉ”์†Œ๋“œ๋ฅผ ์ง์ ‘ ํ˜ธ์ถœํ•ด์ฃผ์ž!

public void service(ServletRequest req,
                    ServletResponse res)
             throws ServletException,
                    IOException

 

// GET
protected void doGet(HttpServletRequest req,
                     HttpServletResponse resp)
              throws ServletException,
                     IOException
 
 // POST
 protected void doPost(HttpServletRequest req,
                      HttpServletResponse resp)
               throws ServletException,
                      IOExceptionโ€‹

 

destroy()

: ์„œ๋ธ”๋ฆฟ์ด ๋ฉ”๋ชจ๋ฆฌ์—์„œ ํ•ด์ œ๋˜๋ฉด ํ˜ธ์ถœ (์ฝ”๋“œ๊ฐ€ ์ˆ˜์ •๋˜๋ฉด ํ˜ธ์ถœ)

public void destroy()

 

 HttpServlet

: ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•˜๋˜ service()๋ฅผ ๋‹ด์€ ์ƒ์† ํด๋ž˜์Šค

HttpServlet์„ ์ƒ์†๋ฐ›์€ ์„œ๋ธ”๋ฆฟ ๋™์ž‘ ๊ตฌ์กฐ

๐Ÿ’ก Servlet ์ธํ„ฐํŽ˜์ด์Šค 
   : init(), service(), destroy(), getServletConfig(), getServletInfo()
     ๋“ฑ 5๊ฐœ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์„ ์–ธํ•˜๊ณ  ์žˆ์Œ
     = servlet ์ƒ๋ช…์ฃผ๊ธฐ์™€ ์—ฐ๊ด€๋œ ๋ฉ”์†Œ๋“œ๋“ค

๐Ÿ’ก GenericServlet ์ธํ„ฐํŽ˜์ด์Šค 
   : service() ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๋ฉ”์†Œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•˜์—ฌ ์ ์ ˆํ•œ ๊ธฐ๋Šฅ์œผ๋กœ ๊ตฌํ˜„

๐Ÿ’ก HttpServlet ์ธํ„ฐํŽ˜์ด์Šค 
   : GenericServlet์„ ์ƒ์†ํ•˜์—ฌ service() ๋ฉ”์†Œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•จ์œผ๋กœ์จ
     HTTP ํ”„๋กœํ† ์ฝœ์— ์•Œ๋งž์€ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ๊ตฌํ˜„
    ( service() ๋ฉ”์†Œ๋“œ ⇒ ์š”์ฒญ ๋ฐฉ์‹์— ๋”ฐ๋ผ doGet(), doPost() ๋“ฑ ์ •ํ•ด์ง„ ์‚ฌ์–‘์˜ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ)

 

GenericServlet

 

GenericServlet (Java(TM) EE 7 Specification APIs)

Defines a generic, protocol-independent servlet. To write an HTTP servlet for use on the Web, extend HttpServlet instead. GenericServlet implements the Servlet and ServletConfig interfaces. GenericServlet may be directly extended by a servlet, although it'

docs.oracle.com

HttpServlet

 

HttpServlet (Java(TM) EE 7 Specification APIs)

Called by the server (via the service method) to allow a servlet to handle a PUT request. The PUT operation allows a client to place a file on the server and is similar to sending a file by FTP. When overriding this method, leave intact any content headers

docs.oracle.com

 

Servlet ์‹ค์Šต

โœ… servlet์„ ํ˜ธ์ถœํ•  HTML ํ™”๋ฉด

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Cache-Control" content="no-cache">
<title>์‚ฌ์šฉ์ž์ •๋ณด์‚ฌ์ดํŠธ</title>
<style>
    label {
        display: inline-block;
        width: 80px;
    }
</style>
</head>
<body>
    <h1>์‚ฌ์šฉ์ž ๊ด€๋ฆฌ</h1>
    <!-- method ํƒ€์ž… ์„ค์ •, submit์‹œ ์–ด๋””๋กœ ๊ฐˆ๊ฑด์ง€ ์„ค์ • ( ๋ฉ”์ธ ์„œ๋ธ”๋ฆฟ url ) -->
    <form method="post" action="main">
        <fieldset>
        <legend>์‚ฌ์šฉ์ž ์ •๋ณด ์ž…๋ ฅ</legend>
        <!-- front-controller pattern์—์„œ ์š”์ฒญ์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•œ parameter -->
        <input type="hidden" name="action" value="regist">
        <label for="id">์•„์ด๋””</label>
        <input type="text" id="id" name="id" value="hong"><br>
        <label for="password">๋น„๋ฐ€๋ฒˆํ˜ธ</label>
        <input type="password" id="password" name="password" value="1234"><br>
        <label for="name">์ด๋ฆ„</label>
        <input type="text" id="name" name="name" value="ํ™๊ธธ๋™"><br>
        <label for="email">์ด๋ฉ”์ผ</label>
        <input type="email" id="email" name="email" value="hong@email.com"><br>
        <label for="age">๋‚˜์ด</label>
        <input type="number" id="age" name="age" value="20"><br>
        <input type="submit" value="๋“ฑ๋ก">
        <input type="reset" value="์ดˆ๊ธฐํ™”">
        </fieldset>
    </form>
</body>
</html>โ€‹

 

โœ… User.java (DTO)

@Getter
@Setter
public class User {

	private String id; // ์•„์ด๋””
	private String password; // ๋น„๋ฐ€๋ฒˆํ˜ธ
	private String name; // ์ด๋ฆ„
	private String email;// ์ด๋ฉ”์ผ
	private int age; // ๋‚˜์ด

	// ๊ธฐ๋ณธ ์ƒ์„ฑ์ž
	public User() {
	}

	public User(String id, String password, String name, String email, int age) {
		super();
		this.id = id;
		this.password = password;
		this.name = name;
		this.email = email;
		this.age = age;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", password=" + password + ", name=" + name + ", email=" + email + ", age=" + age
				+ "]";
	}

}

 

โœ… ํ˜ธ์ถœ๋  servlet

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;
import dto.User;

// ์ด ์„œ๋ธ”๋ฆฟ์ด ํ˜ธ์ถœ๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” url ์ƒ์— http:server_ip:port/context_name/main ์ด ํ•„์š”
 @WebServlet("/main")
 public class MainServlet extends HttpServlet {

     private static final long serialVersionUID = 1L;

    
     /**
      * get ๋ฐฉ์‹์˜ ์š”์ฒญ์— ๋Œ€ํ•ด ์‘๋‹ตํ•˜๋Š” ๋ฉ”์„œ๋“œ
      * front controller pattern์„ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋‚ด๋ถ€์ ์œผ๋กœ process๋ฅผ ํ˜ธ์ถœ
      */
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         process(request, response);
     }
    
    
     /**
      * post ๋ฐฉ์‹์˜ ์š”์ฒญ์— ๋Œ€ํ•ด ์‘๋‹ตํ•˜๋Š” ๋ฉ”์„œ๋“œ
      * front controller pattern์„ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋‚ด๋ถ€์ ์œผ๋กœ process๋ฅผ ํ˜ธ์ถœ
      */
     protected void doPost (HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
//         post ์š”์ฒญ ์‹œ ํ•œ๊ธ€ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด encoding ์ฒ˜๋ฆฌ
         request.setCharacterEncoding("UTF-8");
         process(request, response);
     }
    
     /**
      * request ๊ฐ์ฒด์—์„œ action ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ถ”์ถœํ•ด์„œ ์‹ค์ œ ๋น„์ง€๋‹ˆ์Šค ๋กœ์ง ๋ฉ”์„œ๋“œ(ex: doRegist)ํ˜ธ์ถœ
      */
     private void process(HttpServletRequest request,HttpServletResponse response)
             throws ServletException, IOException {
         String action = request.getParameter("action");
         switch (action) {
         case "regist":
             doRegist(request, response);
             break;
         }
     }
    
     /**
      * ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•ด ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์ž˜ ์ „๋‹ฌ๋˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์ „๋‹ฌ ๊ฒฐ๊ณผ๋ฅผ ํ™”๋ฉด์— ์ถœ๋ ฅ
      * ์ด๋ฅผ ์œ„ํ•ด request์—์„œ ์ „๋‹ฌ ๋ฐ›์€ ๋‚ด์šฉ์„ ์ถ”์ถœํ•ด์„œ User ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ํ›„ response๋กœ ์ถœ๋ ฅ
      * ํŠนํžˆ response ์‹œ content์˜ ํ˜•์‹์— ์ฃผ์˜
      * @param request
      * @param response
      * @throws ServletException
      * @throws IOException
      */
     private void doRegist(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
             
//          request ๊ฐ์ฒด์—์„œ ์ „๋‹ฌ๋œ parameter๋ฅผ ์ถ”์ถœ
         String id = request.getParameter("id");
         String password = request.getParameter("password");
         String name = request.getParameter("name");
         String email = request.getParameter("email");
         
//         ๋ฌธ์ž์—ด๋กœ ์ „๋‹ฌ๋œ age๋Š” ์ˆซ์ž๋กœ ๋ณ€ํ™˜
         int age = Integer.parseInt(request.getParameter("age"));
        
//         ์ „๋‹ฌ๋ฐ›์€ parameter๋ฅผ ์ด์šฉํ•ด์„œ User ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
         User user = new User(id, password, name, email, age);
        
//         ํ™”๋ฉด์— ์ถœ๋ ฅํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ์„ฑ
         StringBuilder output = new StringBuilder();
        
         output.append("<html><body>").append("<h1>์‚ฌ์šฉ์ž ์ •๋ณด</h1>").append(user.toString()).append("</body></html>");
        
//         response ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด์„œ ์ƒ์„ฑํ•œ html์ฝ”๋“œ๋ฅผ ์ถœ๋ ฅ
//         ์‘๋‹ต์ด ์–ด๋–ค ํƒ€์ž…์ธ์ง€ ์„ค์ •
         response.setContentType("text/html; charset=UTF-8");
         response.getWriter().write(output.toString());
        
     }
 }

 

 

JSP (Java Server Page)

  • HTML ์ฝ”๋“œ ๋‚ด Java ์ฝ”๋“œ
  • JSP๋Š” ์‹คํ–‰ ์‹œ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ๋ณ€ํ™˜๋œ ํ›„ ์‹คํ–‰๋จ
  • ์ž๋ฐ”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›น ์„œ๋ฒ„์—์„œ ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•œ ์›น ํŽ˜์ด์ง€๋ฅผ ์›น ๋ธŒ๋ผ์šฐ์ €์— ๋Œ๋ ค์คŒ

 

JSP Scriptlet

์„ ์–ธ

๋ฉค๋ฒ„ ๋ณ€์ˆœ ์„ ์–ธ์ด๋‚˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์„ ์–ธํ•˜๋Š” ์˜์—ญ

<%! ์ „์—ญ๋ณ€์ˆ˜ or method %>

 

์Šคํฌ๋ฆฝํŠธ๋ฆฟ

Client ์š”์ฒญ ์‹œ ๋งค๋ฒˆ ํ˜ธ์ถœ ์˜์—ญ์œผ๋กœ, Servlet์œผ๋กœ ๋ณ€ํ™˜ ์‹œ `service()` method์— ํ•ด๋‹น๋˜๋Š” ์˜์—ญ

<% ์ž๋ฐ”์ฝ”๋“œ %>

 

ํ‘œํ˜„์‹

๋ฐ์ดํ„ฐ๋ฅผ ๋ธŒ๋ผ์šฐ์ €์— ์ถœ๋ ฅํ•  ๋•Œ ์‚ฌ์šฉ  * ๋ฌธ์ž์—ด ๋’ค ; ์€ ์ž‘์„ฑ X

<%= ๋ฌธ์ž์—ด %>

 

์ฃผ์„

<%-- ์ฃผ์„ ๋‚ด์šฉ --%>

 

JSP ๊ธฐ๋ณธ ๊ฐ์ฒด

 ๊ธฐ๋ณธ ๊ฐ์ฒด๋ช…  Type  ์„ค๋ช…
 request  javax.servlet.http.HttpServletRequest  ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ ์ •๋ณด๋ฅผ ์ €์žฅ
 HTML ํผ ์š”์†Œ์˜ ์„ ํƒ ๊ฐ’ ๋“ฑ
 ์‚ฌ์šฉ์ž ์ž…๋ ฅ ์ •๋ณด๋ฅผ ์ฝ์–ด์˜ฌ ๋•Œ ์‚ฌ์šฉ

 * scope
 - ํ•˜๋‚˜์˜ HTTP ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ์˜์—ญ
 - ์›น ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์š”์ฒญํ•  ๋•Œ ๋งˆ๋‹ค
   ์ƒˆ๋กœ์šด request ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋จ
 response  javax.servlet.http.HttpServletResponse  ์‘๋‹ต ์ •๋ณด๋ฅผ ์ €์žฅ
 ์‚ฌ์šฉ์ž ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ
 pageContext  javax.servlet.jsp.PageContext  JSP ํŽ˜์ด์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅ
 ๊ฐ์ข… ๊ธฐ๋ณธ ๊ฐ์ฒด๋ฅผ ์–ป๊ฑฐ๋‚˜, 
 forward ๋ฐ include ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•  ๋•Œ ์‚ฌ์šฉ

 * scope
 - ํ•œ๋ฒˆ์˜ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์— ๋Œ€ํ•˜์—ฌ
   ํ•˜๋‚˜์˜ JSP ํŽ˜์ด์ง€๊ฐ€ ํ˜ธ์ถœ

 -  ํŽ˜์ด์ง€ ์˜์—ญ์— ์ €์žฅํ•œ ๊ฐ’์€ ํŽ˜์ด์ง€๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด ์‚ฌ๋ผ์ง
 * ์ปค์Šคํ…€ ํƒœ๊ทธ์—์„œ ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ ์‚ฌ์šฉ
 session  javax.servlet.http.HttpSession  HTTP ์„ธ์…˜ ์ •๋ณด๋ฅผ ์ €์žฅ
 ํด๋ผ์ด์–ธํŠธ์— ๋Œ€ํ•œ ์„ธ์…˜ ์ •๋ณด๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ
 page directive์˜ session ์†์„ฑ์„ false๋กœ ํ•˜๋ฉด,
 ๋‚ด์žฅ ๊ฐ์ฒด๋Š” ์ƒ์„ฑ ๋˜์ง€ ์•Š์Œ

 * scope
 - ํ•˜๋‚˜์˜ ์›น ๋ธŒ๋ผ์šฐ์ €์™€ ๊ด€๋ จ๋œ ์˜์—ญ
 - ๊ฐ™์€ ์›น๋ธŒ๋ผ์šฐ์ € ๋‚ด์—์„œ
   ์š”์ฒญ๋˜๋Š” ํŽ˜์ด์ง€๋“ค์€ ๊ฐ™์€ ์„ธ์…˜์„ ๊ณต์œ 
 * ๋กœ๊ทธ์ธ ์ •๋ณด ๋“ฑ์„ ์ €์žฅ
 application  javax.servlet.ServletContext  ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅ
 ์›น ์„œ๋ฒ„์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฒ˜๋ฆฌ์™€
 ๊ด€๋ จ๋œ ์ •๋ณด๋ฅผ ๋ ˆํผ๋Ÿฐ์Šคํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ 

 * scope
 - ํ•˜๋‚˜์˜ ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜๊ณผ ๊ด€๋ จ๋œ ์˜์—ญ
 - ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋‹น 1๊ฐœ์˜ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋จ
 - ๊ฐ™์€ ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ
   ์š”์ฒญ๋˜๋Š” ํŽ˜์ด์ง€๋“ค์€ ๊ฐ™์€ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐ์ฒด๋ฅผ ๊ณต์œ 

 

๊ณตํ†ต method

servlet๊ณผ jsp ํŽ˜์ด์ง€ ๊ฐ„์— ํŠน์ • ์ •๋ณด๋ฅผ ์ฃผ๊ณ  ๋ฐ›๊ฑฐ๋‚˜ ๊ณต์œ  ํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”์†Œ๋“œ๋ฅผ ์ง€์›

// ๋ฌธ์ž์—ด name์œผ๋กœ Objectํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ
void setAttribute(String name, Object object)

// ๋ฌธ์ž์—ด name์— ํ•ด๋‹นํ•˜๋Š” ์†์„ฑ ๊ฐ’์ด ์žˆ๋‹ค๋ฉด, 
// Object ํ˜•ํƒœ๋กœ ๊ฐ€์ ธ์˜ค๊ณ  (์ ์ ˆํ•œ ํ˜•๋ณ€ํ™˜ ํ•„์š”)
// ์—†์œผ๋ฉด null์„ ๋ฆฌํ„ด
Object getAttribute(String name)

// ํ˜„์žฌ ๊ฐ์ฒด์— ์ €์žฅ๋œ ์†์„ฑ๋“ค์˜ ์ด๋ฆ„๋“ค์„ Enumeration ํ˜•ํƒœ๋กœ ๊ฐ€์ ธ์˜ด
Enumeration getattributenames()

// ๋ฌธ์ž์—ด name์— ํ•ด๋‹นํ•˜๋Š” ์†์„ฑ์„ ์‚ญ์ œ
void removeAttribute(String name)


// ๋ฌธ์ž์—ด name์— ํ•ด๋‹นํ•ญ๋Š” ์†์„ฑ ๊ฐ’์„ String ํ˜•ํƒœ๋กœ ์–ป์–ด์˜ด
String getParameter(String name)

// ์—ฌ๋Ÿฌ ๊ฐ’ ์ „๋‹ฌ ์‹œ ์‚ฌ์šฉ (ex. checkbox)
String[] getParameterValues(String name)

 

web page ์ด๋™

   forward(request, response)  sendRedirect(location)
 ์ด๋™ ๋ฒ”์œ„  ๋™์ผ ์„œ๋ฒ„(project) ๋‚ด ๊ฒฝ๋กœ   ๋™์ผ ์„œ๋ฒ„ ํฌํ•จ ํƒ€ URL ๊ฐ€๋Šฅ ex www.naver.com  ๋„ ๊ฐ€๋Šฅ!
 location bar  ๊ธฐ์กด URL ์œ ์ง€
(์‹ค์ œ ์ด๋™๋˜๋Š” ์ฃผ์†Œ ํ™•์ธ ๋ถˆ๊ฐ€)
 ์ด๋™ํ•˜๋Š” page๋กœ ๋ณ€๊ฒฝ
 ๊ฐ์ฒด  ๊ธฐ์กด์˜ request์™€ response๊ฐ€ ๊ทธ๋Œ€๋กœ ์ „๋‹ฌ  ๊ธฐ์กด์˜ request์™€ response๋Š” ์†Œ๋ฉธ๋˜๊ณ ,
 ์ƒˆ๋กœ์šด request์™€ response ์ƒ์„ฑ
 ์†๋„  ๋น„๊ต์  ๋น ๋ฆ„  forward()์— ๋น„ํ•ด ๋Š๋ฆผ
 ๋ฐ์ดํ„ฐ ์œ ์ง€  request์˜
 setAttribute(name, value)๋ฅผ ํ†ตํ•ด ์ „๋‹ฌ
 request๋กœ๋Š” data ์ €์žฅ ๋ถˆ๊ฐ€๋Šฅ.
 session์ด๋‚˜ cookie๋ฅผ ์ด์šฉ
// forward
RequestDispatcher dispatcher = request.getrequestdispatcher(path);
dispatcher.forward(request, response);

// sendRedirect
response.sendRedirect(location);

 

Servlet ์‹ค์Šต

controller ์—ญํ• ์€ servlet์ด !

view ์—ญํ• ์€ jsp๊ฐ€ ํ•ด์ฃผ๊ณ  ์žˆ์Œ์„ ํ™•์ธํ•ด๋ณด์ž.

 

โœ… servlet์„ ํ˜ธ์ถœํ•  JSP(HTML) ํ™”๋ฉด  ( regist.jsp )

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>์‚ฌ์šฉ์ž์ •๋ณด์‚ฌ์ดํŠธ</title>
<style>
	label {
		display: inline-block;
		width: 80px;
	}
</style>
</head>
<body>
	<h1>์‚ฌ์šฉ์ž ๊ด€๋ฆฌ</h1>
	<form method="post" action="main">
		<fieldset>
		<legend>์‚ฌ์šฉ์ž ์ •๋ณด ์ž…๋ ฅ</legend>
		<!-- front-controller pattern์—์„œ ์š”์ฒญ์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•œ parameter -->
		<input type="hidden" name="action" value="regist">
		<label for="id">์•„์ด๋””</label>
		<input type="text" id="id" name="id" value="hong"><br>
		<label for="password">๋น„๋ฐ€๋ฒˆํ˜ธ</label>
		<input type="password" id="password" name="password" value="1234"><br>
		<label for="name">์ด๋ฆ„</label>
		<input type="text" id="name" name="name" value="ํ™๊ธธ๋™"><br>
		<label for="email">์ด๋ฉ”์ผ</label>
		<input type="email" id="email" name="email" value="hong@email.com"><br>
		<label for="age">๋‚˜์ด</label>
		<input type="number" id="age" name="age" value=21><br>
		<input type="submit" value="๋“ฑ๋ก">
		<input type="reset" value="์ดˆ๊ธฐํ™”">
		</fieldset>
	</form>
</body>
</html>

 

โœ… ํ˜ธ์ถœ๋  servlet

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//์ด ์„œ๋ธ”๋ฆฟ์ด ํ˜ธ์ถœ๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” url ์ƒ์— http://server_ip:port/context_name/main ์ด ํ•„์š”ํ•จ
@WebServlet("/main")
public class MainServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	
    /**
     * get ๋ฐฉ์‹์˜ ์š”์ฒญ์— ๋Œ€ํ•ด ์‘๋‹ตํ•˜๋Š” ๋ฉ”์„œ๋“œ
     * front controller pattern์„ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋‚ด๋ถ€์ ์œผ๋กœ process๋ฅผ ํ˜ธ์ถœ
     */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		process(request, response);
	}
	
	
    /**
     * post ๋ฐฉ์‹์˜ ์š”์ฒญ์— ๋Œ€ํ•ด ์‘๋‹ตํ•˜๋Š” ๋ฉ”์„œ๋“œ
     * front controller pattern์„ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋‚ด๋ถ€์ ์œผ๋กœ process๋ฅผ ํ˜ธ์ถœ
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // post ์š”์ฒญ ์‹œ ํ•œ๊ธ€ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด encoding์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.
        request.setCharacterEncoding("utf-8");
        process(request, response);
    }
	
    /**
     * request ๊ฐ์ฒด์—์„œ action ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ถ”์ถœํ•ด์„œ ์‹ค์ œ ๋น„์ง€๋‹ˆ์Šค ๋กœ์ง ๋ฉ”์„œ๋“œ(ex: doRegist) ํ˜ธ์ถœ
     */
	private void process(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getParameter("action");
		switch (action) {
		case "regist":
			doRegist(request, response);
			break;
		}
	}
	
    /**
     * ํด๋ผ์ด์–ธํŠธ์—์„œ ์ „๋‹ฌ๋œ request๋ฅผ ๋ถ„์„ํ•œ ๊ฒฐ๊ณผ๋ฅผ regist_result.jsp์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ํ•จ
     * RequestDispatcher๋ฅผ ์‚ฌ์šฉํ•ด์„œ regist_result.jsp๋กœ forwardํ•œ๋‹ค.
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
	private void doRegist(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// request ๊ฐ์ฒด์—์„œ ์ „๋‹ฌ๋œ parameter๋ฅผ ์ถ”์ถœ
		String id = request.getParameter("id");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String email = request.getParameter("email");
		// ๋ฌธ์ž์—ด๋กœ ์ „๋‹ฌ๋œ age๋Š” ์ˆซ์ž๋กœ ๋ณ€ํ™˜
		int age = Integer.parseInt(request.getParameter("age"));
		
		// ์ „๋‹ฌ๋ฐ›์€ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ request์— ๋‹ด์Œ
		request.setAttribute("id", id);
		request.setAttribute("password", password);
		request.setAttribute("name", name);
		request.setAttribute("email", email);
		request.setAttribute("age", age);
		
		// reuqest์— ๋‹ด์€ ์ •๋ณด๋“ค์„ ๊ฒฐ๊ณผ ํŽ˜์ด์ง€์— ๋„˜๊ฒจ์คŒ
		RequestDispatcher disp = request.getRequestDispatcher("/regist_result.jsp");
		disp.forward(request, response);
		
	}
}

 

โœ… servlet์ด ๋„˜๊ฒจ์ค„ JSP(HTML) ํ™”๋ฉด ( regist_result.jsp )

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>์‚ฌ์šฉ์ž ๋“ฑ๋ก ๊ฒฐ๊ณผ</title>
<style>
table {
	border-collapse: collapse;
	width: 100%;
}

th, td {
	border: 1px solid black;
}

th:nth-child(1) {
	width: 120px;
}
</style>
</head>
<body>
	<h1>์‚ฌ์šฉ์ž ๋“ฑ๋ก ๊ฒฐ๊ณผ</h1>
	<h2>๋“ฑ๋ก๋œ ์‚ฌ์šฉ์ž ์ •๋ณด</h2>
	<%-- ํ…Œ์ด๋ธ” ๋‚ด์—์„œ User์˜ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด expression tag๋ฅผ ์‚ฌ์šฉ --%>
	<table>
		<thead>
			<tr>
				<th>ํ•ญ๋ชฉ</th>
				<th>๋‚ด์šฉ</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td>์•„์ด๋””</td>
				<td><%=request.getParameter("id")%></td>
			</tr>
			<tr>
				<td>๋น„๋ฐ€๋ฒˆํ˜ธ</td>
				<td><%=request.getParameter("password")%></td>
			</tr>
			<tr>
				<td>์ด๋ฆ„</td>
				<td><%=request.getParameter("name")%></td>
			</tr>
			<tr>
				<td>์ด๋ฉ”์ผ</td>
				<td><%=request.getParameter("email")%></td>
			</tr>
			<tr>
				<td>๋‚˜์ด</td>
				<td><%=request.getParameter("age")%></td>
			</tr>
		</tbody>
	</table>
	<!-- ๋‹ค์‹œ ์‚ฌ์šฉ์ž๋ฅผ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ๋Š” ๋งํฌ๋ฅผ ์ œ๊ณต -->
	<a href="regist.jsp">์ถ”๊ฐ€๋“ฑ๋ก</a>
</body>
</html>

 

 

๐Ÿ’ก Servlet๊ณผ JSP ๋น„๊ต

๊ณตํ†ต์ 
 - ์›น ํŽ˜์ด์ง€๋ฅผ ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•˜๋Š” ์„œ๋ฒ„ ํ”„๋กœ๊ทธ๋žจ

์ฐจ์ด์ 
 - Servlet : Java ์ฝ”๋“œ ๋‚ด HTML
 - JSP : HTML ๋‚ด Java ์ฝ”๋“œ

 

Servlet + JSP

์ตœ๊ทผ์—๋Š” Servlet๊ณผ JSP๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ์›น์„ ๊ตฌํ˜„ํ•˜๋Š” MVCํŒจํ„ด์˜ Model 2 ๋ฐฉ์‹์ด ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ์Œ

  • View → HTML์ด ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— JSP๋ฅผ ์‚ฌ์šฉ
  • Controller → Java๊ฐ€ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— Serlvet์œผ๋กœ ์ž‘์„ฑ

'WEB > back-end' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Interceptor  (0) 2023.04.22
AOP(Aspect Oriented Programming, ๊ด€์  ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ)  (0) 2023.04.22
Spring  (0) 2023.04.21
JDBC๋ฅผ ์ด์šฉํ•œ DB ์—ฐ๊ฒฐ (MySQL)  (0) 2023.03.24
DAO, DTO, Entity ๋ž€?  (0) 2023.03.22
profile

danbibibi

@danbibibi

๊ฟˆ์„ ๊พธ๋Š” ์‹œ๊ฐ„์€ ๋ฉˆ์ถฐ ์žˆ๋Š” ๊ฒƒ์ด ์•„๋ƒ ๋‘๋ ค์›Œํ•˜์ง€ ๋งˆ ๋ฉˆ์ถ”์ง€ ๋งˆ ํ‘ธ๋ฅธ ๊ฟˆ์†์œผ๋กœ