๐Ÿ’ป

JSP ํ”„๋กœ๊ทธ๋ž˜๋ฐ - [Servlet] Servlet ๊ฐ์ฒด ๋ฒ”์œ„ ๋ณธ๋ฌธ

KITRI/JSP

JSP ํ”„๋กœ๊ทธ๋ž˜๋ฐ - [Servlet] Servlet ๊ฐ์ฒด ๋ฒ”์œ„

๋˜ํšจ๋‹ˆ 2020. 6. 9. 10:41

๊ฐ์ฒด ๋ฒ”์œ„

page < request < session < application

 

 

๊ฐ์ฒด ๋ฒ”์œ„ ์ข…๋ฅ˜

  1. page ์˜์—ญ
    • JSP ํŒŒ์ผ์—๋Š” pageContext๊ฐ€ ๋‚ด์žฅ๋˜์–ด ์žˆ์œผ๋ฉฐ, ์ด ๊ฐ์ฒด๋Š” page ์˜์—ญ์—์„œ๋งŒ ์œ ํšจ
    • JSP ํŒŒ์ผ์— <% %> ์•ˆ์— ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ด ๋ณ€์ˆ˜๋Š” ํ•ด๋‹น JSP ํŒŒ์ผ ๋‚ด์—์„œ๋งŒ ์œ ํšจํ•œ๋ฐ, page ์Šค์ฝ”ํ”„์— ์ •์˜๋œ ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
    • ํ•œ ๋ฒˆ์˜ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์ด ์˜ค๋ฉด, ํ•˜๋‚˜์˜ JSP ํŽ˜์ด์ง€๊ฐ€ ์‘๋‹ต
    • page ์˜์—ญ์€ ์ด ๋•Œ ํ•˜๋‚˜์˜ JSP ํŽ˜์ด์ง€ ๋‚ด์—์„œ๋งŒ ๊ฐ์ฒด๋ฅผ ๊ณต์œ ํ•˜๋Š” ์˜์—ญ์„ ์˜๋ฏธ
  2. request ์˜์—ญ
    • Servlet์—์„œ๋Š” reqeust.setAttribute("์ด๋ฆ„", ๊ฐ์ฒด); ๋กœ ์ „๋‹ฌํ•˜๊ณ , JSP์—์„œ๋Š” Obejct ob j =  request.getAttribute("์ด๋ฆ„"); ์œผ๋กœ ๋ฐ›๋Š”๋‹ค. ์ด ๋•Œ ์‚ฌ์šฉํ–ˆ๋˜ request ๊ฐ์ฒด๊ฐ€ request ์˜์—ญ์ด๋‹ค.
    • ์š”์ฒญ์„ ๋ฐ›์•„์„œ ์‘๋‹ตํ•˜๊ธฐ๊นŒ์ง€ ๊ฐ์ฒด๊ฐ€ ์œ ํšจํ•œ ์˜์—ญ
    • Servlet์—์„œ forward ๋˜๋Š” include๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, request ์š”์ฒญ ๊ฐ์ฒด๊ฐ€ ๊ณต์œ ๋˜์–ด์„œ request ์˜์—ญ์ด ๋œ๋‹ค.
    • forward๋ฅผ ๋” ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค. 
    • ์ง€๊ธˆ๊นŒ์ง€ Servlet์—์„œ JSP๋กœ ๊ฐ์ฒด๋ฅผ ๋ณด๋‚ผ ๋•Œ ์‚ฌ์šฉํ–ˆ๋˜ ๋ฐฉ๋ฒ•
  3. session ์˜์—ญ
    • ๊ฐ™์€ ๋ธŒ๋ผ์šฐ์ € ๋‚ด์—์„œ ์š”์ฒญ๋˜๋Š” ํŽ˜์ด์ง€๋“ค์€ ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ๊ณต์œ ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ์ด๋ฅผ ์„ธ์…˜ ์˜์—ญ์ด๋ผ๊ณ  ํ•œ๋‹ค.
    • ํ•˜๋‚˜์˜ ๋ธŒ๋ผ์šฐ์ € ๋‹น 1๊ฐœ์˜ session ๊ฐ์ฒด ์ƒ์„ฑ
    • ์„ธ์…˜ ์ข…๋ฃŒ ํ›„ ๊ฐ์ฒด ๋ฐ˜ํ™˜
    • requset.getSession() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์„ธ์…˜ ์˜์—ญ์˜ ๊ฐ์ฒด๋ฅผ ์–ป๋Š”๋‹ค.
  4. application ์˜์—ญ
    • ๊ฐ™์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋‚ด์—์„œ ์š”์ฒญ๋˜๋Š” ํŽ˜์ด์ง€๋“ค์€ ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ๊ณต์œ ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋ฅผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์˜์—ญ์ด๋ผ๊ณ  ํ•œ๋‹ค.
    • ํ•˜๋‚˜์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋‹น 1๊ฐœ์˜ application ๊ฐ์ฒด ์ƒ์„ฑ
    • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ข…๋ฃŒ ํ›„ ๊ฐ์ฒด ๋ฐ˜ํ™˜
    • request.getServletContext() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์–ป๋Š”๋‹ค. 

 

 


โ€ป redirect, forward, include ์ž˜ ์•Œ์•„๋‘๊ธฐ!

