How to display pictures stored in a database on a JSP page

Source: Internet
Author: User






1. Find the binary data of the picture from the database, set the found data into the session



Request.getsession (). SetAttribute ("img", binary data of the picture);






2. Write the data into the stream


 
 1 package cn.jbit.auction.web.servlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 public class ShowImgServlet extends HttpServlet {
11     private static final long serialVersionUID = 306275585861980357L;
12     public void doGet(HttpServletRequest request, HttpServletResponse response)
13             throws ServletException, IOException {
14         doPost(request, response);
15     }
16 
17     public void doPost(HttpServletRequest request, HttpServletResponse response)
18             throws ServletException, IOException {
19         byte[] imgBytes = (byte[]) request.getSession().getAttribute("img");
20         response.getOutputStream().write(imgBytes);
21     }
22 }





3. Change the configuration of the Web. xml file (modify <url-pattern> parameters)



<servlet-mapping>
<servlet-name>ShowImgServlet</servlet-name>
<url-pattern>/auctionimg.jpg</url-pattern>
</servlet-mapping>






4. Display on JSP page



/auctionimg.jpg" width= "+ height=" 185 "alt=" picture/>



How to display pictures stored in a database on a JSP page


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.