글쓴이 :최란희 2000년 3월 17일 16:46:58
In Reply to: ... posted by 방문객 on 2000년 3월 16일 16:09:52:
위에서 이야기하신 이유를 모르겠네요...
아래와 같이 조금 변형을 시켰는데..
조금 쓰다가 마는 군요..
//쓰레드로 html에 무한히 쓰는 방식으로 할려고 하거든요 그런데 중간에 쓰다가 마는 군요...
왜 그럴까요..
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class ChatGood extends HttpServlet implements Runnable{
public Vector dialog = new Vector(); // 대화 저장 공간
private String clientMsg = null; //클라이언트가 입력한 대화
private PrintWriter out = null;
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
out = new PrintWriter(res.getOutputStream(),true);
for(int i = 0; i< 10; i++)
dialog.add("사랑해");
//쓰고자 하는 html을 초기화, 헤더 작성..
initHTML(req, res);
//언제든지 queue무엇이 있던지 꺼내서 쓰고 없앤다.
Thread getMsg = new Thread(this);
getMsg.start();
// write(req, res);
}
public void run(){
//test용
if(out == null)
System.out.println("ba bo");
else
System.out.println("ba");
out.println("run<center><BR>");
int count = 0;
while(true){
count++;
out.println("write<center><BR>");
Enumeration queue = dialog.elements();
writeBoard(queue);
System.out.println(count);
}//while
}//RUN
//대화 내용에 board에 쓰게 된다.
public void writeBoard(Enumeration queue) {
while(queue.hasMoreElements()){
String message = (String)queue.nextElement();
out.print(message + "<BR>");
}
}//writeboard
public void initHTML(HttpServletRequest req, HttpServletResponse res) throws IOException{
// PrintWriter out = new PrintWriter(res.getOutputStream(),true);
res.setContentType("text/html");
out.println("<html><title> servlet example : hello world </title>");
out.println("<body bgcolor=#ffffff><center><BR>");
out.println("dif<center><BR>");
}
public void addMessage(String clientMsg){
dialog.add(clientMsg);
}
public void write(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
PrintWriter toClient = new PrintWriter(res.getOutputStream(),true);
toClient.println("<h1> Hello World </h1><BR><BR>");
toClient.println("<h4> This is servlet's output </h4><BR><BR>");
toClient.println("R U feel ");
toClient.close();
}
}