JSP server hard disk images are displayed in the browser through Servlet, jspservlet

Source: Internet
Author: User

JSP server hard disk images are displayed in the browser through Servlet, jspservlet

Use Servlet to Display Server Hard Disk Images in JSP to browsers

In fact, this instance is very simple, but someone asked me today and I wrote a small example. Let's send it by the way!

Display an image in a browser and use tags

 

The img element embeds an image into the webpage.

Note: Technically, the label does not insert images into the webpage, but links images from the webpage. A tag creates a placeholder space for the referenced image.

A tag has two required attributes: src and alt.

Differences between HTML and XHTML
In HTML, the tag does not have an end tag.
In XHTML, the label must be properly disabled.
In HTML 4.01, the "align", "border", "hspace", and "vspace" attributes of the image element are not recommended.
In XHTML 1.0 Strict DTD, the "align", "border", "hspace", and "vspace" attributes of the image element are not supported.

There are many SRC paths:

Point to other sites (such as src = "http: // www. *******. com/****. jpg ")

Point to files in the site (such as src = "/I/image.gif ")

Many new users ignore the fact that IMG only tells the browser to use realistic images, while the browser obtains the image data stream through the path and then displays the image.

In short, SRC is actually a request sent by the browser, and then the request returns the image data stream to the browser.

Therefore, SRC can also be a request, Servlet, or Action. Here we use Servlet for a simple example.

JSP page:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

 Web. xml configuration:

<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5"    xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  <servlet>   <servlet-name>ImageShowServlet</servlet-name>   <servlet-class>servlet.ImageShowServlet</servlet-class>  </servlet>  <servlet-mapping>   <servlet-name>ImageShowServlet</servlet-name>   <url-pattern>/servlet/ImageShowServlet</url-pattern>  </servlet-mapping>  <welcome-file-list>   <welcome-file>index.jsp</welcome-file>  </welcome-file-list> </web-app>  

Servlet is very simple:

Package servlet; import java. io. byteArrayOutputStream; import java. io. file; import java. io. fileInputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse;/*** @ indicates that the Servlet inputs images of the local hard disk into the pipeline * @ au Thor cuisuqiang * @ version 1.0 * @ since */@ resolve ("serial") public class ImageShowServlet extends HttpServlet {@ Override protected void service (HttpServletRequest request, response) throws ServletException, IOException {OutputStream OS = response. getOutputStream (); File file = new File ("C: \ abc.jpg"); FileInputStream fips = new FileInputStream (file); byte [] btImg = ReadStream (fips); OS. write (btImg); OS. flush ();}/*** read stream data in the pipeline */public byte [] readStream (InputStream inStream) {ByteArrayOutputStream bops = new ByteArrayOutputStream (); int data =-1; try {while (data = inStream. read ())! =-1) {bops. write (data);} return bops. toByteArray ();} catch (Exception e) {return null ;}}}

It is to get the byte stream of the file on the local hard disk and then write it into the pipeline!

The above is the server hard disk image in JSP and displayed to the browser instance through Servlet. If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article and hope to help you, thank you for your support!

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.