我正在获得输出线。
rtt min/avg/max/mdev = 10.876/13.344/17.155/2.736 ms
我想将结果解析/解析为
min = 10.876
avg = 13.344
max = 17.155
deviation = 2.736
我想在jsp文件中显示它们。我该怎么办?
更新; 对不起大家。这就是我做的。 Getting the ping results using java Result redirection not working with jsp
我正在检查以下方法进行解析。 http://pages.cs.wisc.edu/~hasti/cs302/examples/Parsing/parseString.html 我是新手。道歉。
各位,
我修改了我的程序如下。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String ip = request.getParameter("ip");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// out.println("The ip address is:"+ip+"\n");
String result = pingTest(ip);
// out.println(result);
String[] split_result = result.split(" ");
String values = split_result[3];
split_result = values.split("/");
double min = Double.parseDouble(split_result[0]);
double avg = Double.parseDouble(split_result[1]);
double max = Double.parseDouble(split_result[2]);
double deviation = Double.parseDouble(split_result[3]);
request.setAttribute("result", result);
request.getRequestDispatcher("Output.jsp").forward(request, response);
}
但我现在如何转发? 我必须添加更多request.setattribute? 喜欢
request.setAttribute("min", min);
request.setAttribute("avg", avg);
request.setAttribute("max", max);
request.setAttribute("deviation", deviation);
答案 0 :(得分:1)
我假设输入是Java。要简单地提取值,您可以
String str = "rtt min/avg/max/mdev = 10.876/13.344/17.155/2.736 ms";
String[] strings = str.split(" "); // split string on spaces, 5 new strings
str = strings[3]; // select the 4th of these strings
strings = str.split("/"); // split again, this time on "/"
double min = Double.parseDouble(strings[0]);
double avg = Double.parseDouble(strings[1]);
double max = Double.parseDouble(strings[2]);
double deviation = Double.parseDouble(strings[3]);
然后仍然将它们解析为jsp并将它们放在一个文件中。我希望这可以让你开始解决这个问题。 (PS:我没有把代码放在方法中,因为我不知道你将如何使用它。)