The example below displays a PDF document loaded from SQLServer in a web browser like Internet Explorer or FireFox.

 import java.io.IOException;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;

public class viewPDF extends HttpServlet {
    
    public void doGet(HttpServletRequest request, 
            HttpServletResponse response)
    throws ServletException, IOException {
        OutputStream out = null;
        DB1 db = new DB1();
        Connection conn=db.dbConnect(
                "jdbc:jtds:sqlserver://localhost:1433/smpp","sa","");
        
        try {
            
            response.setContentType("application/pdf");
            out = response.getOutputStream();
            byte[] b = db.getPDFData(conn);
            out.write(b,0,b.length);
            out.close();
        } catch (Exception e) {
            throw new ServletException(
                    "Exception in Excel Sample Servlet", e);
        } finally {
            if (out != null)
                out.close();
        }
    }
    
    public void doPost(HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException {
        doGet(request, response);
    }
}

class DB1 {
    public DB1() {}
    
    public Connection dbConnect(String db_connect_string,
            String db_userid,String db_password) {
        
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            Connection conn = DriverManager.getConnection(
                    db_connect_string, db_userid,db_password);
            
            System.out.println("connected");
            return conn;
            
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    public byte[] getPDFData(Connection conn) {
        
        byte[] fileBytes = null;
        String query;
        try {
            query = 
              "select data from tableimage where filename like '%.pdf%'";
            Statement state = conn.createStatement();
            ResultSet rs = state.executeQuery(query);
            if (rs.next()) {
                fileBytes = rs.getBytes(1);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileBytes;
    }
};