Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>Back to the actual problem, to start you can use the <a href="http://java.sun.com/javase/6/docs/api/java/net/NetworkInterface.html" rel="nofollow noreferrer">java.net.NetworkInterface</a> API for this.</p> <p>First create a Servlet which obtains a <code>List</code> of <code>NetworkInterface</code>s in the <code>doGet()</code> method, puts it in the request scope and forwards the request to a JSP. As <code>NetworkInterface</code> 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.</p> <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { List&lt;NetworkInterface&gt; 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); } </code></pre> <p>Map this servlet in <code>web.xml</code> on an <code>url-pattern</code> of for example <code>/networkinterfaces</code>. This servlet would be accessible by <code>http://example.com/context/networkinterfaces</code>.</p> <p>Now create a JSP file <code>networkinterfaces.jsp</code> which you place in <code>WEB-INF</code> to prevent from direct access by <code>http://example.com/context/networkinterfaces.jsp</code> (so that users are forced to use the servlet). Use the <a href="http://download.java.net/maven/1/jstl/jars/jstl-1.2.jar" rel="nofollow noreferrer">JSTL</a> (just put JAR in <code>/WEB-INF/lib</code> if not done yet) <a href="http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/c/forEach.html" rel="nofollow noreferrer">c:forEach</a> tag to iterate over the <code>List</code> and access the getters by <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPIntro7.html" rel="nofollow noreferrer">EL</a>.</p> <pre><code>&lt;c:forEach items="${networkInterfaces}" var="networkInterface"&gt; Name: ${networkInterface.name}&lt;br&gt; Display name: ${networkInterface.displayName}&lt;br&gt; MTU: ${networkInterface.MTU}&lt;br&gt; &lt;c:forEach items="${networkInterface.interfaceAddresses}" var="interfaceAddress" varStatus="loop"&gt; IP address #${loop.index + 1}: ${interfaceAddress.address}&lt;br&gt; &lt;/c:forEach&gt; &lt;hr&gt; &lt;/c:forEach&gt; </code></pre> <p>That should be it.</p> <p><strong>Edit</strong> 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload