Coding/JSP

[Servlet] Servlet 객체 범위

Soocong 2022. 4. 4. 11:29

객체 범위

page < request < session < application

 

 

객체 범위 종류

  1. page 영역
  2. request 영역
  3. session 영역
  4. application 영역

 

 


※ 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 방식 결과 >