2014-04-30 2 views
0

안녕하세요, TimerTask을 사용하여 배열 목록에 액세스하려고합니다.JSP 페이지에서 TimerTask를 사용하는 방법?

ScheduledTask

import java.util.TimerTask; 
import java.util.Date; 
public class ScheduledTask extends TimerTask { 
public void run() { 

    } 
} 
: 5 번째 시간 간격 여기

사이의 JSP 페이지에 대한 모든 목록 내 코드는 것을 나는 서로 다른 두 하위 목록의 배열 목록 값을 얻으려면 다음 인쇄 할

My.jsp 내가 원하는을 달성 할 수있는 방법

<%@ page import="Demo.ScheduledTask"%> 
<html> 
<head> 

    <title>JSP Page</title> 
</head> 
<body> 
    <jsp:useBean id="test" class="Demo.ScheduledTask" /> 
    <% 

    Timer time = new Timer(); // Instantiate Timer Object 
     ScheduledTask st = new ScheduledTask(); // Instantiate SheduledTask class 
     time.schedule(st, 0, 1000); 
        for (int i = 0; i <3; i++) { 
      URL url; 
     try { 
      // get URL content 

      String a = "http://122.160.81.37:8080/mandic/commoditywise?c=paddy"; 
      url = new URL(a); 
      URLConnection conn = url.openConnection(); 

      // open the stream and put it into BufferedReader 
      BufferedReader br = new BufferedReader(
        new InputStreamReader(conn.getInputStream())); 
      StringBuffer sb = new StringBuffer(); 
      String inputLine; 
      ArrayList<String> list1 = new ArrayList<String>(); 
      ArrayList<String> list2 = new ArrayList<String>(); 
      List sublist1=null; 
      List sublist2=null; 
      List sublist3=null; 
      while ((inputLine = br.readLine()) != null) { 

       String s = inputLine.replace("|", "\n"); 

       s = s.replace("~", " "); 
       //System.out.println(s); 
       StringTokenizer str = new StringTokenizer(s); 
       while (str.hasMoreTokens()) { 
        String mandi = str.nextElement().toString(); 
        String price = str.nextElement().toString(); 
        list1.add(mandi); 
        list2.add(price); 

       } 
      } sublist1=list1.subList(0, 15); 
      sublist2=list1.subList(16,30); 
      sublist3=list1.subList(31,45); 
      String item1 = null; 
      for (int l = 0; l < sublist1.size(); l++) 

      { 
       out.println("List1"+sublist1.get(l)); 
      } 
      Thread.sleep(5000); 
       for(int j=0;j<sublist2.size();j++) 
       { 
        out.println("List2"+sublist2.get(j)); 

       } 
       Thread.sleep(5000); 
       for(int k=0;k<sublist2.size();k++){ 
        out.println("List3"+sublist3.get(k)); 
      } 
      Thread.sleep(5000); 
      if (i == 2) 
      { 
       out.println("Application Terminates"); 
       System.exit(0); 
      } 

      br.close(); 


     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 


     } 
    %> 

</body> 
</html> 

산출.

미리 감사드립니다.

+0

http://stackoverflow.com/questions/307964/countdown-timer-in-jsp-servlets 바로 r에이 – guptakvgaurav

답변

1

사용자가 5 초마다 새로운 콘텐츠를 보길 원합니다. JSP가 브라우저로 보내지는 html 페이지를 생성하기 때문에 JSP에서는 이것을 할 수 없습니다. 브라우저에서 실행되지 않습니다.

당신은

    자바 스크립트에 시간 제한 조치를 다시 작성
  • 수 있습니다. 이것은 broswer에서 실행됩니다.
  • 자바 스크립트에서 ajax 콜백을 설정하면 타이머 작업 내에서 수행 한 작업을 주기적으로 호출하여 브라우저로 보내 UI를 업데이트 할 수 있습니다.
+0

이 U 그를 위해 코드를 작성하지 않는 나를 위해 – user3585120

+2

일을 몇 가지 코드를 작성할 수 있습니다 확인하십시오. –

+0

[자바 스크립트 타이밍 이벤트] (http://javascript.info/tutorial/settimeout-setinterval)를 사용해야합니다. –

관련 문제