Java Web04:Response和Request
Web服务器接收到客户端的HTTP请求,针对这个请求,分别创建一个代表该请求的HttpServletRequest对象,和一个代表响应的HttpServletResponse对象
HttpServletResponse
给客户端响应一些信息
方法分类
向浏览器发送数据
ServletOutputStream getOutputStream() throws IOException;
PrintWriter getWriter() throws IOException;
向浏览器发送响应头
void setCharacterEncoding(String var1);
void setContentLength(int var1);
void setContentLengthLong(long var1);
void setContentType(String var1);
void setDateHeader(String var1, long var2);
void addDateHeader(String var1, long var2);
void setHeader(String var1, String var2);
void addHeader(String var1, String var2);
void setIntHeader(String var1, int var2);
void addIntHeader(String var1, int var2);
响应状态码
int SC_CONTINUE = 100;
int SC_SWITCHING_PROTOCOLS = 101;
int SC_OK = 200;
int SC_CREATED = 201;
int SC_ACCEPTED = 202;
int SC_NON_AUTHORITATIVE_INFORMATION = 203;
int SC_NO_CONTENT = 204;
int SC_RESET_CONTENT = 205;
int SC_PARTIAL_CONTENT = 206;
int SC_MULTIPLE_CHOICES = 300;
int SC_MOVED_PERMANENTLY = 301;
int SC_MOVED_TEMPORARILY = 302;
int SC_FOUND = 302;
int SC_SEE_OTHER = 303;
int SC_NOT_MODIFIED = 304;
int SC_USE_PROXY = 305;
int SC_TEMPORARY_REDIRECT = 307;
int SC_BAD_REQUEST = 400;
int SC_UNAUTHORIZED = 401;
int SC_PAYMENT_REQUIRED = 402;
int SC_FORBIDDEN = 403;
int SC_NOT_FOUND = 404;
int SC_METHOD_NOT_ALLOWED = 405;
int SC_NOT_ACCEPTABLE = 406;
int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
int SC_REQUEST_TIMEOUT = 408;
int SC_CONFLICT = 409;
int SC_GONE = 410;
int SC_LENGTH_REQUIRED = 411;
int SC_PRECONDITION_FAILED = 412;
int SC_REQUEST_ENTITY_TOO_LARGE = 413;
int SC_REQUEST_URI_TOO_LONG = 414;
int SC_UNSUPPORTED_MEDIA_TYPE = 415;
int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
int SC_EXPECTATION_FAILED = 417;
int SC_INTERNAL_SERVER_ERROR = 500;
int SC_NOT_IMPLEMENTED = 501;
int SC_BAD_GATEWAY = 502;
int SC_SERVICE_UNAVAILABLE = 503;
int SC_GATEWAY_TIMEOUT = 504;
int SC_HTTP_VERSION_NOT_SUPPORTED = 505;
下载文件
将文件放在WEB-INF目录下
import javax.servlet.ServletContext;
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 HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
res.setContentType("text/html;charset=utf-8");
ServletContext servletContext = this.getServletContext();
/**
* 获取下载文件在服务器的路径
*/
String realPath = servletContext.getRealPath("/WEB-INF/万叶.jpg");
System.out.println("下载路径为:" + realPath);
/**
* 获取下载文件名
*/
String filename = realPath.substring(realPath.lastIndexOf("\\") + 1);
/**
* 设置头,让浏览器支持下载
* 如果文件名为中文会乱码,手动转换一下
*/
res.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
/**
* 创建文件输入流
*/
FileInputStream file = new FileInputStream(realPath);
/**
* 创建缓冲区
*/
int len = 0;
byte[] buffer = new byte[1024];
/**
* 创建Response对象的字节输出流
* getWriter()对应字符输出流
*/
ServletOutputStream out = res.getOutputStream();
/**
* 将文件输出流写入到缓冲区,然后Response对象的字节输出流读取缓冲区,发送到浏览器(客户端)
*/
while ((len = file.read(buffer)) != -1){
out.write(buffer, 0, len);
}
out.close();
file.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
重定向(302)
请求转发时,URL不会发生变化
重定向时,URL会发生变化
sendRedirect()方法重定向
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Redirect extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
/**
* sendRedirect()方法重定向
* 其本质是封装了以下两步:
* resp.setHeader("Location", "./Hello");
* resp.setStatus(302);
*/
resp.sendRedirect("./Hello");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
JPS表单重定向
<%--设置浏览器页面编码格式--%>
<%@page contentType="text/html; charset=utf-8"%>
<html>
<body>
<h2>Hello World!</h2>
<form action="./Hello" method="get">
<p>用户名:<input type="text" name="username"></p>
<p>密码:<input type="password" name="password"></p>
<p><input type="checkbox" name="hobbies" value="Java">Java</p>
<p><input type="checkbox" name="hobbies" value="Python">Python</p>
<p><input type="checkbox" name="hobbies" value="PHP">PHP</p>
<p><input type="submit"></p>
</form>
</body>
</html>
HttpServletRequest
用户通过HTTP协议访问服务器,HTTP请求中的所有信息会被封装到HttpServletRequest对象中
通过该对象的方法,可以获取客户端请求过来的参数
获取前端传递的参数
getParameter()方法获取单个参数
getParameterValues()方法获取多个参数
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登陆成功</title>
</head>
<body>
<h1>登陆成功!</h1>
</body>
</html>
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.setContentType("text/html;charset=utf-8");
/**
* getParameter()方法获取request请求的单个参数
* getParameterValues()方法获取request请求的多个参数
*/
String username = req.getParameter("username");
String password = req.getParameter("password");
String[] hobbies = req.getParameterValues("hobbies");
System.out.println("用户名:" + username);
System.out.println("密码:" + password);
System.out.println(Arrays.toString(hobbies));
res.sendRedirect("./login_success.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
请求转发(307)
一般不会用之前的ServletContext对象进行转发,而是用Reques对象转发,方法一样
getRequestDispatcher()、forward()
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
res.setContentType("text/html;charset=utf-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
String[] hobbies = req.getParameterValues("hobbies");
System.out.println("用户名:" + username);
System.out.println("密码:" + password);
System.out.println(Arrays.toString(hobbies));
/**
* Request对象转发请求
*/
RequestDispatcher requestDispatcher = req.getRequestDispatcher("./login_success.jsp");
requestDispatcher.forward(req, res);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}