import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class getImage extends HttpServlet{ Connection con = null; PreparedStatement pstmt1 = null; PreparedStatement pstmt2 = null; //Kopplar till databasen "guestbookimage" public void init() throws ServletException{ try{ String url = "jdbc:mysql://localhost/guestbookimage"; Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection(url, "josef", "cotaidis"); }catch(ClassNotFoundException cnfe){ log("Couldn't load database driver: " + cnfe.getMessage()); }catch(SQLException sqle){ log("SQLException caught: " + sqle.getMessage()); } } //Kopplar från databasen public void destroy(){ try{ con.close(); }catch(SQLException sqle){ log("SQLException caught: " + sqle.getMessage()); } } /*Tar fram en specifik rad ur tabellen image sätter content type till den aktuella bildens content type och skriver ut den */ public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{ Statement stmt = null; try{ stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM image WHERE id = " + req.getParameter("id") + ";"); if (rs.first()){ res.setContentType(rs.getString("mime")); OutputStream out = res.getOutputStream(); out.write(rs.getBytes("pic")); out.flush(); out.close(); } }catch(SQLException sqle){ log(sqle.getMessage()); } } }