我已经下载了 log4j jar。但不知道如何使用它将输出保存到文本文件。我的代码是:
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
public class ShowHeaders extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
String headerName;
String headerValue;
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
String method= request.getMethod();
out.println("Request Method: " +method);
out.println("-----------------------");
String uri= request.getRequestURI();
out.println("URI: " +uri);
out.println("-----------------------");
out.println("Request Headers");
Enumeration<?> Enumeration = (java.util.Enumeration<?>) request.getHeaderNames();
while (Enumeration.hasMoreElements()) {
String headerName = (String) Enumeration.nextElement();
String headerValue = request.getHeader(headerName);
out.print(""+headerName + ": ");
out.println(headerValue + "");
}
}
请告诉我在哪里描述文件位置和使用什么而不是 out.println(); 显示它的文本文件