동일한 서블릿에 동일한 사용자가 동시에 실행될 수있는 ajax 메서드가 있습니다. 문제를 설명하기 위해 잘못된 단어를 사용하면 죄송합니다. 그러나 지금까지 이해하고있는 방법입니다 (스레딩에 대해 많이 알지 못합니다).스레드 간의 출력 스트림
은 어쨌든 여기가 두 번째 (동일한 ID에) 잘가 호출되는 1 시간을 작동하지만,이 방법은 아약스를 통해 호출되는 방법
private void ajaxPartidas() throws ServletException, IOException {
//Variables necesarias
DataSource pool = (DataSource) session.get().getAttribute("pool");
Recibo registro = null;
int id = -1;
try{ id = Integer.parseInt(request.get().getParameter("id"));}catch(NumberFormatException e){}
if(id > 0){
registro = new Recibo(id);
if(!registro.obtener(pool))
registro = null;
registro.setPartidas(Partida.obtenerRegistros(pool, registro.getId()));
}
response.get().setContentType("application/json");
response.get().setHeader("Content-Type", "application/json; charset=utf-8");
response.get().getWriter().print((new Gson()).toJson(registro.getPartidas()));
}
그리고 그것은로 getWriter에 NullPointer를 (반환) 선. 나는 주변을 둘러 보았고 모두가 스레드에 대한 문제를 정확히 지적한 것 같습니다. 이제 더 문맥 조금
private static ThreadLocal<HttpServletResponse> response = new ThreadLocal<>();
내가 그에게 응답을 할당 I 글로벌 바르에과 같이 선언 ThreadLocal 변수를 할당 서블릿이
doPost(request, response)
에 들어가 매번 것
doPost() 메소드에서Home.response.set(response);
어떻게하면 getWriter() threadsafe를 만들까요?
정말 id가 "registro"가 null이 아니라고 확신합니다. NullPointerException을 throw합니다. 시간. – alexhg11