【问题标题】:illegalException:: response getwriter() is already defined非法异常:: 响应 getwriter() 已定义
【发布时间】:2016-03-22 03:16:12
【问题描述】:

当我运行这段代码时,

illegalException:: response getwriter() 已定义。

<% 
            Blob image = null;
                Connection con = null;
                byte[ ] imgData = null ;
                Statement stmt = null;
                ResultSet rs = null;
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hicheck","root","root");
                    stmt = con.createStatement();
                    String s="sivabrahma";
                    PreparedStatement ps=con.prepareStatement("select photo from hic_relation where email=? ");  
                    ps.setString(1,"sivabrahma");
                    rs=ps.executeQuery();  
                    if (rs.next()) {
                    image = rs.getBlob(1);
                    imgData = image.getBytes(1,(int)image.length());
                    }
                     else {
                    out.println("Display Blob Example");
                    out.println("image not found for given id");
                    return;
                    }
                    // display the image
                    response.setContentType("image/gif");
                    response.getOutputStream();
                    ServletOutputStream o = response.getOutputStream();
                    o.write(imgData);
                    o.flush();
                    o.close();
                      System.out.println(o.toString());
          System.out.println(imgData.toString());
                } catch (Exception e) {
                    out.println("Unable To Display image");
                    out.println("Image Display Error=" + e.getMessage());
//                    return;
                } finally {
                try {
                    rs.close();
                    stmt.close();
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }

                } %>

当我从数据库中检索图像时发生异常。

【问题讨论】:

  • 您的问题不准确。 1) 异常名称不正确。 2)从数据库中检索图像时没有发生异常:之后发生。此外,寻求 Java 异常帮助的问题应始终包含完整的堆栈跟踪。

标签: java jsp


【解决方案1】:

您不能在 JSP 中安全地调用 response.getOutputStream() 或 getWriter()。 JSP 已经为响应创建了编写器并将使用它。

考虑一下。 JSP 引擎如何知道由 JSP 组装的输出的正确交错并将直接写入流/写入器的 JSP 代码片段(“scriptlet”)输出。

如果您想“直接”写入 JSP 中的响应流,请使用隐式声明的 JSP out 变量...它为您提供 JSP 使用的 JSPWriter。

【讨论】:

    【解决方案2】:

    您可以在 JSP 中获得 OutputStream 或 Writer ,而不是两者。因为 JSP scriptlet 已经有一个隐式 out ,您不能调用 getOutputStream()

    getOutputStream() 的文档:https://docs.oracle.com/javaee/5/api/javax/servlet/ServletResponse.html#getOutputStream()

    IllegalStateException - 如果 getWriter 方法已被调用 这个回复

    【讨论】:

    • 他是在JSP 而不是Servlet 中完成的。
    • @Satya JSP 被转换为 servlet
    • @Nazeerbasha out 是 JspWriter
    猜你喜欢
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    相关资源
    最近更新 更多