Em um contêiner Servlet, a execução do programa MpuServlet1 ...
Comentários
Veja os comentários dos nossos alunos
getRequestDispatcher
public RequestDispatcher getRequestDispatcher(java.lang.String path)
- The default behavior of this method is to return getRequestDispatcher(String path) on the wrapped request object.
-
- Specified by:
-
getRequestDispatcherin interfaceServletRequest
-
- Parameters:
-
path- aStringspecifying the pathname to the resource. If it is relative, it must be relative against the current servlet. - Returns:
- a
RequestDispatcherobject that acts as a wrapper for the resource at the specified path, ornullif the servlet container cannot return aRequestDispatcherhttp://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/ServletRequestWrapper.html
fonte:
De acordo com a especificação da Sun.
"The forward method of the RequestDispatcher interface may be called by the
calling servlet only when no output has been committed to the client. If output data
exists in the response buffer that has not been committed, the content must be
cleared before the target servlet’s service method is called. If the response has been
committed, an IllegalStateException must be thrown."
Para chamar o metodo forward da classe RequestDispatcher, que serve para chamar outro Servlet, o objeto response não pode ter nehum dado.
Se existe dados no objeto response ele deve ser limpo, response.clear(), antes de chamar outro Servlet.
Caso exista dados no objeto response e ele seja "comitado" (response.flushBuffer();) vai ocorrer uma exceção, IllegalStateException. O que acontece no codigo da questão.
Não sabemos qual é o pattern q ela atende e nem o seu servlet-name
A questão está errada, pois ou você trata a requisição e envia uma resposta ao cliente, ou encaminha para outro servlet tratá-la através do método forward() do RequestDispatcher.
O código abaixo vai gerar uma IllegalStateException, pois o servlet gravou uma resposta e depois despachou a requisição para outro servlet.
Clique para visualizar este comentário
Visualize os comentários desta questão clicando no botão abaixo