I. Ecological environment of response and request
Second, HttpServletResponse---representative response object
1. Set the status code
void SetStatus (int sc)
Status code:
200: All normal normal return data
302: Redirect
304: Take the cache
404: Resource Not Found
500: Server Side error (general, code is abnormal)
2. Set the response header (1) API to set the response header
(2) Case-redirect
Redirect:
Implementing redirects: 302+location
Servlet1:
Response.setstatus (302);
Response.setheader ("Location", "url");
Servlet2:
Response.getwriter (). Write ("Hello");
Features of redirection:
1) Client Access two times
2) The address of the client address bar has changed
A convenient way to redirect in response:
void Sendredirect (String location)
(3) Case-timed jump
The refresh head represents a timed jump
Header corresponding to the value of the notation: second value; Url=url address
Response.setheader ("Refresh", "5;url=/response2");
(4) 304 The principle of the status code
3. Set the response body (1) to the client character
PrintWriter writer = Response.getwriter ();
Writer.write (character);
Reason: The Write method when writing is actually writes the content to the response buffer, response's buffer has the default encoding format iso-8859-1 question: Writes the Chinese garbled language to the client
Workaround: Manually set the response buffer encoding
void Setcharacterencoding (String Charset)
Issue: Set response encoding to UTF-8 after discovery page display
Workaround: Use code to specify the encoding reason for page parsing : page default parsing encoding is GBK
Guide the browser to use which encoding for page parsing via Content-type header
Response.setheader ("Content-type", "text/html;charset=utf-8");
Response.setcontenttype ("Text/html;charset=utf-8");
Note: There are two functions within the method of setting Content-type
1) Self-setting response buffer encoding is UTF-8;
2) The code to guide the browser parsing page is UTF-8;
Remember: Solve response Chinese garbled
Response.setcontenttype ("Text/html;charset=utf-8");
(2) Write to client bytes
1) Get byte stream
Servletoutputstram Response.getoutputstream ();
2) Requirements: Output Verification code picture
3) Requirements: File download
Two streams:
Input stream: Read file
Output stream: Written to client---write to response buffer
Two heads:
The MIME type that is displayed to tell the client file
Response.setcontenttype ("Audio/mpeg");
Tells the client to write back the file to be opened as an attachment (download)
Response.setheader ("Content-disposition", "Attachment;filename=1.mp3");
4. Some details of the response
1) Getwriter and Getoutputsteam cannot be called simultaneously in a servlet
2) streams obtained via response do not have to be closed manually
After the service method finishes, the Web container checks to see if the stream response is closed and closes the stream without closing the container.
3) The size of the response buffer is 8k can be set size only to a larger range setting
Third, HttpServletRequest
1. Get Request Line
1) How to get the request
String method = Request.getmethod (); GET POST
2) access to requested resources
Request.getrequesturi ()
Request.getrequesturl ()
Request.getcontextpath (): Name of the web App
1 //1. Request Method2String method =Request.getmethod ();3System.out.println (method);//GET4 //2. Obtaining the requested Resources5String RequestUri =Request.getrequesturi ();6StringBuffer Requesturl =Request.getrequesturl ();7 System.out.println (requesturi);8 System.out.println (requesturl.tostring ());9String ContextPath =Request.getcontextpath ();TenSystem.out.println (ContextPath);
3) Gets the string form of the data for the Get commit
1 // 3. Gets the arguments of the string form of the Get commit 2 String querystr = request.getquerystring (); 3 System.out.println (QUERYSTR);
4) Get the parameters of the client
1 // 4, obtain the client's IP 2 String remoteaddr = request.getremoteaddr (); 3 System.out.println (REMOTEADDR);
2. Get Request Header
1 //1. Get a specified header2String Header = Request.getheader ("User-agent");3 System.out.println (header);4 //2. Determine the browser type of the client5 if(Header.contains ("MSIE")){6System.out.println ("This is IE");7}Else if(Header.contains ("Firefox")){8System.out.println ("This is Firefox");9}Else{TenSystem.out.println ("This is the other"); One } A //3, get all the names of the head -System.out.println ("-------------------------------------------"); -Enumeration Headernames =request.getheadernames (); the while(Headernames.hasmoreelements ()) { -String Headername =(String) headernames.nextelement (); -System.out.println (headername+ ":" +Request.getheader (Headername)); -}3. Get the request body (get the parameters of the post submission)
Format of the request body:
Username=zhangsan&password=123&hobby=zq&hobby=pq
1) String Getparamter (name)
Obtained Zhangsan:request.getParameter ("username");
2) string[] Getparametervalues (name)
Get the Hobby hobby:request.getParamterValues ("hobby");
3) map<string,string[]> Getparametermap ()
Get the above request body:
Username:{zhangsan}
password:{12345}
HOBBY:{ZQ,PQ}
4) Getparameternames (): Get the name of all request parameters
Get the above request body:
[Username,password,hobby]
1 //1. Get username---????2String username = request.getparameter ("username");3 //Get mode4 //Encoding---byte array5 byte[] bytes = Username.getbytes ("Iso8859-1");6 //Decoding---utf-87Username =NewString (Bytes, "UTF-8");8 9 Ten System.out.println (username); One //2, Password AString Password = request.getparameter ("Password"); - System.out.println (password); - //3. Get Hobby thestring[] Hobby = request.getparametervalues ("Hobby"); - System.out.println (hobby.length); - //4, get all the parameters of the map form -Map<string,string[]> Parametermap =Request.getparametermap (); +System.out.println (PARAMETERMAP);
Problem: page is garbled when it is submitted to server side
Reason: The default encoding of the request object is Iso-8859-1
Solution: Set the encoding of request to UTF-8 (for post submission only)
Request.setcharacterencoding ("UTF-8");
Note: Get how to solve Chinese garbled characters
The encoding of the client is (encoded) UTF-8----> Server-side request encoding is (decoded) iso8859-1--->???? ???? ---> Decoding with iso8859-1 encoding---->utf-8
4. Request is a domain object and requests forwarding
Syntax for forwarding:
Request.getrequestdispatcher ("Address to be forwarded"). Forward (Request,response);
1 request.getrequestdispatcher ("/request2"). Forward (request, response);
Request Domain Visibility Range: In a single request chain
5. The request contains
Grammar:
Request.getrequestdispatcher ("Address of the resource to include"). Include (Request,response);
Java Combat 04javaweb-02request and response