2015-04-15 205 views
0

有java類執行,並返回結果JSP返回任何結果

package Test; 
import static java.lang.System.out; 
import java.util.ArrayList; 
import java.util.List; 
import org.asteriskjava.manager.ManagerConnection; 
import org.asteriskjava.manager.ManagerConnectionFactory; 
import org.asteriskjava.manager.action.CommandAction; 
import org.asteriskjava.manager.response.CommandResponse; 



public class Manager 
{ 
    private ManagerConnection c; 

    public Manager() throws Exception 
    { 
     ManagerConnectionFactory factory = new ManagerConnectionFactory(
       "tttt", "admin", "ttt"); 
     c = factory.createManagerConnection(); 
    } 

    public void run() throws Exception 
    { 
     c.login(); 

     CommandAction action; 
     CommandResponse response; 
     List<String> list = new ArrayList<String>(); 


     action = new CommandAction(); 
     action.setCommand(" sip show peers"); 
     response = (CommandResponse) c.sendAction(action); 

     list = response.getResult(); 
     for (String s : list) { 
      if (s.contains("VOIP")) { 
      out.print(s); 

      } 
     } 

     c.logoff(); 
    } 

    public static void main(String[] args) throws Exception 
    { 
     new Manager().run(); 


    } 
} 

結果:

TEST   172.28.1.1         a    5060  OK (4 ms)         tes   172.28.1.1         a    5060  OK (2 ms)          at Test.Manager.main(Manager.java:50) 
Java Result: 1 

的jsp:

<%@page import="Test.Manager" %> 
<% 
    Manager o = new Manager(); 
    o.run(); 
    out.print(o); 
%> 

當你調用JSP中瀏覽器,我看到代碼被執行(很明顯,他衝到服務器並接收到數據),但在瀏覽器頁面上doe不顯示執行結果。告訴我要挖哪種方式。

+0

如果您在瀏覽器中查看「查看頁面源代碼」,您是否看到該編號? – BretC

+0

是的,我看到了源代碼。但是一個類的輸出不存在 – santer

+0

你確定這個編譯? 「out」來自「run」方法的地方在哪裏?不要,你需要從頁面傳遞給run方法嗎?另外,在JSP中,您正在執行「out.print(o)」。因爲你的經理類沒有toString方法,所以我期望這會輸出一些可怕的東西,比如「Manager @ 1befc」或者其他什麼 – BretC

回答

0

您需要一些方法來捕獲您正在構建的字符串,然後將其輸出到生成的HTML頁面。我已更新您的run方法以返回您輸出的字符串的副本。

public class Manager 
{ 
    private ManagerConnection c; 

    public Manager() throws Exception 
    { 
     ManagerConnectionFactory factory = new ManagerConnectionFactory(
       "tttt", "admin", "ttt"); 
     c = factory.createManagerConnection(); 
    } 

    public String run() throws Exception 
    { 
     c.login(); 

     CommandAction action; 
     CommandResponse response; 
     List<String> list = new ArrayList<String>(); 
     StringBuilder sb = new StringBuilder(); 

     action = new CommandAction(); 
     action.setCommand(" sip show peers"); 
     response = (CommandResponse) c.sendAction(action); 

     list = response.getResult(); 
     for (String s : list) { 
      if (s.contains("VOIP")) { 
      out.print(s); 
      sb.append(s); 
      } 
     } 

     c.logoff(); 
     return sb.toString(); 
    } 

    public static void main(String[] args) throws Exception 
    { 
     new Manager().run(); 
    } 
} 

現在更新JSP以顯示HTML頁面上的字符串 - 不是在控制檯中。

<%@page import="Test.Manager" %> 
<% 
    Manager o = new Manager(); 
    String output = o.run(); 
%> 
<%= output %> 

我也不建議使用scriplets,但這應該讓你的代碼工作。