Bonsoir, j'ai une classe qui fait des test ping et afficher temps de réponse et taux de perte comme suit
une servlet pour appeler cette classe ping
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 public class pingtrouble { String IP; String Paquet; String mtuu; public pingtrouble(String ip,String p,String mtu) { this.IP=ip; this.Paquet=p; this.mtuu=mtu; } public double time() throws IllegalStateException, IOException { Shell sh =new Shell(); String time; time = sh.command("ping -c"+p+" -s"+"mtu "+ip+" | grep rtt | cut -d'/' -f 5").consumeAsString(); double time1 = Double.parseDouble(time); return time1; } public String perte() throws IllegalStateException, IOException { Shell sh =new Shell(); String perte; perte = sh.command("ping -c"+p+" -s"+"mtu "+ip+" |grep loss | cut -d',' -f 3 |awk '{print $1}'").consumeAsString(); return perte; } }
et une JSP qui verifie s'il y'a un resultat, elle l'affiche
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 import marouene.pingtrouble; public class Pingtr extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); //PrintWriter out = response.getWriter(); String mtu = request.getParameter("mtu"); String paquets = request.getParameter("paquet"); pingtrouble ping = new pingtrouble("@server", mtu, paquets); String Perte = ping.perte(); Double time = ping.time(); // Stockage de l'objet ping request.setAttribute("pingtrouble",ping); // Affichage du formulaire request.getRequestDispatcher("Pingtrouble.jsp").forward(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); }
Mais ceci me genere l'erreur suivante : Ping n'est pas initialisé...alors qu'lle n'est pas censé l'etre !!! non ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 <%page import="marouene.pingtrouble"%> <form id="form1" method="post" action='Pingtr'> <p></p> <p> <label> <input type="radio" name="mtu" value="1500" /> 1500</label> <br /> <label> <input type="radio" name="mtu" value="2000" /> 2000</label> <br /> <label> <input type="radio" name="mtu" value="4000" /> 4000</label> </p> <p>Veuillez definir le nombre de paquets à envoyer: <label for="textfield3"></label> <input type="text" name="paquet" /> </p> <p> <label for="textfield3"></label> Lancer le test : <input type="submit" name="button" id="button" value="OK" /> </p> </form> <% // Récupération de l'objet ping. pingtrouble ping; if(request.getAttribute("pingtrouble")!=null) { ping = (pingtrouble) request.getAttribute("pingtrouble"); %> <% <h2>Résultat :</h2> Temps : <%= ping.time()%><br/> Pertes : <%= ping.perte()%> } %>
Partager