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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:13:55+00:00 2026-05-15T04:13:55+00:00

Is there anyway to scale an image then display in jsp page? When retrieve

  • 0

Is there anyway to scale an image then display in jsp page? When retrieve and display the images, I want to show all photos in same size. is there any API can do it? I have searched from google, those I found was about scaling images byusing tookit but can’t works in web application.

  • 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-15T04:13:56+00:00Added an answer on May 15, 2026 at 4:13 am

    You can use the builtin Java 2D API for this (basic Sun tutorial here).

    Basically, you need to create a Servlet which gets an InputStream of the original image in the doGet() method, passes it through the Java 2D API and then writes it to the OutputStream of the HTTP response. Then you just map this Servlet on a certain url-pattern in web.xml, e.g. /thumbs/* and call this Servlet in the src attribute of the HTML <img> element.

    Here’s a basic kickoff example (you still need to handle unexpected conditions yourself the way you want):

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // First get image file name as request pathinfo (or parameter, whatever you want).
        String imageFilename = request.getPathInfo().substring(1);
    
        // And get the thumbnail dimensions as request parameters as well.
        int thumbWidth = Integer.parseInt(request.getParameter("w"));
        int thumbHeight = Integer.parseInt(request.getParameter("h"));
    
        // Then get an InputStream of image from for example local disk file system.
        InputStream imageInput = new FileInputStream(new File("/images", imageFilename));
    
        // Now scale the image using Java 2D API to the desired thumb size.
        Image image = ImageIO.read(imageInput);
        BufferedImage thumb = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = thumb.createGraphics();
        graphics2D.setBackground(Color.WHITE);
        graphics2D.setPaint(Color.WHITE); 
        graphics2D.fillRect(0, 0, thumbWidth, thumbHeight);
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
    
        // Write the image as JPG to the response along with correct content type.
        response.setContentType("image/jpeg");
        ImageIO.write(thumb, "JPG", response.getOutputStream());
    }
    

    With the servlet mapped in web.xml as follows:

    <servlet>
        <servlet-name>thumbServlet</servlet-name>
        <servlet-class>com.example.ThumbServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>thumbServlet</servlet-name>
        <url-pattern>/thumbs/*</url-pattern>        
    </servlet-mapping>
    

    This can be used as follows:

    <img src="thumbs/filename.jpg?w=100&h=100" width="100" height="100">
    

    Note: no, this cannot be done with JSP alone since it’s a view technology unsuitable for this task.


    Note 2: this is a pretty expensive (CPU intensive) task, keep this in mind. You may want to consider to cache or pregenerate the thumbs beforehand yourself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there anyway to keep images in the gallery set at all times? Instead
Is there anyway or any addin for VS2010 that can remove all the comments
I want to draw tiled images and then transform them by using the usual
Is there anyway to highlight the initial text in a textbox on a web
Is there anyway to get the Facebook ID of a user without forcing them
Is there anyway to create an ObjectSet from a DbSet ? Some background information:
Is there anyway to open the twitter app from an html link on an
is there anyway to get the class when click event is fired. My code
Is there anyway to get access to stackoverflow's awesome tagging system? I would like
Is there anyway to serialize a List of datacontracts? I have created a datacontract

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.