HttpServletRequest 특성 컬렉션의 내용을 로깅하려고합니다. 서블릿이 처음 시작될 때이를 수행해야하며, 서블릿이 완료되기 전에 다시해야합니다. 난 crufty 및 유지 관리 서블릿을 이해하려고 시도하고있다. 가능한 한 작은 영향을 주어야하기 때문에 서블릿 필터는 옵션이 아닙니다.HttpServletRequest.getAttributeNames()를 여러 번 반복하는 방법이 있습니까?
그래서 여기에 문제가 있습니다. 서블릿이 시작되면 HttpServletRequest.getAttributeNames()가 반환하는 열거를 반복합니다. 그러나 그것을 다시 반복 할 때 getAttributeNames(). hasMoreElements()는 "false"를 반환합니다! 열거 형을 '재설정'할 방법을 찾을 수 없습니다. 더 나쁜 것은 HttpServletRequest.setAttribute()를 사용하여 콜렉션에 속성을 추가하더라도 getAttributeNames(). hasMoreElements()를 호출 할 때 여전히 "false"결과를 얻습니다.
정말 가능한가요? 속성 이름을 두 번 이상 반복 할 방법이 없습니까?
요청하면 여기에 내 코드가 있습니다. 그것은 꽤 직설적입니다. 나는 재미있는 것을하고 있다고 생각하지 않습니다.
/**
*
* Returns the contents of the Attributes collection, formatted for the InterfaceTracker loglines
*
*/
@SuppressWarnings("unchecked")
public static String getAttributes(HttpServletRequest request) {
try {
StringBuilder toLog = new StringBuilder();
Enumeration attributeNames = request.getAttributeNames();
while(attributeNames.hasMoreElements()) {
String current = (String) attributeNames.nextElement();
toLog.append(current + "=" + request.getAttribute(current));
if(attributeNames.hasMoreElements()) {
toLog.append(", ");
}
}
return "TRACKER_ATTRIBUTES={"+ toLog.toString() + "}";
}
catch (Exception ex) {
return "TRACKER_ATTRIBUTES={" + InterfaceTrackerValues.DATA_UNKNOWN_EXCEPTION_THROWN + "}";
}
}
그래서 서비스 시작/doGet/doPost 메소드에서 getAttributes 메소드를 호출 한 다음 끝에 다시 호출하십시오. rite? – YoK
Downvoting 이것은 단지 사용자 오류입니다. – EJP