我的程序正在运行,但JSP页面加载速度非常慢,这是我的问题。我将empid和图像名称作为参数从jsp页面传递给Servlet,我能够完美地完成它并在Servlet中接收并调用数据访问类关联方法,之后我将其检索并将所有这些传递给JSP在JSP中我正在使用Scriptlet,该图像正在加载到JSP页面中,但它加载速度非常慢,至少10-15秒,控制台显示如下错误:
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类方法
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;
}
基于我们从jsp页面接收的输入(图像名称和emp id名称)调用数据访问类(员工bean)方法的图像检索servlet
@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页面:
<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>
答案 0 :(得分:1)
在jsp中添加以下行: -
<%@page language="java" trimDirectiveWhitespaces="true"%>
也可以进行以下更改: -
BufferedInputStream input;
BufferedOutputStream output;
........
try{
.........
............
BufferedInputStream input = new BufferedInputStream
(new ByteArrayInputStream(sImageBytes));
BufferedOutputStream output =
new BufferedOutputStream(response.getOutputStream());
int length;
length = sImageBytes.length;
output.write(sImageBytes, 0, length);
catch(Exception ex){
System.out.println("error :"+ex);
} finally{
if(output != null) {
output.flush();
output.close();
}
if(input != null) {
input.close();
}
答案 1 :(得分:0)
您需要在Servlet中更改以下功能代码:
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
BufferedInputStream input = null;
BufferedOutputStream output = null;
try
{
HttpSession session = request.getSession(true);
Employee emp1 = (Employee)session.getAttribute("emp1");
String iname = request.getParameter("iname");
System.out.println("...." +iname);
emp1.setEmpId(emp1.getEmpId());
emp1.setIname(iname);
EmployeeBean eb = new EmployeeBean();
eb.RetrieveImg(emp1);
if(emp1.isValid1())
{
response.setContentType(emp1.getContentType());
response.setContentLength(sImageBytes.length);
response.setHeader("Content-Disposition", "inline; filename=\"");
input = new BufferedInputStream (new ByteArrayInputStream(sImageBytes));
output = new BufferedOutputStream(response.getOutputStream());
response.getOutputStream().write(emp1.getFileBytes);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
if(output != null)
{
output.flush();
output.close();
}
if(input != null)
{
input.close();
}
}
}
愿这对你有所帮助。