Application of common methods based on HttpServletResponse correlation _java

Source: Internet
Author: User

public void filedownload(httpservletresponse response) throws Exception {
ServletContext context = This.getservletcontext ();
String Path = Context.getrealpath ("/download/awf.jpg");
String filename = path.substring (path.lastindexof ("\") + 1);

If the download file is a Chinese file, the filename needs to be encoded by URL;
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 Use :
Beanutils.pupulate (bean,mapinstance);////Map load the value of key and key corresponding to the bean attribute in Bean,map;
Beanutils.copyproperties (bean,mapinstance);//copy map to Bean;

Forwarding is a request that 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);//Send messages to Message page for display;

<2>response.setheader ("Refresh", "3;url= '/webtwo/index.jsp '");
Response.getwriter (). Write ("Congratulations login success, if there is no relay, please click the hyperlink <a href= ' webtwo/index.jsp '");

Program code:
Program to what code table output, you must control what the browser to open the Code table;
Using meta technology in HTML to simulate HTTP response headers to control the behavior of the browser;
Out.write ("<meta http-equiv= ' content-type ' content= ' Text/html;charset=utf-8 ' >"). getBytes ());
  

Response.setcharacterencoding ("UTF-8");/set the code table used by the response to control what code table the response writes data to the browser;
Response.setheader ("Content-type", "text/html;charset=utf-8");//Specify what code table the browser opens the data in;
Quite a few words above:
Response.setcontenttype ("Text/html;charset=utf-8");

Response.setdateheader ("Expires", System.currenttimemillis () + 1000*3600);/Set session effective time 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.