当前位置:网站首页>Response (XI)
Response (XI)
2022-06-25 04:47:00 【hercu1iz】
HTTP agreement :
1. The request message : Data sent by client to server
* data format :
1. Request line
2. Request header
3. Request a blank line
4. Request body
2. The response message : Data sent by the server to the client
* data format :
1. Response line
1. form : agreement / edition Response status code Status code description
2. Response status code : The server tells the client browser the status of this request and response .
1. Status codes are 3 Digit number
2. classification :
1. 1xx: The server receives the client message , But didn't accept completion , Wait a while , send out 1xx Multi status code
2. 2xx: success . representative :200
3. 3xx: Redirect . representative :302( Redirect ),304( Access cache )
4. 4xx: Client error .
* representative :
* 404( Request path has no corresponding resource )
* 405: The request method does not correspond to doXxx Method
5. 5xx: Server-side error . representative :500( An exception occurred inside the server )
2. Response head :
1. Format : Header name : value
2. Common response headers :
1. Content-Type: The server tells the client the data format and encoding format of the response
2. Content-disposition: The server tells the client what format to open the response body data
* value :
* in-line: The default value is , Open... In the current page
* attachment;filename=xxx: Open the response body as an attachment . File download
3. Respond to blank lines
4. Response body : Data transmitted
* Response string format
HTTP/1.1 200 OK
Content-Type: text/html;charset=UTF-8
Content-Length: 101
Date: Wed, 06 Jun 2018 07:08:42 GMT
<html>
<head>
<title>$Title$</title>
</head>
<body>
hello , response
</body>
</html>
Response object
* function : Set response message
1. Set the response line
1. Format :HTTP/1.1 200 ok
2. Set status code :setStatus(int sc)
2. Set the response header :setHeader(String name, String value)
3. Set the response body :
* Use steps :
1. Get the output stream
* Character output stream :PrintWriter getWriter()
* Byte output stream :ServletOutputStream getOutputStream()
2. Use output stream , Output data to client browser
* Case study :
1. Complete redirection
* Redirect : The way resources jump
* Code implementation :
//1. Set the status code to 302
response.setStatus(302);
//2. Set the response header location
response.setHeader("location","/day15/responseDemo2");
// Simple redirection method
response.sendRedirect("/day15/responseDemo2");
* Features of redirection :redirect
1. The address bar changes
2. Redirection can access other sites ( The server ) Resources for
3. Redirection is two requests . Out of commission request Object to share data
* Features of forwarding :forward
1. The path of the forwarding address bar does not change
2. Forwarding can only access resources under the current server
3. Forwarding is a request , have access to request Object to share data
* forward and redirect difference
* Path writing :
1. Path classification
1. Relative paths : It is not possible to determine a unique resource through a relative path
* Such as :./index.html
* Don't to / start , With . The beginning path
* The rules : Find the relative location relationship between the current resource and the target resource
* ./: Current directory
* ../: Go back one level
2. Absolute path : The only resource can be determined by an absolute path
* Such as :http://localhost/day15/responseDemo2 /day15/responseDemo2
* With / Path at the beginning
* The rules : Judge who the defined path is for ? Determine where the request will come from
* to client Browsers use : Need to add virtual directory ( Access path of the project )
* It is recommended that the virtual directory be obtained dynamically :request.getContextPath()
* <a> , <form> Redirect ...
* to The server Use : No need to add virtual directory
* Forwarding path
2. Server output character data to browser
* step :
1. Get character output stream
2. Output data
* Be careful :
* The code problem :
1. PrintWriter pw = response.getWriter(); The default encoding for the acquired stream is ISO-8859-1
2. Set the default encoding for the stream
3. Tell the browser the code used for the response body
// Simple form , Set encoding , It is set before getting the stream
response.setContentType("text/html;charset=utf-8");
3. The server outputs byte data to the browser
* step :
1. Get byte output stream
2. Output data
4. Verification Code
1. The essence : picture
2. Purpose : Prevent malicious form registration
ServletContext object :
1. Concept : On behalf of the entire web application , Containers that can be used with programs ( The server ) To communication
2. obtain :
1. adopt request Object acquisition
request.getServletContext();
2. adopt HttpServlet obtain
this.getServletContext();
3. function :
1. obtain MIME type :
* MIME type : A file data type defined in the process of Internet communication
* Format : Large type / Subtype text/html image/jpeg
* obtain :String getMimeType(String file)
2. Domain object : Shared data
1. setAttribute(String name,Object value)
2. getAttribute(String name)
3. removeAttribute(String name)
* ServletContext Object scope : All users all requested data
3. Get the truth of the document ( The server ) route
1. Method :String getRealPath(String path)
String b = context.getRealPath("/b.txt");//web Resource access under Directory
System.out.println(b);
String c = context.getRealPath("/WEB-INF/c.txt");//WEB-INF Resource access under Directory
System.out.println(c);
String a = context.getRealPath("/WEB-INF/classes/a.txt");//src Resource access under Directory
System.out.println(a);
Case study :
* File download requirements :
1. The page shows hyperlinks
2. Click the hyperlink and a download prompt box will pop up
3. Complete image file download
* analysis :
1. If the resources that the hyperlink points to can be parsed by the browser , Show it in the browser , If you can't parse , The download prompt box will pop up . Not meeting demand
2. Any resource must have a download prompt box
3. Use response headers to set how resources are opened :
* content-disposition:attachment;filename=xxx
* step :
1. Define page , Edit hyperlink href attribute , Point to Servlet, Pass the resource name filename
2. Definition Servlet
1. Get the file name
2. Use byte input stream to load file into memory
3. Appoint response Response header for : content-disposition:attachment;filename=xxx
4. Write the data to response Output stream
* problem :
* Problems with Chinese documents
* Solutions :
1. Get the browser version information used by the client
2. According to different version information , Set up filename Are coded in different ways
边栏推荐
- Gbase 8s memory management
- CTF_ Web: Advanced questions of attack and defense world expert zone WP (15-18)
- 高效的NoSQL数据库服务Amozon DynamoDB体验分享
- LabVIEW development gas regulator
- halcon之区域:多种区域(Region)生成(3)
- OpenSea PHP开发包
- [image fusion] image fusion based on MATLAB directional discrete cosine transform and principal component analysis [including Matlab source code 1907]
- How do the defi protocols perform under this round of stress test?
- Concat() in JS
- Gbase 8s parallel operation problem scenario description
猜你喜欢

