학습목표 : 웹 시스템을 통해 서버에서 싱글톤이 구현되는 부분을 확인한다.
부제 : 여러 사용자의 접근을 통해 웹에서 싱글톤을 확인하자.
1. 웹 프로젝트 실행 (New - Dynamic Web Project)
2. NaSingleClass.java 생성 (싱글톤 객체)
package singleton;
public class NaSingleClass {
private String strName;
// 클래스 참조 변수 선언
// 해당 값을 static 으로 선언하여, 메모리에 올려, 모두가 참조하게 함(포인터)
private static NaSingleClass singleInstance;
// 생성자가 private , new 접근 할 수 없음
private NaSingleClass() {
}
// 해당 함수를 통해서만, 객체화가능,,,
// 해당 함수를 통해서만, 객체화가능,,,
//public static NaSingleClass getSingleInstance() {
//public static synchronized NaSingleClass getSingleInstance() {
public static synchronized NaSingleClass getSingleInstance() {
if(singleInstance ==null) {
synchronized (NaSingleClass.class) {
if(singleInstance ==null) {
singleInstance = new NaSingleClass();
}
}
}
return singleInstance;
}
public String getStrName() {
return strName;
}
public void setStrName(String strName) {
this.strName = strName;
}
}
3. NaSingleClassNo.java (일반 자바 클래스, static 변수)
package no_singleton;
public class NaSingleClassNo {
private static String strName;
public NaSingleClassNo() {
}
public String getStrName() {
return strName;
}
public void setStrName(String strName) {
this.strName = strName;
}
}
4. hello.java (Servlet , 생성) , parameter (name=~~)
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;
import singleton.NaSingleClass;
import no_singleton.NaSingleClassNo;
/**
* Servlet implementation class hello
*/
@WebServlet("/hello")
public class hello extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public hello() {
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
response.getWriter().append("Served at: ").append(request.getContextPath());
String strIp = request.getRemoteAddr();
String strName = request.getParameter("name");
System.out.println("\n\n\n\n ================ No Single =========================="+strIp);
// No Single
NaSingleClassNo na = new NaSingleClassNo();
NaSingleClassNo na2 = new NaSingleClassNo();
NaSingleClassNo na3 = new NaSingleClassNo();
na.setStrName("Kim");
if(strName !=null) {
na.setStrName(strName);
}
System.out.printf("na , %s , %s \n", na, na.getStrName());
System.out.printf("na2 , %s , %s \n", na2, na2.getStrName());
System.out.printf("na3 , %s , %s \n", na3, na3.getStrName());
System.out.println("\n ================ Single =========================="+strIp);
// Single
NaSingleClass naS = NaSingleClass.getSingleInstance();
NaSingleClass naS2 = NaSingleClass.getSingleInstance();
NaSingleClass naS3 = NaSingleClass.getSingleInstance();
naS.setStrName("Kim");
if(strName !=null) {
naS.setStrName(strName);
}
System.out.printf("naS , %s , %s \n", naS, naS.getStrName());
System.out.printf("naS2 , %s , %s \n", naS2, naS2.getStrName());;
System.out.printf("naS3 , %s , %s \n", naS3, naS3.getStrName());;
/* 아래가 바로 초창기 Servlet 의 위엄... */
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter w = response.getWriter();
w.write("<html>\n" +
"<head>\n" +
"<meta charset=\"UTF-8\">\n" +
"</head>\n" +
"<body>\n" +
"<p> =============== No Single =========================="+strIp+
"<ul>\n" +
" <li>na Address = "+na +" , _______Name: "+na.getStrName() +"</li>\n" +
" <li>na2 Address ="+na2+" , _______Name: "+na2.getStrName() +"</li>\n" +
" <li>na3 Address ="+na3+" , _______Name: "+na3.getStrName()+"</li>\n" +
"</ul>\n" +
"<p> =============== Single =========================="+strIp+
"<ul>\n" +
" <li>naS Address = "+naS +" , _______Name: "+naS.getStrName() +"</li>\n" +
" <li>naS2 Address ="+naS2+" , _______Name: "+naS2.getStrName() +"</li>\n" +
" <li>naS3 Address ="+naS3+" , _______Name: "+naS3.getStrName()+"</li>\n" +
"</ul>\n" +
"</body>\n" +
"</html>");
}
/**
* @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);
}
}
5. hello_2.java (Servlet , 생성) 단순 조회
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;
import no_singleton.NaSingleClassNo;
import singleton.NaSingleClass;
/**
* Servlet implementation class hello_2
*/
@WebServlet("/hello_2")
public class hello_2 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public hello_2() {
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
response.getWriter().append("Served at: ").append(request.getContextPath());
String strIp = request.getRemoteAddr();
System.out.println("\n\n\n\n ================ No Single =========================="+strIp);
// No Single
NaSingleClassNo na = new NaSingleClassNo();
NaSingleClassNo na2 = new NaSingleClassNo();
NaSingleClassNo na3 = new NaSingleClassNo();
System.out.printf("na , %s , %s \n", na, na.getStrName());
System.out.printf("na2 , %s , %s \n", na2, na2.getStrName());
System.out.printf("na3 , %s , %s \n", na3, na3.getStrName());
System.out.println("\n ================ No Single =========================="+strIp);
// Single
NaSingleClass naS = NaSingleClass.getSingleInstance();
NaSingleClass naS2 = NaSingleClass.getSingleInstance();
NaSingleClass naS3 = NaSingleClass.getSingleInstance();
System.out.printf("naS , %s , %s \n", naS, naS.getStrName());
System.out.printf("naS2 , %s , %s \n", naS2, naS2.getStrName());;
System.out.printf("naS3 , %s , %s \n", naS3, naS3.getStrName());;
/* 아래가 바로 초창기 Servlet 의 위엄... */
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter w = response.getWriter();
w.write("<html>\n" +
"<head>\n" +
"<meta charset=\"UTF-8\">\n" +
"</head>\n" +
"<body>\n" +
"<p> =============== No Single =========================="+strIp+
"<ul>\n" +
" <li>na Address = "+na +" , _______Name: "+na.getStrName() +"</li>\n" +
" <li>na2 Address ="+na2+" , _______Name: "+na2.getStrName() +"</li>\n" +
" <li>na3 Address ="+na3+" , _______Name: "+na3.getStrName()+"</li>\n" +
"</ul>\n" +
"<p> =============== Single =========================="+strIp+
"<ul>\n" +
" <li>naS Address = "+naS +" , _______Name: "+naS.getStrName() +"</li>\n" +
" <li>naS2 Address ="+naS2+" , _______Name: "+naS2.getStrName() +"</li>\n" +
" <li>naS3 Address ="+naS3+" , _______Name: "+naS3.getStrName()+"</li>\n" +
"</ul>\n" +
"</body>\n" +
"</html>");
}
/**
* @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);
}
}
# Test : http://localhost:8080/SingleTonWebTest/hello?name=KIM
# Test : http://localhost:8080/SingleTonWebTest/hello_2
'객체지향 (OOP)' 카테고리의 다른 글
객체지향(OOP) 4대 특성 (0) | 2021.05.10 |
---|---|
[추상화] abstraction (3) | 2021.05.03 |
Java Application - 싱글톤 패턴(Singleton pattern) (0) | 2021.04.29 |