标签:err ring iter size str stat 设值 map style
request与response
response:响应
作用: 往浏览器写东西
组成部分: 响应行 响应头 响应体
操作响应行
格式: 协议/版本 状态码 状态码说明
状态码:
1xx:已发送请求
2xx:已完成响应
200:正常响应
3xx:还需浏览器进一步操作
302:重定向 配合响应头:location
304:读缓存
4xx:用户操作错误
404:用户操作错误.
405:访问的方法不存在
5xx:服务器错误
500:内部异常
常用方法:
setStatus(int 状态码):针对于 1xx 2xx 3xx
了解 :sendError(int 状态码):针对于 4xx和5xx
操作响应头
格式: key:value(value可以是多个值)
常用的方法:
setHeader(String key,String value):设置字符串形式的响应头
了解:setIntHeader(String key,int value):设值整形的响应头
了解:setDateHeader(String key,long value):设值时间的响应头
addHeader(String key,String value):添加字符串形式的响应头 之前设置过则追加,若没有设置过则设置
了解:addIntHeader(String key,int value):添加整形的响应头
了解:addDateHeader(String key,long value):添加时间的响应头
常用的响应头:
location:重定向
refresh:定时刷新
content-type:设置文件的mime类型,设置响应流的编码及告诉浏览器用什么编码打开
content-disposition:文件下载
重定向:
方式1:
★response.sendRedirect("/day10/loc2");
方式2:
response.setStatus(302);
respooen.setHeader("location","/day10/loc2");
案例1:请求重定向
web.xml文件配置
<servlet>
<servlet-name>SHServlet1</servlet-name>
<servlet-class>com.hjh.request_response.SHServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SHServlet1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>SHServlet2</servlet-name>
<servlet-class>com.hjh.request_response.SHServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SHServlet2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
SHServlet1.java源码
package com.hjh.request_response;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SHServlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//重定向方式一
//1.设置状态码
response.setStatus(302);
//2.设置响应头
//response.setHeader("location", "/Servlet/servlet2");
//重定向方式二:设置响应头
response.sendRedirect("/Servlet/servlet2");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
SHServlet2.java源码:
package com.hjh.request_response;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SHServlet2 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("跳转到这里啦,哈哈哈");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
项目运行,在浏览器中输入“http://localhost:8080/Servlet/servlet1”,回车,url变为“http://localhost:8080/Servlet/servlet2”,浏览网页输出为:“跳转到这里啦,哈哈哈”
定时刷新:
方案1:设置头 refresh
respooen.setHeader("refresh","秒数;url=跳转的路径");
方案2:http的meta标签
<meta http-equiv="refresh" content="3;url=/day10/refresh2.html">
标签:err ring iter size str stat 设值 map style
原文地址:https://www.cnblogs.com/hejh/p/10968645.html