Response对象
设置状态码
void setStatus(int sc)
设置状态码
设置响应头(key:value)
一个key对应一个value或一个key对应多个value
-
void setHeader(String name, String value)
(一个key对应一个value)经常使用
setHeader(“aa”,”bb”);
setHeader(“aa”,”cc”);
结果:aa:cc -
void addHeader(String name, String value)
(一个key对应多个value)
addHeader(“aa”,”bb”);
addHeader(“aa”,”cc”);
结果:aa:bb,cc
设置响应体
-
ServletOutputStream getOutputStream()
字节输出流 -
PrintWriter getWriter()
字符输出流
重定向
- 登录页面重定向
- 状态码302,响应头
loaction
Api
方法一:
// 设置302的状态码 response.setStatus(302);
// 设置地址 response.setHeader("location", "/day10/response/login.html");
方法二:
// response对象提供了一个方法,可以完成重定向。 response.sendRedirect("/day10/response/login.html");
案例
登陆页面,用户名和密码的登陆页面,用户名和密码都是admin,
如果有一个不是,重定向到登陆页面,重新登陆。
login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/Day21/login" method="post">
<table border="1" width="50%">
<tr>
<td>输入姓名</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>输入密码</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交"> </td>
</tr>
</table>
</form>
</body>
</html>
LoginServlet
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if("admin".equals(username)&&"admin".equals(password)){
response.getWriter().write("success");
}else{
方法一
// //设置状态码
// response.setStatus(302);
设置地址
// response.setHeader("location", "/Day21/response/login.html");
// 方法二
response.sendRedirect("/Day21/response/login.html");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
页面定时刷新(页面读秒操作)
响应头
refresh
案例
public class RefreshServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write("<h1>页面将在5秒后跳转</h1>");
//通过refresh头完成刷新页面
response.setHeader("refresh", "5;url=/Day21/response/login.html");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
上面有一个问题是秒数不能动态改变,我们可以换另外一个方法实现,在Html中实现
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- 设置请求头 -->
<meta http-equiv="refresh" content="5;url=/Day21/response/login.html">
<title>Insert title here</title>
</head>
<body onload="run()">
<h2>
页面将在<span id="spanId">5</span>秒后跳转
</h2>
</body>
<script type="text/javascript">
/*
页面一加载执行onload()
*/
var x=5;
function run(){
var span=document.getElementById("spanId");
span.innerHTML=x;
x--;
window.setTimeout("run()", 1000);
}
</script>
</html>
禁用浏览器的缓存(三个头信息)
一般应用在:网银系统
Cache-Control : no-cache
Expires: -1 值是日期类型(setDateHeader())
Pragma : no-cache
案例
public class CEPServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
// 这里是设置dateheader
response.setDateHeader("Expires", -1);
Date date=new Date();
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = simpleDateFormat.format(date);
response.getWriter().write(str);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
向页面输出中文(乱码问题)
字节流
/**
* 字节输出中文
* * 如果不设置头的话,不一定会乱码,看浏览器配置
* * 解决办法
* 1.设置浏览器打开文件时所采用的编码
* response.setHeader("Content-Type", "text/html;charset=UTF-8");
* 2.获取字符串byte数组时编码和打开文件时编码一致。
* "乱码测试".getBytes("UTF-8")
* @throws IOException
*/
public void run1(HttpServletResponse response) throws IOException{
// 设置浏览器打开文件时编码
response.setHeader("Content-Type", "text/html;charset=UTF-8");
// 获取字节输出流
OutputStream os = response.getOutputStream();
// 输出中文
os.write("乱码测试".getBytes("UTF-8"));
}
字符流
/**
* 字符输出中文
* * 肯定乱码
* response缓冲区的编码,默认值ISO-8859-1
* 方法一:
* 1. 设置response缓冲编码
* response.setCharacterEncoding("UTF-8");
* 2。 设置浏览器打开文件所采用的编码
* response.setHeader("Content-Type", "text/html;charset=UTF-8");
* 方法二:
* 简写方式
* response.setContentType("text/html;charset=UTF-8");
*
* @param response
* @throws IOException
*/
public void run2(HttpServletResponse response) throws IOException{
// 设置response缓冲区的编码
//response.setCharacterEncoding("UTF-8");
// 设置浏览器打开文件所采用的编码
//response.setHeader("Content-Type", "text/html;charset=UTF-8");
// 简写的形式(等于上面两句)
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write("哈罗卧得");
}