ServletContext 를 이용한 데이터 공유

2022. 12. 8. 23:03JSP

여러개의 Servlet 에서 데이터를 공유해야 할 경우에 context parameter를 사용한다.

web.xml 파일에 데이터를 작성하면, Servlet 에서 공유할 수 있다.

 

순서

Servlet 클래스 제작

web.xml 파일에 context parameter 기술

ServletContext 메소드를 이용해서 데이터를 사용한다.

앞에서는 특정한 서블릿 하나에만 해당되는 초기화값을 썼고 컨텍스트 파라미터 방법은 여러개의 서블릿에서 초기화값을 공유하는 방법이다.

getServletContext() : ServletContext를 얻어오는 메소드

 

servlet01 프로젝트 내 Java Resources 내 src 내 com.test.ex 패키지 내 ServletContext_Ex.java

package com.test.ex;

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;

//ServletContext를 이용한 데이터 공유

// 여러개의 Servlet에서 데이터를 공유해야 할 경우에 context parameter를 사용한다.
// web.xml파일에 데이터를 작성하면, Servlet에서 공유할 수 있다.

// 순서
// Servlet클래스 제작
// web.xml파일에 context parameter 기술
// ServletContext메소드를 이용해서 데이터를 사용한다.
// 앞에서는 특정한 서블릿하나에만 해당되는 초기화값을 썼고 컨텍스트 파라미터 방법은 여러개의 서블릿에서 초기화값을 공유하는 방법이다.
// getServletContext() : ServletContext를 얻어오는 메소드

/**
 * Servlet implementation class ServletContext_Ex
 */
@WebServlet(name = "SCEx", urlPatterns = { "/SCEx" })
public class ServletContext_Ex extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletContext_Ex() {
        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
		String id = getServletContext().getInitParameter("id");
		String pw = getServletContext().getInitParameter("password");
		String local = getServletContext().getInitParameter("local");
		
//		response.setContentType("text/html; charset=utf-8");
//		PrintWriter out = response.getWriter();//출력스트림얻기
		
		System.out.println("id : " + id);
		System.out.println("pw : " + pw);
		System.out.println("local : " + local);
	}

	/**
	 * @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);
	}

}

servlet01 프로젝트 내 WebContent 내 WEB-INF 내 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>servlet01</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
    <param-name>id</param-name>
    <param-value>test33</param-value>
  </context-param>
  <context-param>
    <param-name>password</param-name>
    <param-value>1234</param-value>
  </context-param>
  <context-param>
    <param-name>local</param-name>
    <param-value>kwangju</param-value>
  </context-param>
  
  <listener>
  	<listener-class>com.test.ex.ContextL</listener-class>
  </listener>
  
</web-app>

'JSP' 카테고리의 다른 글

표현식  (0) 2022.12.10
파일 업로드  (0) 2022.12.10
Servlet 사이클  (0) 2022.12.08
ServletContextListener  (0) 2022.12.08
Servlet 초기화  (0) 2022.12.08