四、HttpServletResponse接口
1、介绍
(1)该接口来自Servlet规范中,存在于Tomcat中的servlet-api.jar包中
(2)该接口的实现类是由Http服务器负责提供
(3)该接口负责将doGet或者doPost方法执行的结果写入到响应体中并交给浏览器
(4)该接口修饰的对象常被称为响应对象
2、主要功能
(1)将执行结果以二进制的形式写入到响应体中
(2)设置响应头中的content-type属性,从而控制浏览器使用对应编译器将二进制数据编译为其他格式,如文字、图片、视频,命令等
(3)设置响应头中的location属性,将一个请求地址赋值给location,从而控制浏览器向指定浏览器发送请求
public class oneServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String result="Hello World";
response.setContentType("text/html;charset=utf-8");
response.sendRedirect("https://www.baidu.com"); //将location属性设置为百度网址
//1、向Http服务器索要输出流
PrintWriter pw =response.getWriter();
//2、写入请求结果,最后不需要关闭输出流,因为不是自己创建的
pw.write(result); //这个方法可以把字符、字符串、ASCII码写入到响应体中
pw.print(3); //常用这个方法代替write方法把真实数据写入响应体中
pw.print("html<br/>css<br/>javascript");
pw.print("中文乱码");
pw.print("没有办法");
}
}
五、HttpServletRequest接口
1、介绍
与HttpServletResponse接口不同地方在于
(1)该接口负责在doGet或doPost方法运行时读取Http请求协议包中的信息
(2)该接口修饰的对象称为请求对象
2、主要功能
(1)读取Http请求协议包中的请求行的信息
(2)读取Http请求协议包中的请求头或者请求体中的请求参数信息
(3)可以代替浏览器向Http服务器申请调用资源文件
public class OneServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String url=request.getRequestURL().toString();
String method=request.getMethod();
//URI:资源文件精准定位地址,在请求行中并没有这个属性,其本质是截取URL中一个字符串,格式为“/网站/资源文件名”,
//目的为让HTTP服务器对资源文件进行定位
String uri=request.getRequestURI();
System.out.println("url:"+url+",method:"+method+",URI:"+uri);
}
}
/**
* @author : MrLiu22
* @date : 2021/1/5,19:37
* @description :在访问TwoServlet的后面添加参数,可以通过HttpServletRequest获取到参数名和参数值
*/
public class TwoServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取到请求体中的参数信息返回到控制台
Enumeration parameterNames =request.getParameterNames();
while(parameterNames.hasMoreElements()){
String paraName=(String) parameterNames.nextElement();
String value=request.getParameter(paraName);
System.out.println("参数名为:"+paraName+",参数值为:"+value);
}
}
}
/**
* @author : MrLiu22
* @date : 2021/1/5,19:59
* @description : 将输入框中的内容以get或post的方法发出请求,并获取到请求头中的参数信息
*/
public class ThreeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String userName=request.getParameter("username");
System.out.println("userName:"+userName);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//通过请求对象,获取请求头信息
request.setCharacterEncoding("utf-8");
String userName=request.getParameter("username");
System.out.println("userName:"+userName);
}
}
六、请求对象和响应对象的生命周期
1、在Http服务器在接收到浏览器发送的Http请求协议包之后,自动为当前的Http请求协议包生成一个请求对象和响应对象
2、在Http服务器调用doGet或者doPost方法时,会将请求对象和响应对象作为实参传给该方法,确保该方法可以正确执行
3、在Http服务器向浏览器推送Http响应协议包之前,会将本次请求相关联的请求对象和响应对象销毁掉
总结:请求对象和响应对象的生命周期贯穿一次请求的处理过程中,而请求对象和响应对象相当于用户在服务端的代理人