servlet¿¡¼­ Thread¸¦ »ç¿ëÇÒ ½Ã¿¡...


[ ´ÙÀ½ ±Ûµé ] [ À̾ ±Û¿Ã¸®±â(´äÇϱâ) ] [ ÀÚ¹Ù ¹¯°í ´äÇϱâ ]

±Û¾´ÀÌ :ÃÖ ¶õÈñ 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





´ÙÀ½ ±Ûµé:



À̾ ±Û¿Ã¸®±â(´äÇϱâ)

À̸§:
E-Mail:
Á¦¸ñ:
³»¿ë:
HTML ÅÂ±× Æ÷ÇÔ ¿©ºÎ: HTML ¹®¼­ÀÏ °æ¿ì üũ
°ü·Ã URL(¼±ÅÃ):
URL Á¦¸ñ(¼±ÅÃ):
°ü·Ã À̹ÌÁö URL:


[ ´ÙÀ½ ±Ûµé ] [ À̾ ±Û¿Ã¸®±â(´äÇϱâ) ] [ ÀÚ¹Ù ¹¯°í ´äÇϱâ ]