在jsf中包含jsp,输入长度= 1时会出现异常 [英] Including jsp in jsf gives exception as Input length = 1

查看:187
本文介绍了在jsf中包含jsp,输入长度= 1时会出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jsp中有以下代码

I have below code in jsp

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <%@ page import="java.sql.*"%>
        <%@ page import="java.io.*"%>
        <%
                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:8889/mySacDB", "root", "root");
                    stmt = con.createStatement();
                    rs = stmt.executeQuery("select photo from personalInfo where userid = 5");
                    if (rs.next()) {
                        image = rs.getBlob(1);
                        imgData = image.getBytes(1, (int) image.length());
                    }
                    // display the image
                    response.setContentType("image/jpeg");
                    OutputStream o = response.getOutputStream();
                    o.write(imgData);
                    o.flush();
                    o.close();
                } catch (Exception e) {
                    out.println("Unable To Display image<br /><br />");
                    out.println("Image Display Error=" + e.getMessage());
                    return;
                } finally {
                    try {
                        rs.close();
                        stmt.close();
                        con.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
        %>
    </body>
</html>

使用此功能,图像可以正常显示。

By use of this, image get displayed properly.

但是,当我在JSF 2.0页面中包含相同的页面时,我会收到异常。

However when I include the same page in JSF 2.0 page, I get exception.

<o:resourceInclude path="myDisplayImage.jsp"/>

注意: o = xmlns:o =http:// omnifaces。 org / ui

异常我得到的是输入长度= 1 。 StackTrace是

Exception I get is Input length = 1. Stacktrace is

 java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(CoderResult.java:260)
at java.nio.charset.CharsetDecoder.decode(CharsetDecoder.java:781)
at com.sun.faces.application.ByteArrayWebOutputStream.writeTo(ByteArrayWebOutputStream.java:112)
at com.sun.faces.application.ViewHandlerResponseWrapper.flushToWriter(ViewHandlerResponseWrapper.java:162)
at com.sun.faces.application.view.JspViewHandlingStrategy.renderView(JspViewHandlingStrategy.java:264)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:809)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:671)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:626)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:556)
at org.omnifaces.component.output.ResourceInclude.encodeBegin(ResourceInclude.java:79)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1755)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:349)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at com.sac.filter.MyFilter.doFilter(MyFilter.java:48)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:680)



< hr>

编辑1



我也尝试使用新的代码使用 getBinaryStream 还是一样的错误。代码是


Edit 1

I also tried with new code using getBinaryStream still same error. Code is

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <%@ page import="java.sql.*"%>
        <%@ page import="java.io.*"%>
        <%
            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:8889/mySacDB", "root", "root");
                stmt = con.createStatement();
                rs = stmt.executeQuery("select photo from personalInfo where userid = 5");
                if (rs.next()) {
                    String imgLen = rs.getString(1);
                    System.out.println(imgLen.length());
                    int len = imgLen.length();
                    byte[] rb = new byte[len];
                    InputStream readImg = rs.getBinaryStream(1);
                    int index = readImg.read(rb, 0, len);
                    System.out.println("index----------------" + index);
                    response.reset();
                    response.setHeader("Content-Length", String.valueOf(len));
                    String tempString = "image/jpeg";
                    String tempExtenson = tempString.substring(tempString.lastIndexOf("/") + 1, tempString.length());
                    response.setHeader("Content-disposition", "inline;filename=/file." + tempExtenson);
                    response.setContentType(tempString);
                    response.getOutputStream().write(rb, 0, len);
                    response.getOutputStream().flush();
                }
                stmt.close();
                // display the image
            } catch (Exception e) {
                out.println("Unable To Display image<br /><br />");
                out.println("Image Display Error=" + e.getMessage());
                return;
            } finally {
                try {
                    rs.close();
                    stmt.close();
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        %>
    </body>
</html>


推荐答案

您不能在XHTML中包含一个JSP输出文本以外的东西。

You can't include in your XHTML a JSP that outputs something other than text.

问题是您的JSP正在将图像的输出混合到整个请求中,将内容类型设置为 image / jpeg(或出现错误时输出text / html)。您将无法将其包含在另一个请求中。

The problem is that your JSP is mixing output of an image to the entire request, setting the content type as "image/jpeg" (or outputs "text/html" when some error occurs). You won't be able to include it in another request as it is.

在您的情况下,您可能希望仅删除所有HTML标签,并使JSP仅输出图片。然后,您将能够使用纯HTML来正确显示图像,如下所示:

In your case, you probably want to just remove all HTML tags and make the JSP just output the image. Then you would be able to show the image properly using plain HTML, like this:

<img src="myDisplayImage.jsp" />

这篇关于在jsf中包含jsp,输入长度= 1时会出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