views:

281

answers:

2

Hi All,

We are trying to develop a Topology Map of available devices in a network.

I have the details of the system, like IP, Device Name.

All I need to do is to populate them on the graph and show it graphically, the problem is that we need to do it in JSP.

Is there another way, like show using Flex, where I would pass IP/Device Name and just create the environment. BTW its not fixed on how many systems, so its gotta be dynamic.

Can someone help?

A: 
Mr. Graves
Mr. Graves, I have all the data I need, but displaying in a tree structure may not be an option.
Panther24
+2  A: 

First of all, this isn't necessarily a JSP problem. Obtaining list of network devices isn't to be done in a JSP file, but in a real Java class. JSP is just a view technology.

Back to the actual problem, to start you can use the java.net.NetworkInterface API for this.

First create a Servlet which obtains a List of NetworkInterfaces in the doGet() method, puts it in the request scope and forwards the request to a JSP. As NetworkInterface class already conforms the Javabean spec with several useful getter methods, we don't need to wrap it in another Javabean class and can just reuse it.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        List<NetworkInterface> networkInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        request.setAttribute("networkInterfaces", networkInterfaces);
    } catch (SocketException e) {
        throw new ServletException("Cannot obtain networkinterfaces.", e);
    }
    request.getRequestDispatcher("/WEB-INF/networkinterfaces.jsp").forward(request, response);
}

Map this servlet in web.xml on an url-pattern of for example /networkinterfaces. This servlet would be accessible by http://example.com/context/networkinterfaces.

Now create a JSP file networkinterfaces.jsp which you place in WEB-INF to prevent from direct access by http://example.com/context/networkinterfaces.jsp (so that users are forced to use the servlet). Use the JSTL (just put JAR in /WEB-INF/lib if not done yet) c:forEach tag to iterate over the List and access the getters by EL.

<c:forEach items="${networkInterfaces}" var="networkInterface">
    Name: ${networkInterface.name}<br>
    Display name: ${networkInterface.displayName}<br>
    MTU: ${networkInterface.MTU}<br>
    <c:forEach items="${networkInterface.interfaceAddresses}" var="interfaceAddress" varStatus="loop">
        IP address #${loop.index + 1}: ${interfaceAddress.address}<br>
    </c:forEach>
    <hr>
</c:forEach>

That should be it.

Edit to have it "visually presented", either use Java 2D API to generate an image or use HTML/CSS to position the elements based on the information gathered in servlet.

BalusC
The code that you have posted would just help me list the network elements, but I need to show them in a Visual format, that the problem all abt :)
Panther24
Either use Java 2D API to creat a png/gif or play smart with the information and a shot of right HTML elements and CSS styles.
BalusC