1 HttpServletResponse Overview
2 Response Operation Process
3 Grab the HTTP response by grasping the package tool
4 response Lines
5 Setting the response header
The difference between set and add
6 redirects
Need to complete analysis '
6 Set the response Body 6.1 set text and garbled
//set the Code table for the response query//response.setcharacterencoding ("UTF-8"); //tell the client what code table to use with a header Content-type//Response.setheader ("Content-type", "Text/html;charset=utf-8");Response.setcontenttype ("Text/html;charset=utf-8"); PrintWriter writer=Response.getwriter (); //writer.write ("Hello response!!!");Writer.write ("Hello");
6.2 Setting bytes
6.3 File Download
//get the name of the file you want to downloadString filename = Request.getparameter ("filename");//a.flv//The type of this file to download-----client to distinguish types by MIME type of fileResponse.setcontenttype ( This. Getservletcontext (). GetMimeType (filename); //tells the client that the file is not parsed directly but opens as an attachment (download)Response.setheader ("content-disposition","attachment;filename="+filename); //gets the absolute path of the fileString Path = This. Getservletcontext (). Getrealpath ("download/"+filename); //get the input stream for the fileInputStreaminch=NewFileInputStream (path); //get the output stream---the output stream obtained through response is used to write content to the clientServletoutputstream out=Response.getoutputstream (); //template code for file copy intLen =0; byte[] buffer =New byte[1024x768]; while((len=inch. Read (buffer)) >0){ out. write (buffer,0, Len); } inch. Close (); //out.close ();
6.4 File Download (English)
//******* file name is in Chinese download *******//get the name of the file you want to downloadString filename = Request.getparameter ("filename");//????. JPG//solve the problem of getting Chinese parameter garbled----the next lessonfilename =NewString (Filename.getbytes ("iso8859-1"),"UTF-8");//beautiful. jpg//get the User-agent in the request headerString Agent = Request.getheader ("user-agent"); //different encodings based on different browsersString Filenameencoder =""; if(Agent.contains ("MSIE")) { //IE BrowserFilenameencoder = Urlencoder.encode (filename,"Utf-8"); Filenameencoder= Filenameencoder.replace ("+"," "); } Else if(Agent.contains ("Firefox")) { //Firefox BrowserBase64encoder Base64encoder =NewBase64encoder (); Filenameencoder="=?utf-8? B?"+ Base64encoder.encode (filename.getbytes ("Utf-8")) +"?="; } Else { //Other BrowsersFilenameencoder = Urlencoder.encode (filename,"Utf-8"); } //The type of this file to download-----client to distinguish types by MIME type of fileResponse.setcontenttype ( This. Getservletcontext (). GetMimeType (filename); //tells the client that the file is not parsed directly but opens as an attachment (download)----filename= "+filename client decodes the name by defaultResponse.setheader ("content-disposition","attachment;filename="+Filenameencoder); //gets the absolute path of the fileString Path = This. Getservletcontext (). Getrealpath ("download/"+filename); //get the input stream for the fileInputStreaminch=NewFileInputStream (path); //get the output stream---the output stream obtained through response is used to write content to the clientServletoutputstream out=Response.getoutputstream (); //template code for file copy intLen =0; byte[] buffer =New byte[1024x768]; while((len=inch. Read (buffer)) >0){ out. write (buffer,0, Len); } inch. Close (); //out.close ();
7 Response Details
Jsp-response (HttpServletResponse)