jsp返回无结果

时间:2015-04-15 15:10:29

标签: java jsp

执行并返回结果的java类

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时,我看到代码被执行(显然他已经赶到服务器并接收数据)但是在浏览器的页面上没有显示执行结果。告诉我挖掘哪种方式。

1 个答案:

答案 0 :(得分: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,但这应该可以让您的代码正常工作。