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 6012367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:23:39+00:00 2026-05-23T02:23:39+00:00

Servlet doGet() code for getting an Image from Database and to store image in

  • 0

Servlet doGet() code for getting an Image from Database and to store image in Response

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

// Get userid from session

try {

    // Get photos from database in (image)

    // Init servlet response.
    response.reset();
    response.setBufferSize(DEFAULT_BUFFER_SIZE);
    response.setContentType(image.getContenttype());
    response.setHeader("Content-Length", String.valueOf(image.getLength()));
    response.setHeader("Content-Disposition", "inline; filename=\"" + image.getTitle()
    + "\"");

    // Prepare streams.
    BufferedInputStream input = null;
    BufferedOutputStream output = null;

    try {
        // Open streams.
        input = new BufferedInputStream(image.getPhoto(), DEFAULT_BUFFER_SIZE);
        output = new BufferedOutputStream(response.getOutputStream(),
                 DEFAULT_BUFFER_SIZE);

        // Write file contents to response.
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        int length;
        while ((length = input.read(buffer)) > 0) {
            output.write(buffer, 0, length);
        }
    } finally {
        // Gently close streams.
        output.close();
        input.close();
    }

    //Redirect it to photo page
    RequestDispatcher rd = request.getRequestDispatcher
        ("/webplugin/jsp/profile/photos.jsp");
    rd.forward(request, response);

} catch (Exception e) {
    e.printStackTrace();
}

}

However, When this servlet shows the JSP page it shows only image and not the JSP page.

JSP code:

... JSP code

<img src="Servlet url">

... JSP code cont...

What output I get:

  1. I only get Image instead of image inside JSP
  2. When I use RequestDispatcher/sendRedirect() I get following Exception java.lang.IllegalStateException: Cannot forward after response has been committed

Question:

  1. How to get Image inside JSP instead of just Image in browser
  2. How to avoid above Exception?

EDIT: My Web.xml looks like this

<servlet>
    <servlet-name>Photo Module</servlet-name>
    <servlet-class>app.controllers.PhotoServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Photo Module</servlet-name>
    <url-pattern>/Photos</url-pattern>
</servlet-mapping>
  • 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-05-23T02:23:40+00:00Added an answer on May 23, 2026 at 2:23 am

    How to get Image inside JSP instead of just Image in browser

    Enter the URL to the JSP file containing the <img> element in the browser address bar.

    http://localhost:8080/contextname/webplugin/jsp/profile/photos.jsp


    How to avoid above Exception?

    Remove the following lines from the servlet code.

    //Redirect it to profile page
    RequestDispatcher rd = request.getRequestDispatcher
        ("/webplugin/jsp/profile/photos.jsp");
    rd.forward(request, response);
    

    The servlet should just return the image. Nothing more. It’s the webbrowser itself who is supposed to download and display the image, not the webserver.

    See also:

    • How to retrieve image from database and display in JSP via Servlet?
    • How to retrieve and display images from a database in a JSP page?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My servlet function looks like these: CODE: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
I have the following code... @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
I have the following scala code: package dummy import javax.servlet.http.{HttpServlet, HttpServletRequest => HSReq, HttpServletResponse
@RequestMapping(value = /testerror, method = RequestMethod.GET) public @ResponseBody ErrorTO testerror(HttpServletRequest request, HttpServletResponse response) {
public class Servlet2Stateless extends HttpServlet { @EJB private HelloUserLocal helloUser; @Override public void doGet(HttpServletRequest
I am creating a servlet(proxy server) which handles multiple request from the client.I do
I am getting the following error from my code: Attempt to split long or
When you get a doGet or doPost call in a servlet you can use
I'd like to hava a Dojo dojox.grid.DataGrid with its data from a servlet. Problem:
I'm trying to get the output from a servlet on an Android phone. This

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.