当前位置:网站首页>Response object - response character data
Response object - response character data
2022-07-26 16:37:00 【Chen Yuchen】
Response The response data
1. response Return the character data to the browser
Two steps are needed. :
- adopt response Object to get the character output stream PrintWriter writer = resp.getWriter();
- Write data through character output stream :writer.writer(“aaa”);
Case study
Print out characters in browser
/** * Response character data : Set the response body of character data */
@WebServlet("/resp3")
public class ResponseDemo3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//1. Get character output stream
PrintWriter writer = response.getWriter();
writer.write("aaa");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
You can go back to html character string , And the browser can parse it , as follows :
PrintWriter writer = response.getWriter();
//content-type, Tell the browser what type of data to return HTML Type data , In this way, the browser will parse HTML label
response.setHeader("content-type","text/html");
writer.write("<h1>aaa</h1>");
Be careful : After a request response ,response The object will be destroyed , So don't close the flow manually
remind If you want to return a Chinese string Hello , You need to set the response data code to utf-8
// Set the data format and code of the response
response.setContentType("text/html;charset=utf-8");
writer.write(" Hello ");
2. response Response byte data
You need to write byte data back to the browser , We need two steps :
- adopt response Object to get byte output stream :servletOutputStream outputStream = resp.getOutputStream();
- Write data through byte output stream :outputStream.write( Bytes of data );
1. Returns a picture file to the browser , as follows :
/** * Response byte data : Set the response body of byte data */
@WebServlet("/resp4")
public class ResponseDemo4 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. Read the file
FileInputStream fis = new FileInputStream("d://a.jpg");
//2. obtain response Byte output stream
ServletOutputStream os = response.getOutputStream();
//3. Completion flow copy
byte[] buff = new byte[1024];
int len = 0;
while ((len = fis.read(buff))!= -1){
os.write(buff,0,len);
}
fis.close();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
In the above code , For flow copy The code is more complex , So we can use good methods provided by others to simplify code development , The specific steps are as follows :
- pom.xml Add dependency
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
- Call tool class method
//fis: Input stream
//os: Output stream
IOUtils.copy(fis,os);
After optimizing the code :
/** * Response byte data : Set the response body of byte data */
@WebServlet("/resp4")
public class ResponseDemo4 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. Read the file
FileInputStream fis = new FileInputStream("d://a.jpg");
//2. obtain response Byte output stream
ServletOutputStream os = response.getOutputStream();
//3. Completion flow copy
IOUtils.copy(fis,os);
fis.close();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
边栏推荐
- 量化交易之数字货币篇 - 通过时间戳与方向来合并逐笔成交数据(大单合并)
- 2022 Niuke summer multi school training camp 1 (acdgij)
- [ten thousand words long text] Based on LSM tree thought Net 6.0 C # realize kV database (case version)
- Guangzhou Municipal Safety Committee Office issued warnings and reminders on safety precautions in hot weather
- [e-mr] error recovery record of namenode
- Comprehensively design an oppe homepage -- the design of the top and head
- A firefox/chrome plug-in that visualizes browser history
- The difference between oncreate and onrestoreinstancestate recovery data of activity
- NUC 11 build esxi 7.0.3f install network card driver-v2 (upgraded version in July 2022)
- How to test the circle of friends (mind map)
猜你喜欢

Digital intelligence transformation, management first | jnpf strives to build a "full life cycle management" platform

Comprehensive design of an oppe homepage -- Design of navigation bar
![[RCTF2015]EasySQL](/img/68/328ee5cffc8b267b6b0f284eb8db2c.png)
[RCTF2015]EasySQL

综合设计一个OPPE主页--导航栏的设计

Vscode batch delete

Tdengine landed in GCL energy technology, with tens of billions of data compressed to 600gb

Re8:读论文 Hier-SPCNet: A Legal Statute Hierarchy-based Heterogeneous Network for Computing Legal Case

docker安装redis?如何配置持久化策略?

Re7:读论文 FLA/MLAC Learning to Predict Charges for Criminal Cases with Legal Basis

第一章概述-------第一节--1.3互联网的组成
随机推荐
2022牛客暑期多校训练营1(ACDGIJ)
SQL statement -- single line comment and multi line comment
kubernetes之ConfigMap
基于sisotool极点配置PI参数及基于Plecs的三相电压源逆变器仿真
Pat grade a 1044 shopping in Mars
FTP protocol
There are six ways to help you deal with the simpledateformat class, which is not a thread safety problem
Differences between the use of structs and classes
docker安装redis?如何配置持久化策略?
Docker install redis? How to configure persistence policy?
Use verdaccio to build your own NPM private library
Tdengine landed in GCL energy technology, with tens of billions of data compressed to 600gb
C语言重点知识总结
2022 latest Tibet Construction scaffolder (construction special operation) simulation exam questions and answers
How to write unit tests
It turns out that cappuccino information security association does this. Let's have a look.
搭建typora图床
面试时候常说的复杂度到底是什么?
kubernetes之ReplicationController与ReplicaSet
Comprehensive design of an oppe homepage -- Design of navigation bar