Nine implicit JSP objects ----- jsp implements image download

Source: Internet
Author: User

 

JSP nine implicit objects

Request

Response

Config

Application

Exception

Session

Page

Out

PageContext

Out implicit object

The out implicit object is used to send text data to the client.

The out object is returned by calling the getOut method of the pageContext object. Its function and usage are very similar to the PrintWriter object returned by the ServletResponse. getWriter method.

The out implicit object type on the JSP page is JspWriter. JspWriter is equivalent to a PrintWriter with the cache function. You can adjust the cache size by setting the buffer attribute of the page instruction on the JSP page, even disable its cache.

The out object calls ServletResponse only when the content written to the out object meets any of the following conditions. the getWriter method returns the PrintWriter object to write the content in the buffer zone of the out object to the buffer zone provided by the Servlet engine:

The buffer attribute of the page instruction is set to disable the cache function of the out object.

The buffer of the out object is full.

End of the entire JSP page

PageContext object

The pageContext object is the most important object in JSP technology. It represents the running environment of JSP pages. This object not only encapsulates references to other 8 hidden objects, but also is a domain object, it can be used to save data. In addition, this object also encapsulates some common operations that are often involved in web development, such as introducing and redirecting other resources and retrieving attributes in other domain objects.

Here is an example: use JSP to download files.

<% @ Page import = "java.net. URLEncoder "%> <% @ page import =" java. io. file "%> <% @ page import =" java. io. inputStream "%> <% @ page import =" java. io. fileInputStream "%> <% @ page import =" java. io. outputStream "%> <% @ page language =" java "import =" java. util. * "pageEncoding =" ISO-8859-1 "%> <%

String path = getServletContext (). getRealPath ("./images/11.jpg ");

File file = new File (path );

InputStream is = new FileInputStream (file );

Response. setHeader ("content-disposition", "attachment; filename =" + URLEncoder. encode (file. getName (), "UTF-8 "));

OutputStream OS = response. getOutputStream ();

Byte B [] = new byte [1024];

Int flag = 0;

While (flag = is. read (B ))! =-1 ){

OS. write (B, 0, flag );

}

OS. close ();

Is. close ();

%>

Remember that there must be no space.

 

Chenglong0513

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.