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 방식 κ²°κ³Ό >

λ°˜μ‘ν˜•