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 NetworkInterface
s 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.