web服务器接收到客户端的http请求,针对这个请求分别创建一个代表请求的HttpServletResponse对象,一个代表响应的HttpServletResponse对象;
- 如果要获取客户端请求过来的参数:找HttpServletRequest
- 如果要客户端响应一些信息:找HttpServletResponse
- 负责向浏览器发送数据的方法
public ServletOutputStream getOutputStream() throws IOException; public PrintWriter getWriter() throws IOException;
- 负责向浏览器**发送响应头(encoding…)**的方法
void setCharacterEncoding(String charset); void setContentLength(int len); void setContentLengthLong(long length); void setContentType(String type); void setDateHeader(String name, long date); void addDateHeader(String name, long date); void setHeader(String name, String value); void addHeader(String name, String value); void setIntHeader(String name, int value); void addIntHeader(String name, int value);
- 响应的状态码
public static final int SC_CONTINUE = 100;
public static final int SC_SWITCHING_PROTOCOLS = 101;
public static final int SC_OK = 200;
public static final int SC_CREATED = 201;
public static final int SC_ACCEPTED = 202;
public static final int SC_NON_AUTHORITATIVE_INFORMATION = 203;
public static final int SC_NO_CONTENT = 204;
public static final int SC_RESET_CONTENT = 205;
public static final int SC_PARTIAL_CONTENT = 206;
public static final int SC_MULTIPLE_CHOICES = 300;
public static final int SC_MOVED_PERMANENTLY = 301;
public static final int SC_MOVED_TEMPORARILY = 302;
public static final int SC_FOUND = 302;
public static final int SC_SEE_OTHER = 303;
public static final int SC_NOT_MODIFIED = 304;
public static final int SC_USE_PROXY = 305;
public static final int SC_TEMPORARY_REDIRECT = 307;
public static final int SC_BAD_REQUEST = 400;
public static final int SC_UNAUTHORIZED = 401;
public static final int SC_PAYMENT_REQUIRED = 402;
public static final int SC_FORBIDDEN = 403;
public static final int SC_NOT_FOUND = 404;
public static final int SC_METHOD_NOT_ALLOWED = 405;
public static final int SC_NOT_ACCEPTABLE = 406;
public static final int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
public static final int SC_REQUEST_TIMEOUT = 408;
public static final int SC_CONFLICT = 409;
public static final int SC_GONE = 410;
public static final int SC_LENGTH_REQUIRED = 411;
public static final int SC_PRECONDITION_FAILED = 412;
public static final int SC_REQUEST_ENTITY_TOO_LARGE = 413;
public static final int SC_REQUEST_URI_TOO_LONG = 414;
public static final int SC_UNSUPPORTED_MEDIA_TYPE = 415;
public static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
public static final int SC_EXPECTATION_FAILED = 417;
public static final int SC_INTERNAL_SERVER_ERROR = 500;
public static final int SC_NOT_IMPLEMENTED = 501;
public static final int SC_BAD_GATEWAY = 502;
public static final int SC_SERVICE_UNAVAILABLE = 503;
public static final int SC_GATEWAY_TIMEOUT = 504;
public static final int SC_HTTP_VERSION_NOT_SUPPORTED = 505;
200:请求响应成功 3xx:请求重定向 重定向:你重新到我给你新位置去; 4xx:找不到资源 404 资源不存在 5xx:服务器代码错误 500 502: 网关错误2、常见应用
1.向浏览器输出消息
2.下载文件——
(1)要获取下载文件的 路径
(2)下载的文件名是什么?
(3)设置想办法让浏览器能支持下载我们需要的东西
(4)获取下载文件的输入流
(5)创建缓冲区
(6)获取OutputStream对象
(7)将FileOutputStream流写入到buffer缓冲区
(8)将OutputStream将缓冲区中的数据输出到客户端
- web.xml
在resources下添加一个图片1.png
package com.kuang.servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
public class FileServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//
// (1)要获取下载文件的 路径
// String realPath = this.getServletContext().getRealPath("/1.png");
String realPath = "D:\学习笔记\狂神说Java\javaweb-02-servlet\response\target\classes\1.png";
System.out.println("下载文件的路径:"+realPath);
// (2)下载的文件名是什么?
String fileName = realPath.substring(realPath.lastIndexOf("\")+1);
// (3)设置想办法让浏览器能支持(Content-Disposition)下载我们需要的东西.URLEncoder.encode(fileName,"UTF-8")):用来处理中文编码,否则可能会乱码
// resp.setHeader("Content-Disposition","attachment;filename="+ fileName);
resp.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(fileName,"UTF-8"));
// (4)获取下载文件的输入流;FileInputStream将文件变成流
FileInputStream in = new FileInputStream(realPath);
// (5)创建缓冲区
int len = 0;
byte[] buffer = new byte[1024];
// (6)获取OutputStream对象
ServletOutputStream out = resp.getOutputStream();
// (7)将FileOutputStream流写入到buffer缓冲区,将OutputStream将缓冲区中的数据输出到客户端
while((len=in.read(buffer))>0){
out.write(buffer,0,len);
}
//(8)关闭流
in.close();
out.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
修改web.xml
HttpServletRequest 参考页面fileDown com.kuang.servlet.FileServlet fileDown /down
web文件下载头信息
错误文件下载只显示页面,不提示文件下载
错误显示
原因:
正确显示:
正确代码:
package com.kuang.servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
public class FileServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//
// (1)要获取下载文件的 路径
// String realPath = this.getServletContext().getRealPath("/1.png");
String realPath = "D:\学习笔记\狂神说Java\javaweb-02-servlet\response\target\classes\1.png";
System.out.println("下载文件的路径:"+realPath);
// (2)下载的文件名是什么?
String fileName = realPath.substring(realPath.lastIndexOf("\")+1);
// (3)设置想办法让浏览器能支持(Content-Disposition)下载我们需要的东西.URLEncoder.encode(fileName,"UTF-8")):用来处理中文编码
// resp.setHeader("Content-Disposition","attachment;filename="+ fileName);
// resp.setHeader("Content-Disposition","attachment:filename="+ URLEncoder.encode(fileName,"UTF-8"));
resp.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(fileName,"UTF-8"));
// (4)获取下载文件的输入流;FileInputStream将文件变成流
FileInputStream in = new FileInputStream(realPath);
// (5)创建缓冲区
int len = 0;
byte[] buffer = new byte[1024];
// (6)获取OutputStream对象
ServletOutputStream out = resp.getOutputStream();
// (7)将FileOutputStream流写入到buffer缓冲区,将OutputStream将缓冲区中的数据输出到客户端
while((len=in.read(buffer))>0){
out.write(buffer,0,len);
}
//(8)关闭流
in.close();
out.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}



