• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Reading HTML files from a database

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!
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 java.io.*;
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 = "org.gjt.mm.mysql.Driver";
private String dbUrl = "jdbc:mysql://xxx";
public void init() {
try {
Class.forName(dbDriver);
conn = DriverManager.getConnection(dbUrl, user, passWd);
}catch(Exception ex) {
System.out.println("Database connection could not be established!");
System.out.println("reason:"+ex.getMessage());
//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
{
//dbConnect();
System.out.println("Processing request");
// get id from request
String tmp = req.getParameter("id");
int id = Integer.parseInt(tmp);
System.out.println("ID:"+id);
// 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(rs.next()) {
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
res.setBufferSize(1024);
try {
// get the binary output stream
BufferedOutputStream out = new
BufferedOutputStream(res.getOutputStream());
// write the data
out.write(data, 0, data.length);
}
catch(IOException ex) {
System.out.println("Data send failed:"+ex.getMessage());
}
}
}
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the way I normaly do it:

/Rene
[ July 17, 2002: Message edited by: Rene Larsen ]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Bill
 
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic