当前位置:网站首页>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
边栏推荐
- MySQL concept and operation (III)
- Classification of gbase 8s locks
- Vscode 设置clang-format
- js的call()和apply()
- 哪个编程语言实现hello world最烦琐?
- 初识 Flutter 的绘图组件 — CustomPaint
- Region of Halcon: generation of multiple regions (3)
- 使用文本分析识别一段文本中的主要性别
- 30岁了开始自学编程,家里比较困难还来得及吗?
- [Flink] problems and solutions of the continuous growth of checkpoint size in rocksdb incremental mode
猜你喜欢

2.0springmvc uses restful

Record the problem of C # print size once

Sleep more, you can lose weight. According to the latest research from the University of Chicago, sleeping more than 1 hour a day is equivalent to eating less than one fried chicken leg

第九章 APP项目测试(2) 测试工具

Xiaobai learns MySQL - Statistical 'opportunism'

多睡觉,能减肥,芝加哥大学最新研究:每天多睡1小时,等于少吃一根炸鸡腿...

Vscade setting clang format

固态硬盘开盘数据恢复的方法

大话云原生数据库中的存算分离

File upload vulnerability shooting range upload labs learning (pass1-pass5)
随机推荐
哪个编程语言实现hello world最烦琐?
多睡觉,能减肥,芝加哥大学最新研究:每天多睡1小时,等于少吃一根炸鸡腿...
DMA double buffer mode of stm32
Structure syntaxique des procédures stockées gbase 8S
Blob page in gbase 8s
为什么SQL语句命中索引比不命中索引要快?
Deep learning - several types of learning
Why PHP is not safe
JS' sort() function
重磅直播 | 相移法+多频外差之数学原理推导+实现
Immutable學習之路----告別傳統拷貝
初识 Flutter 的绘图组件 — CustomPaint
本轮压力测试下,DeFi协议们表现如何?
Data import and export for gbase 8s
Upgrade PHP to php7 The impact of X (I). The problem of session retention. Keep login
CTF_ Web: Learn flask template injection (SSTI) from 0
Calculate student grade (virtual function and polymorphism)
Cannot import name 'escape' from 'jinja2' [solved successfully]
js的arguments
Gbase 8s overall architecture