当前位置:网站首页>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
边栏推荐
- CTF_ Web: Advanced questions of attack and defense world expert zone WP (15-18)
- At the age of 30, I began to learn programming by myself. Is it still time for me to have difficulties at home?
- CTF_ Web: how to recognize and evaluate a regular expression
- Php7.2 add JPEG extension
- 哪个编程语言实现hello world最烦琐?
- cannot import name ‘escape’ from ‘jinja2’【成功解决】
- 冰冰学习笔记:循环队列的实现
- 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)
- Chapter IX app project test (2) test tools
- 深度学习——几种学习类型
猜你喜欢

深度学习——几种学习类型

CTF_ Web: how to recognize and evaluate a regular expression

Separation of storage and computing in Dahua cloud native database

电脑的dwg文件怎么打开

魔法猪系统重装大师怎么使用

JS, BOM, DOM (VI)

Deep learning - several types of learning

Part I Verilog quick start

Cnpm: unable to load file c:\users\administrator\appdata\roaming\npm\cnpm PS1 because running scripts is prohibited on this system.

Kotlin Compose 完善toDo项目 Surface 渲染背景 与阴影
随机推荐
Introduction to the isolation level of gbase 8s
Multithreading structure of gbase 8s
计算学生成绩等级(虚函数和多态)
【Flink】RocksDB增量模式checkpoint大小持续增长的问题及解决
The SQL response is slow. What are your troubleshooting ideas?
OOP stack class template (template +ds)
Get to know the drawing component of flutter - custompaint
leetcode1221. 分割平衡字符串
Vscade setting clang format
Méthode de récupération des données d'ouverture du disque dur à l'état solide
【Keil】ADuCM4050官方库的GPIO输出宏定义
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
[esp32 learning path 6 - Flash encryption]
Efficient NoSQL database service Amazon dynamodb experience sharing
坐标系左乘右乘
《牛客刷verilog》Part I Verilog快速入门
Why is the TCP handshake just 3 times?
CTF_ Web: how to recognize and evaluate a regular expression
My IC journey - the growth of senior chip design verification engineers - "Hu" said that IC engineers are perfect and advanced