Application of common methods based on HttpServletResponse

Source: Internet
Author: User

Public voidFiledownload(HttpServletResponse response) throws Exception {
ServletContext context = this. getServletContext ();
String path = context. getRealPath ("/download/awf.jpg ");
String filename = path. substring (path. lastIndexOf ("\") + 1 );

// If the downloaded file is a Chinese file, the file name must be url encoded;
Response. setHeader ("Content-disposition", "attachment; filename =" + URLEncoder. encode (filename, "UTF-8 "));

InputStream in = new FileInputStream (path );
Int len = 0;
Byte [] buffer = new byte [1024];
OutputStream out = response. getOutputStream ();
While (len = in. read (buffer)> 0 ){
Out. write (buffer, 0, len );
}
In. close ();
Out. close ();
}

BeanUtils:
BeanUtils. pupulate (bean, MapInstance); // load the bean with map. The map contains the key corresponding to the bean attribute and the value corresponding to the key;
BeanUtils. copyProperties (bean, MapInstance); // copy map to bean;

A forward request uses the same response and request;

Page jump:
<1> String message = "<meta http-equiv = 'refresh' content = '3; url =/webTwo/index. jsp '> <a href = 'webtwo/index. jsp '> AAAA </a> ";
This. getServletContext (). setAttribute ("message", message );
This. getServletContext (). getRequestDispatcher ("/message. jsp"). forward (request, response); // bring the message to the message page for display;

<2> response. setHeader ("refresh", "3; url = '/webTwo/index. jsp '");
Response. getWriter (). write ("congratulations, the logon is successful. If there is no transit, click the hyperlink <a href = 'webtwo/index. js'> AAAA </a> ");

Program code:
// What code table the program outputs must control which code table the browser opens;
// Use meta Technology in html to simulate http response headers to control browser behavior;
// Out. write ("<meta http-equiv = 'content-type' content = 'text/html; charset = UTF-8 '>". getBytes ());
  

Response. setCharacterEncoding ("UTF-8"); // set the code table used by response to control the code table in which response writes data to the browser;
Response. setHeader ("Content-type", "text/html; charset = UTF-8"); // specifies the code table in which the browser opens the data;
// Equivalent to the above two sentences:
// Response. setContentType ("text/html; charset = UTF-8 ");

Response. setDateHeader ("expires", System. currentTimeMillis () + 1000*3600); // set the session validity period to 10 minutes;
Response. getWriter (). write (data); Response. getWriter () --> return PrintWriter;
Response. setHeader ("refresh", "3 ");

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.