글쓴이 :최 란희 2000년 3월 15일 13:51:51
thread 클래스의 아래의 run()에서...
out.println("run<center><BR>"); --- 1)
System.out.println("ba bo"); --- 2)
2)은 실행되어 보이는 데 1)은 보이질 않는군요.
------------ 소스 -------------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class ChatServlet extends HttpServlet{
String RANDOMSTRING = "\n--ThisRandomString\n";
public Vector dialog = new Vector(); // 대화 저장 공간
private String clientMsg = null; //클라이언트가 입력한 대화
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
for(int i = 0; i< 100; i++)
dialog.add("사랑해");
//쓰고자 하는 html을 초기화, 헤더 작성..
initHTML(req, res);
//언제든지 queue무엇이 있던지 꺼내서 쓰고 없앤다.
Thread getMsg = new Thread(new ObtainMsg(res, this));
getMsg.start();
}
public void initHTML(HttpServletRequest req, HttpServletResponse res) throws IOException{
res.setContentType("multipart/x-mixed-replace;boundary=ThisRandomString");
PrintWriter out = new PrintWriter(res.getOutputStream(),true);
res.setHeader("pragma", "no-cache");
res.setHeader("boundary",RANDOMSTRING);
res.setContentType("text/html");
res.setHeader("pragma", "no-cache");
res.setHeader("boundary",RANDOMSTRING);
out.println("<html><title> servlet example : hello world </title>");
out.println("<body bgcolor=#ffffff><center><BR>");
out.println("dihj<center><BR>");
out.close();
}
}
------------- thread 소스 ----------------------
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ObtainMsg implements Runnable{
private PrintWriter out;
private Vector dialog;
private final int waitTime = 100; //queue에 message가 입력되도록 기다리는 시간..
ObtainMsg(HttpServletResponse res, ChatServlet chat){
try{
out = new PrintWriter(res.getOutputStream(),true);
}catch(IOException IOe){}
dialog = chat.dialog;
}
public void run(){
//test용
out.println("run<center><BR>");
System.out.println("ba bo");
while(true){
//queue가 비어 있으면 0.1초 동안 기다린다.
if(dialog.isEmpty()){
try{
Thread.sleep(waitTime);
}catch(InterruptedException IE){}
}
//queue에 무엇이라도 있으면 이것을 쓰게된다.
else{
out.println("write<center><BR>");
Enumeration queue = dialog.elements();
writeBoard(queue);
dialog.removeAllElements();
}
}//while
}//RUN
//대화 내용에 board에 쓰게 된다.
public void writeBoard(Enumeration queue){
while(queue.hasMoreElements()){
String message = (String)queue.nextElement();
out.print(message + "\n");
}
out.close();
}//writeboard
}//CLSSS Obtain