0

我真的很难理解为什么我被告知 getParameter 返回一个我需要将其转换为以下代码中的字符串的对象?在 String timeTaken 我收到错误 Type mismatch: cannot convert from void to 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);
}

提前致谢。

4

1 回答 1

1

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);
于 2013-05-04T08:26:24.613 回答