0

There java class that executes and returns the result

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();


    }
}

The result of :

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);
%>

When you call the JSP in the browser , I see that the code is executed ( It is evident that he Rushed to the server and received data ) but on the page in the browser does not display the result of execution. Tell me which way to dig.

santer
  • 103
  • 4
  • Do you see the number if you go to "view page source" in the browser? – BretC Apr 15 '15 at 15:12
  • Yes, I see the source code . But the output of a class is not there – santer Apr 15 '15 at 15:14
  • Are you sure this even compiles? Where does "out" come from in your "run" method? Don;t you need to pass in "out" to the run method from the page? Also, in the JSP, you are doing "out.print(o)". As your Manager class does not have a toString method, I'd expect this to output something horrible like "Manager@1befc" or something – BretC Apr 15 '15 at 15:17
  • All compiled and displayed as you said "Test.Manager@6d787352" – santer Apr 15 '15 at 15:19
  • agree with @Bret, you need a `toString()` if you want to output an object. – Sid Zhang Apr 15 '15 at 15:20
  • You are using a static import - `import static java.lang.System.out;` and then using `out.print(s);` which sends the output to the IO console. You need an instance of `PrintWriter` anyway something along the line - `PrintWriter out = HttpServletResponse#getWriter();` – Tiny Apr 15 '15 at 15:25
  • Your JSP has no code to display anything. Also, you should [avoid Java code in JSPs](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files). – GriffeyDog Apr 15 '15 at 15:31
  • Use <%= o %> instead – thoitbk Apr 15 '15 at 15:58
  • @thoitbk Your option displays on the page "Test.Manager@535b6e7d " – santer Apr 15 '15 at 16:00
  • Try implementing toString() of Manager class – thoitbk Apr 15 '15 at 16:01
  • Tried to get the same result – santer Apr 15 '15 at 16:07

1 Answers1

0

You need some way to capture the string you are building, then output it to the generated HTML page. I have updated your run method to return a copy of the string you are outputting.

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();
    }
}

Now update the JSP to show the string on the HTML page - not in your console.

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

I also do not recommend using scriplets, but this should get your code working.

Michael Freake
  • 1,197
  • 2
  • 14
  • 35