0

我只是想从数据库中检索图像并显示它。我写了以下代码:

    RetreiveImage.java

    import myconnection.ConnetionDefault;
    import java.sql.*;
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class RetreiveImage extends HttpServlet{
    public void doGet(HttpServletRequest request, HttpServletResponse response)         throws         ServletException, IOException{
    PrintWriter out = response.getWriter();
    Connection con=null;
    String id=request.getParameter("id");
    try{
    con=ConnetionDefault.getConnection();
    ResultSet results = null;
    PreparedStatement ps = null;
    ps = con.prepareStatement("select picture from pic_229084519 where pic_id=?");
    ps.setString(1,id);
    results = ps.executeQuery();
    String imgLen="";
    if(results.next()){
    imgLen = results.getString(1);
    System.out.println(imgLen.length());


    int len = imgLen.length();
    byte [] rb = new byte[len];
    InputStream readImg = results.getBinaryStream(1);
    int index=readImg.read(rb, 0, len);
    System.out.println("index"+index);
    ConnetionDefault.close(results,ps,con);
    response.reset();
    response.setContentType("image/jpg");
    response.getOutputStream().write(rb,0,len);
    response.getOutputStream().flush();

    RequestDispatcher rd=request.getRequestDispatcher("gg.html");
    rd.forward(request,response);
    }
    } catch (Exception e){

    e.printStackTrace();
    }
    }
    }

    retImagel.html

    <html>
    <head>
    <title>Login</title>

    </head>  
    <body bgcolor="Gold">
    <center>

    <form action="ret.pic">
    <table>
    <tr><th>Namme</th><td><input type="text" name="id" size="3"/></td><td></td>

    <td><input type="submit" value="Get"></td><td><input type="reset"     value="Cancel"/></td></tr>
    </table>
    </form>

    </center>
    </body>
    </html>

    gg.html

    <html>
    <head>
     <title>Login</title>

    </head>  
    <body bgcolor="Gold">

    <img src="/ret.pic?id=${id}"/>
    </center>
    </body>
    </html>

我的问题是它在默认响应页面中显示图像。但我想要它在我出错的 gg.html 中。请帮助我。

4

1 回答 1

0

在您的 web.xml 中设置过滤器以调用您的 servlet。

在您的 gg.html 页面上插入标签 < img "url to image that filter can intersept" >

于 2012-05-04T09:54:36.113 回答