我真的很难理解为什么我被告知 getParameter 返回一个对象,我需要在下面的代码中将其转换为字符串?在 String timeTaken 时,我收到错误“类型不匹配:无法从 void 转换为 String”。我对导致错误的原因感到困惑,持续时间的长数据类型还是用户的字符串数据类型?
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
long t0 = System.currentTimeMillis();
// pass the request along the filter chain
chain.doFilter(request, response);
long t1 = System.currentTimeMillis();
long duration = t1 - t0;
String user = request.getParameter("userName");
String timeTaken = System.out.println("<HTML><BODY><P>Request from " + user + " at 10.10.1.123 took " + duration + "ms </P></BODY></HTML>");
context.log(timeTaken);
}
提前致谢。
最佳答案
System.out.println
不返回任何内容,它只是将值打印到控制台。尝试使用不存在的返回值并将其保存到 timeTaken 会给出错误消息。
您可能只想将字符串分配给 timeTaken;
String timeTaken = "<HTML><BODY><P>Request from " + user +
" at 10.10.1.123 took " + duration + "ms </P></BODY></HTML>";
如果您仍然想输出字符串,可能在下一行;
System.out.println(timeTaken);
关于java - java中的转换,request.getParameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16372205/