-> response๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” sendRedirect(ํŽ˜์ด์ง€ ์ด๋™), ๊ฐ•์ œ๋กœ ์ด๋™ํ•ด์ค„๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. 

-> forward include(๋‘˜ ๋‹ค ๋ฐ์ดํ„ฐ ๋˜์ ธ์คŒ) ์ฐจ์ด์ (include๋Š” ์ œ์–ด๊ถŒ์ด ๋Œ์•„์˜ค๊ณ , forward๋Š” ๋‹ค์‹œ ๋Œ์•„์˜ค์ง€ ์•Š๋Š”๋‹ค.) 

-> forward๋ฅผ ์ œ์ผ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค. 

 

 

08_example.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>		
	<h3>include</h3>
	<form action="http://localhost:8181/webTesting/com/java/servlet/Example08" method="get">
		<input type="text" name="message"/>
		
		<input type="submit" value="์ „์†ก"/>
		<input type="reset" value="์ทจ์†Œ"/>
	</form>
	<br /><br /><br />

	<h3>forward</h3>
	<form action="http://localhost:8181/webTesting/com/java/servlet/Example08" method="post">
		<input type="text" name="message"/>
		
		<input type="submit" value="์ „์†ก"/>
		<input type="reset" value="์ทจ์†Œ"/>
	</form>
	<br /><br /><br />

</body>
</html>

 

 

Example08.java

package com.java.servlet;

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

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;

/**
 * Servlet implementation class Example08
 */
public class Example08 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Example08() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//include
		request.setCharacterEncoding("utf-8");
		String message = request.getParameter("message");
		System.out.println(message);
	
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print("<html>");
		out.print("<head><title></title></head>");
		out.print("<body>");
		
		request.setAttribute("name", "ํ™๊ธธ๋™");
		request.setAttribute("phone", "010-1234-5678");
		
		RequestDispatcher rd = request.getRequestDispatcher("/com/java/servlet/Example08_Sub");
		rd.include(request, response);
		
		out.print("<hr color='red' width='400px'/>");
		out.print("<h3> include๋Š” ๋‹ค์‹œ ๋Œ์•„์˜จ๋‹ค. ์ œ์–ด๊ถŒ์„ ๋„˜๊ฒจ์ฃผ์ง€ ์•Š๋Š”๋‹ค. </h3>");
		out.print("<h3> ์„œ๋ธ”๋ฆฟ์—์„œ ๋ฐ์ดํ„ฐ ์ƒ๋ช…์ฃผ๊ธฐ pageScope, requestScope, sessionScope, applicationScope  </h3>");
		out.print("</body>");
		out.print("</html>");
		out.close();
		
		//close() include๋ฐฉ์‹์—์„œ๋Š” ์ŠคํŠธ๋ฆผ์ด ๋‹ซํ˜€์„œ ์ถœ๋ ฅ ์•ˆ๋œ๋‹ค. forward๋Š” ๋œ๋‹ค. 
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//forward
		request.setCharacterEncoding("utf-8");
		String message = request.getParameter("message");
		System.out.println(message);
	
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print("<html>");
		out.print("<head><title></title></head>");
		out.print("<body>");
		
		request.setAttribute("name", "ํ™๊ธธ๋™");
		request.setAttribute("phone", "010-1234-5678");
		
		RequestDispatcher rd = request.getRequestDispatcher("/com/java/servlet/Example08_Sub");
		rd.forward(request, response);
		
		out.print("<hr color='red' width='400px'/>");
		out.print("<h3> include๋Š” ๋‹ค์‹œ ๋Œ์•„์˜จ๋‹ค. ์ œ์–ด๊ถŒ์„ ๋„˜๊ฒจ์ฃผ์ง€ ์•Š๋Š”๋‹ค. </h3>");
		out.print("<h3> ์„œ๋ธ”๋ฆฟ์—์„œ ๋ฐ์ดํ„ฐ ์ƒ๋ช…์ฃผ๊ธฐ pageScope, requestScope, sessionScope, applicationScope  </h3>");
		out.print("</body>");
		out.print("</html>");
		out.close();
	}

}

 

 

 

Example08_Sub.java

package com.java.servlet;

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

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

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

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String message = request.getParameter("message");
		String name = (String) request.getAttribute("name"); //upCasting
		String phone = (String) request.getAttribute("phone"); //upCasting
		
		System.out.println("Example08_Sub" +message);
		
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print("<html>");
		out.print("<head><title></title></head>");
		out.print("<body>");
		
		out.print("<h3 style = 'color:blue' width='400px'>" + message + "</h3>");
		out.print("<h3 style = 'color:blue' width='400px'>" + name + "</h3>");
		out.print("<h3 style = 'color:blue' width='400px'>" + phone + "</h3>");
		out.print("</body>");
		out.print("</html>");
		// close๋ฅผ ์“ฐ๋ฉด ํŽ˜์ด์ง€๋ฅผ ๋‹ค์‹œ ๋Œ์•„๊ฐˆ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์“ฐ๋ฉด ์•ˆ๋œ๋‹ค.
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

 

 

< include ๋ฐฉ์‹ ๊ฒฐ๊ณผ >

 

 

 

< forward ๋ฐฉ์‹ ๊ฒฐ๊ณผ >

๋ฐ˜์‘ํ˜•
Comments