ASEMI三相整流桥的工作原理

在 .NET 6 中使用 dotnet format 格式化代码

CTF_ Web: Advanced questions of attack and defense world expert zone WP (19-21)

Concat() in JS

【FLink】access closed classloader classloader.check-leaked-classloader

olap分析引擎——Kylin4.0

Paper notes: multi label learning ESMC (I don't understand it, but I haven't written it yet, so I'll put it here for a place temporarily)

Simple text analysis of malicious samples - Introduction

Record the problem of C # print size once

ROS2/DDS/QoS/主题的记录
随机推荐
How do the defi protocols perform under this round of stress test?
以太网是什么要怎么连接电脑
Record the problem of C # print size once
固态硬盘开盘数据恢复的方法
Concat() in JS
[untitled]
Bingbing's learning notes: implementation of circular queue
Part I Verilog quick start
Record small knowledge points
Gbase 8s stored procedure execution and deletion
Code scanning payment flow chart of Alipay payment function developed by PHP
Gbase 8s stored procedure flow control
php开发支付宝支付功能之扫码支付流程图
Data view for gbase 8s
CTF_ Web: Changan cup-2021 old but a little new & asuka
Immutable學習之路----告別傳統拷貝
PHP encapsulates curl to send get and post request methods, and uses
Web3 DApp用户体验最佳实践
XML (VIII)
JS call() and apply()