Issue
I would like to include the jQuery library for every page by modifying the response. Here it is:
This is in the Filter:
PrintWriter out = response.getWriter();
out.println("<head><script src=\"http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.1.min.js\"></script></head>");
chain.doFilter(request, response);
It works just fine but when I want to write something else to the response with a servlet then get the following error:
java.lang.IllegalStateException: getWriter() has already been called for this response
How can I solve this?
Solution
Put the html code to the file and use servlet dispatcher method include that file. See more about it in javax.servlet.RequestDispatcher#include(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
included.html
:
<head><script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.1.min.js"></script></head>
filter code:
request.getRequestDispatcher("included.html").include(request, respose);
chain.doFilter(request, response);
Answered By - Roman C