1
I have in my application the following error:
getOutputStream() has already been called for this Response.
Ok, the chunk of code that is causing this error is as follows:
boolean hasLogoLogin = new BrandResourceDao().selectByTag(BrandResourceCnt.TAG_BRANDLOGOLOGIN) != null;
boolean hasLogoDefault = new BrandResourceDao().selectByTag(BrandResourceCnt.TAG_BRANDLOGODEFAULT) != null;
boolean hasLogoHight = new BrandResourceDao()
.selectByTag(BrandResourceCnt.TAG_BRANDLOGOHIGHLIGHTEDE) != null;
boolean hasBackground = new BrandResourceDao().selectByTag(BrandResourceCnt.TAG_BRANDBACKGROUND) != null;
Which is nothing more than a select to check whether the images saved at a certain time in the application really are there. The code just below is repeated for each return of the above selects(Each for its value).
<img id="imgLogoLogin"
<%=hasLogoLogin
? "src='loadImage.jsp?" + Params.TAG + "=" + BrandResourceCnt.TAG_BRANDLOGOLOGIN + "'" : ""%>
style="max-width: 325px; max-height: 200px; width: auto; height: auto;" />
And here the loadImage.jsp that searches the database for the image, converts and displays in the application.
try {
String tag = request.getParameter(Params.TAG);
BrandResourceDao dao = new BrandResourceDao();
BrandResource brand = dao.selectByTag(tag);
if (brand != null) {
byte[] bytes = Base64.decode(brand.getValue());
response.setContentType("image/gif");
OutputStream oImage = response.getOutputStream(); <-- É culpa desse cara aí
oImage.write(bytes);
oImage.flush();
oImage.close();
}
} catch (Exception e) {
e.printStackTrace();
}
Please no swearing by the style of the tag (img) I will put it in css in the future.
Well my doubt is: How to solve this? I need to change the structure is obvious. However I am beginner and do not know how to do it. Does anyone have any ideas to expedite my life with this?
Would you have an example to indicate? Everyone I met is calling getOutputStream().
– Cesar Roberto Martins
@Cesarrobertomartins I updated the answer with more information
– utluiz
Um. . I’m a beginner, as I said, I’ve seen it in several examples of code written the same way. I think it looks good and organized. But I can’t use it. I’m at work, and I’ve been instructed not to use this project model. There is no more "simple" solution in the case of the model I already have here?
– Cesar Roberto Martins
@Cesarrobertomartins I wish I could go to your company and talk to whoever said that. The official answer is: with JSP I can’t. No one in their right mind does that. Servlets or Controllers (if using any framework) are the right way to do it. The rest is gambiarra. With and I said in the answer, you can try to convert bytes to a String and print this on output, but there is no guarantee that the image will not get corrupted.
– utluiz
Anyway, I will communicate this to the project leader and tell him. Let’s see what he has in mind. In the meantime, I’ll solve what I can. Thank you very much for the answer, it was of great help.
– Cesar Roberto Martins