My program is working, but JSP page loading very slow that is my issue. I am passing empid and image name as a parameter from a jsp page to a Servlet which I am able to do perfectly and it is receiving in Servlet and calling data access class associated method, after that I am retrieving it and passing all this to JSP in JSP I am using Scriptlet inside that image is loading into JSP page but it is loading very slow at least 10-15 second and console showing below error :
EVERE: Servlet.service() for servlet [jsp] in context with path [/MVCDemoProject] threw exception [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
EmployeeBean class method
public Employee RetrieveImg(Employee emp1){
Connection con = null;
Statement stmt = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
byte[] sImageBytes;
int z = emp1.getEmpId();
String q = emp1.getIname();
try {
int count =0;
con = ConnectionManager.getConnection();
stmt = con.createStatement();
String Query ="SELECT photo FROM upload_documets WHERE empId ='"+z+"' and Name ='"+q+"'";
rs = stmt.executeQuery(Query);
if(rs.next())
{ sImageBytes = rs.getBytes(1);
emp1 = new Employee();
emp1.setFileBytes(sImageBytes);
emp1.setValid1(true);
}
}catch (SQLException ex) {
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
} catch (SQLException ex) {
Logger.getLogger(EmployeeBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
return emp1;
}
Image retrieval servlet which is calling data access class(employee bean) method on the basis of input which we have received from jsp page (image name and emp id name)
@WebServlet(name = "Retrieve_Image", urlPatterns = {"/Retrieve_Image"})
public class Retrieve_Image extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(true);
Employee emp1 = (Employee)session.getAttribute("emp1");
String iname = request.getParameter("iname");
emp1.setEmpId(emp1.getEmpId());
emp1.setIname(iname);
EmployeeBean eb = new EmployeeBean();
eb.RetrieveImg(emp1);
if(emp1.isValid1())
{
response.sendRedirect("Image.jsp");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
jsp page after calling method where we want to see image response:
<body>
<%
Employee emp1 = (Employee)session.getAttribute("emp1");
session.setAttribute("emp1",emp1);
byte[] sImageBytes;
try{
sImageBytes = emp1.getFileBytes();
response.setContentType("image/jpeg");
response.setContentLength(sImageBytes.length);
response.setHeader("Content-Disposition", "inline; filename=\"");
BufferedInputStream input = new BufferedInputStream
(new ByteArrayInputStream(sImageBytes));
BufferedOutputStream output =
new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[8192];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
}
catch(Exception ex){
System.out.println("error :"+ex);
}
%>
</body>
</html>