在其他页面上包含servlet

时间:2012-12-09 20:10:58

标签: servlets

我正在做广告页面,提供粘贴在某人页面上的代码并获取图片或文字。

我得到的servlet只显示来自数据库的图片或文本,并希望将其包含在某个页面上。

我试试     <script src="http://mylocalhost/myservlet?someparameters=x"></script> 但它没有用。

当我做简单的HTML页面时,做     <img src="http://mylocalhost/myservlet?someparameters=x"> 它显示图像(我需要提供参数和servlet解析它应该是图像或文本)。

我的servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {
        String wielkosc = request.getParameter("wielkosc");
        String typ = request.getParameter("typ");
        ArrayList<Integer> lista = new ArrayList<Integer>();
//WYŚWIETLANIE OBRAZKÓW  -------------------------------------------------------------------------
        if (typ.equals("1")) {

            PreparedStatement ps = con.prepareStatement("SELECT Id FROM Reklamy WHERE Typ=? AND Size=?");
            if (wielkosc.equals("0")) {
                ps.setInt(1, 1);
                ps.setInt(2, 0);
            } else if (wielkosc.equals("1")) {
                ps.setInt(1, 1);
                ps.setInt(2, 1);
            }

            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                lista.add(rs.getInt("Id"));
            }
            Random r = new Random();
            PreparedStatement ps2 = con.prepareStatement("SELECT Content FROM Reklamy WHERE Id=?");
            int los = r.nextInt(lista.size());
            ps2.setInt(1, lista.get(los));

            rs = ps2.executeQuery();
            if (rs.next()) {
                Blob blob = rs.getBlob(1);
                InputStream stream = blob.getBinaryStream();
                int len = (int) blob.length();
                System.out.println(len);
                byte[] rb = new byte[len];
                int read = stream.read(rb, 0, len);
                response.setContentType("image/jpg");
                response.getOutputStream().write(rb, 0, len);
                response.getOutputStream().flush();
            }
// WYŚWIETLANIE TEKSTU -------------------------------------------------------------------------
        } else if (typ.equals("0")) {
            PreparedStatement ps = con.prepareStatement("SELECT Id FROM Reklamy WHERE Typ=? AND Size=?");
            if (wielkosc.equals("0")) {
                ps.setInt(1, 0);
                ps.setInt(2, 0);
            } else if (wielkosc.equals("1")) {
                ps.setInt(1, 0);
                ps.setInt(2, 1);
            }

            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                lista.add(rs.getInt("Id"));
            }
            System.out.println(lista.size());
            Random r = new Random();
            PreparedStatement ps2 = con.prepareStatement("SELECT Content FROM Reklamy WHERE Id=?");
            int los = r.nextInt(lista.size());
            ps2.setInt(1, lista.get(los));                
            rs = ps2.executeQuery();
            if (rs.next()) {
                Blob blob = rs.getBlob(1);
                InputStream stream = blob.getBinaryStream();
                int len = (int) blob.length();
                System.out.println(len);
                byte[] rb = new byte[len];
                int read = stream.read(rb, 0, len);
                response.setContentType("text/html;charset=UTF-8");
                response.getOutputStream().write(rb, 0, len);
                response.getOutputStream().flush();
            }
        }
    }

0 个答案:

没有答案