Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9218261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:49:19+00:00 2026-06-18T02:49:19+00:00

I try to make servlet which take file from my DB(PostgreSQL) and send it

  • 0

I try to make servlet which take file from my DB(PostgreSQL) and send it to cliet. I says:

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    try
    {
        Connection ce = ConnectionManager.createConnection();
    } catch (ClassNotFoundException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Statement sta = null;
    sta = ce.createStatement();
    String fileName="";
    byte[] file=null;
    int bufferSize = 8192;
    String sql=("SELECT files,filename FROM filestock WHERE num =(SELECT filestock_id FROM parcels_temp WHERE num="+num+")");
    ResultSet rs=sta.executeQuery(sql);
    while(rs.next()){
        file = rs.getBytes("files");
        fileName=rs.getString("filename");
    }
}

So i just create connection and eclipse says me that i gonna surround each line in Try/catch. Whats wrong? Its bad idea usind JDBC in servlet and i have to make any deals with data base in another class?
In this case how to send file to servlet? I gonna send it as File or something else i want use this for send file to cliet from servlet.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:49:20+00:00Added an answer on June 18, 2026 at 2:49 am

    Doing everything in the servlet is not a good programming practice. Try to use a 3 tier architecture..

    request---->Servlet--->Manager---->DAO--->Database
    

    Create another DAO(DatabaseAccessObject). Its nothing but another class which does only database operations. All these code which you have written in this servlet will go to a function in that class.

    Next step is to create a Bean in which you will store the results.

    Now return the bean from The DAO to this servlet.

    Now the question comes? What are you going to do with the file and fileName?

    If you want to download the file, just push it into the output stream. Make sure that you set the contentType.

    EDIT:
    Sample file download

    OutputStream out = response.getOutputStream();
    FileInputStream in = new FileInputStream(my_file);
    byte[] buffer = new byte[4096];
    int length;
    while ((length = in.read(buffer)) > 0){
        out.write(buffer, 0, length);
    }
    in.close();
    out.flush();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to make an Android application which communicates with a non-standard ISO15693 (NFC-V)
I have a servlet which uses JAXB to unmarhsal and marshal an XML file,
I am trying to make a get request from the GWT servlet to get
Good night, I'm trying to send from a servlet written in Java data name
Can I make the welcome-file of the website to be a servlet ? If
I try to make my ListBox connected to ObservaleCollection be more efficient so for
I try to make an ActiveX by C# with COM-visible. It is a Windows
I try to make an imageview clickable. Actually it is clickable, so that I
I try to make a table header fixed when scoll down on pages. The
I try to make a apps that need barcode scanner, i already can get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.