Response of java learning
•
Java
Response of java learning
0x00 Preface
Continuing from the previous article, this chapter updates the response.
0x01 Response
Common methods:
setStatus(int sc) : 设置响应状态码
setHeader(String name,String value) 设置响应头
resp.sendRedirect(String Redirect); 302跳转
Forwarding and redirection are different here. Let's see the difference between them.
重定向:
1. 地址栏发生变化
2. 重定向可以访问其他站点(服务器)的资源
3. 重定向是两次请求。不能使用request对象来共享数据
转发:
1. 转发地址栏路径不变
2. 转发只能访问当前服务器下的资源
3. 转发是一次请求,可以使用request对象来共享数据
Redirect Code:
Response class:
@WebServlet("/response")
public class Response extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException {
resp.setStatus(302);
resp.setHeader("location","/demo1");
}
@Override
protected void doPost(HttpServletRequest req,IOException {
super.doPost(req,resp);
}
}
Servletdemo class:
@WebServlet("/demo1")
public class ServeletDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req,IOException {
System.out.println("已跳转");
resp.sendRedirect("http://www.baidu.com");
}
@Override
protected void doPost(HttpServletRequest req,resp);
}
}
The writer method uses:
@WebServlet("/response")
public class Response extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req,IOException {
resp.setCharacterEncoding("gbk"); //设置编码
PrintWriter writer = resp.getWriter(); //获取输出对象
writer.write("response");//输出内容
writer.println("1235");
}
@Override
protected void doPost(HttpServletRequest req,resp);
}
}
Output byte stream
ServletOutputStream outputStream = resp.getOutputStream();
outputStream.write("abc".getBytes());
0x02 ServletContext
Common methods:
request.getServletContext(); : 通过request对象获取
this.getServletContext(); : 通过HttpServlet获取
String getMimeType(String file) : 获取MIME对象
域对象:共享数据
1. setAttribute(String name,Object value)
2. getAttribute(String name)
3. removeAttribute(String name)
获取文件的真实(服务器)路径:
String getRealPath(String path)
File download case:
Write an HTML page with a hyperlink to download the pictures.
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<Meta charset="UTF-8">
<title>downlaod</title>
</head>
<body>
<a href="/demo/img/1.jpg">图片查看</a>
<a href="downloadServlet?filename=1.jpg">图片下载</a>
</body>
</html>
Downloadserlet class:
@WebServlet("/downloadServlet")
public class downloadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
this.doGet(request,response);
}
protected void doGet(HttpServletRequest request,IOException {
String filename = request.getParameter("filename"); //接收filename传参的值
ServletContext servletContext = this.getServletContext(); //创建servletContext 对象
String Path = servletContext.getRealPath("/img/" + filename); //获取文件真实路径
String mimeType = servletContext.getMimeType(filename);//获取mimel类型
FileInputStream fis = new FileInputStream(Path); //路径参数文件输入流对象中
response.setHeader("content-type",mimeType); //响应头设置mime类型
response.setHeader("content-disposition","attachment;filename="+filename); //设置为附件类型
ServletOutputStream outputStream = response.getOutputStream(); //获取文件输出流
byte[] buff = new byte[1024]; //设置每次读取的字节大小
int len =0;
while ((len = fis.read(buff))!=-1){
outputStream.write(buff,len);
}
}
}
Here a simple case is completed.
0x03 end
The contents here are nothing more than the use of several methods, which reflects the power of java object-oriented language.
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码