display image or pdf retrieved from database in jsp

I would like to display a document retrieved from the database. The document can be an image, a pdf, a .docx etc. Currently, my code is only handling images. How can I have the jsp handle all types of documents – not just images?

This is the jsp:

<img id="attachment" src="${pageContext.request.contextPath}/images/${ticket.getAttachmentName()}" alt="${ticket.getAttachmentName()}">

and here is my servlet code (from https://stackoverflow.com/a/2341322/9022279) :

@WebServlet("/images/*")
public class ImageServlet extends HttpServlet {

private static final String SQL_FIND = "SELECT attachment FROM tickets WHERE attachment_name = ?";


protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String imageName = request.getPathInfo().substring(1); 

    try (Connection connection = DBConnection.createConnection(); PreparedStatement statement = connection.prepareStatement(SQL_FIND)) {
        statement.setString(1, imageName);

        try (ResultSet resultSet = statement.executeQuery()) {
            if (resultSet.next()) {
                byte[] content = resultSet.getBytes("attachment");
                response.setContentType(getServletContext().getMimeType(imageName));
                response.setContentLength(content.length);
                response.getOutputStream().write(content);
            } else {
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
            }
        }
    } catch (SQLException e) {
        throw new ServletException("Something failed at SQL/DB level.", e);
    }
}