aspose file tools
The moose likes Servlets and the fly likes Reading HTML files from a database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Reading HTML files from a database" Watch "Reading HTML files from a database" New topic

Reading HTML files from a database

Samuli Ikola

Joined: Jul 17, 2002
Posts: 1
I have a problem reading/presenting HTML files from a database. I have tried it in many different ways but I can't get it to work. Here is my latest try:
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ImageServlet extends HttpServlet
private Connection conn = null;
private String database = "xxx";
private String path = "xxx";
private String user = "xxx";
private String passWd = "xxx";
private String dbDriver = "";
private String dbUrl = "jdbc:mysql://xxx";
public void init() {
try {
conn = DriverManager.getConnection(dbUrl, user, passWd);
}catch(Exception ex) {
System.out.println("Database connection could not be established!");
//throw new ServletException("Database connection could not be established!");

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException
processRequest(req, res);
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException
processRequest(req, res);
private void processRequest(HttpServletRequest req, HttpServletResponse res)
throws ServletException
System.out.println("Processing request");
// get id from request
String tmp = req.getParameter("id");
int id = Integer.parseInt(tmp);
// get the blob from database
// FIXME: name of the table ???
String SQL = "select materiaalitiedosto, mtiedostonimi from materiaali "
+ "where id = ?";
try {
PreparedStatement ps = conn.prepareStatement(SQL);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
Blob blob = null;
String filename = "";
// get the data from query
while( {
blob = rs.getBlob(1);
filename = rs.getString(2);
// get the actual data as bytes from blob
byte [] data = blob.getBytes(1l,(int) blob.length());
// send the data to client
sendData(data, res);
}catch(SQLException ex) {
System.out.println("Database select operation failed:"+ex.getMessage());
throw new ServletException("Database select operation failed");
private void sendData(byte[] data, HttpServletResponse res) {
// set responses buffer size to 1kB
// so clients can handle the files more easily
try {
// get the binary output stream
BufferedOutputStream out = new
// write the data
out.write(data, 0, data.length);
catch(IOException ex) {
System.out.println("Data send failed:"+ex.getMessage());

Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

This is the way I normaly do it:

[ July 17, 2002: Message edited by: Rene Larsen ]

Regards, Rene Larsen
Dropbox Invite
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12981
1. You need to set the response content type before sending any data.
2. Getting a connection in the init() method is a very bad idea. If the database closes that connection you have no way to re-establish it, and there are many other problems. There are various connection pool packages that handle this problem.
I agree. Here's the link:
subject: Reading HTML files from a database
It's not a secret anymore!