JavaWeb入门:HttpResponse和HttpRequest详解
httpresponse 讲解
httpservletresponse概述:
在创建servlet时会覆盖service()方法,或doget()/dopost(),这些方法都有两个参数,一个为代表请求的request和代表响应response。service方法中的response的类型是servletresponse,而doget/dopost方法的response的类型是httpservletresponse,httpservletresponse是servletresponse的子接口,功能和方法更加强大。
response运行流程
响应头有很多这里只介绍常用的。
在浏览器可以按f12 抓包看响应头、请求头、具体的可以再查。
设置响应行
设置响应的状态码
/** * @author: crush * @date: 2021-05-09 19:35 * version 1.0 */ @webservlet("/test3") public class httpresponsetest3 extends httpservlet { @override protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { system.out.println("设置状态码,前台通过判断状态码,来判断请求是否成功"); resp.setstatus(404); } @override protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { doget(req, resp); } }
测试
设置响应头
刷新 跳转页面
/** * @author: crush * @date: 2021-05-09 19:35 * version 1.0 */ @webservlet("/test4") public class httpresponsetest4 extends httpservlet { @override protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { // 设置响应头 每1秒自动刷新 system.out.println("设置响应头 每1秒自动刷新"); resp.setheader("refresh", "1"); //定时跳转 3秒后将自动跳转 // resp.setheader("refresh","3;url=hello.jsp"); } @override protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { doget(req, resp); } }
测试
一个小demo 实现刷新 页面累加
/** * response * @author adimi */ @webservlet("/test4") public class responsetest4 extends httpservlet { private static integer id=1; @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setheader("refresh","1"); printwriter writer = response.getwriter(); id++; writer.print("id==>"+id); } @override protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { doget(req, resp); } }
设置字符编码 解决中文乱码问题
/** * @author: crush * @date: 2021-05-09 19:35 * version 1.0 */ @webservlet("/test1") public class httpresponsetest1 extends httpservlet { @override protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { // 解决中文乱码问题 // 设置响应头 // 设置字符编码 resp.setcharacterencoding("utf-8"); // 设置响应内容以什么格式展示到页面 什么编码格式 包含了设置字符编码 resp.setcontenttype("text/html;charset=utf-8"); printwriter writer = resp.getwriter(); writer.print("中国,你好!!!"); } @override protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { doget(req, resp); } }
测试
重定向 redirect
/** * 重定向 * @author: crush * @date: 2021-05-09 19:35 * version 1.0 */ @webservlet("/test5") public class httpresponsetest5 extends httpservlet { @override protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { resp.sendredirect("hello.jsp"); // resp.setheader("location","www.baidu.com"); 通过设置响应头转发 } @override protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { doget(req, resp); } }
request转发
/** * 重定向 * @author: crush * @date: 2021-05-09 19:35 * version 1.0 */ @webservlet("/test5") public class httpresponsetest5 extends httpservlet { @override protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { // resp.sendredirect("hello.jsp"); req.getrequestdispatcher("hello.jsp").forward(req,resp); } @override protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { doget(req, resp); } }
重定向和转发的区别
文件下载
获取路径资源
string path=this.getservletcontext().getrealpath("web-inf\\classes\\8.jpg");
读取资源
fileinputstream fileinputstream=new fileinputstream(path);
获取到文件名,路径在电脑上保存的形式是 \ \
string filename=path.substring(path.lastindexof("\\")+1);
设置消息头告诉浏览器,我要下载1.png这个图片 设置编码
resp.setheader("content-disposition", "attachment; filename=" + urlencoder.encode(filename, "utf-8"));
把读取到的内容回送给浏览器
int len=0; byte[] bytes=new byte[1024]; // servletoutputstream 提供用于将二进制数据发送到客户端的输出流 servletoutputstream servletoutputstream=resp.getoutputstream(); while((len=fileinputstream.read(bytes))>0) { servletoutputstream.write(bytes,0,len); }
关闭资源
servletoutputstream.close(); fileinputstream.close();
注:8.jpg 放在我的resources 文件夹下 但是这里需要写的是编译完8.jpg存放的位置
具体代码
/** * @author: crush * @date: 2021-05-09 19:40 * version 1.0 */ @webservlet("/down") public class responsedownfile extends httpservlet { @override protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { //获取路径资源 string path=this.getservletcontext().getrealpath("web-inf\\classes\\8.jpg"); //读取资源 fileinputstream fileinputstream=new fileinputstream(path); //获取到文件名,路径在电脑上保存的形式是\\ string filename=path.substring(path.lastindexof("\\")+1); //设置消息头告诉浏览器,我要下载1.png这个图片 // 该方式文件名为中文时会乱码 //防止中文乱码 resp.setheader("content-disposition", "attachment; filename=" + urlencoder.encode(filename, "utf-8")); //把读取到的内容回送给浏览器 int len=0; byte[] bytes=new byte[1024]; servletoutputstream servletoutputstream=resp.getoutputstream(); while((len=fileinputstream.read(bytes))>0) { servletoutputstream.write(bytes,0,len); } // 关闭资源 servletoutputstream.close(); fileinputstream.close(); } @override protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { doget(req, resp); } }
httprequest 讲解
httpservletrequest概述
我们在创建servlet时会覆盖service()方法,或doget()/dopost(),这些方法都有两个参数,一个为代表请求的request和代表响应response。service()方法中写了根据请求方式的不同调用doget()和dopost().
service方法中的request的类型是servletrequest,而doget/dopost方法的request类型httpservletrequest,httpservletrequest是servletrequest的子接口,功能和方法更加强大.
request 运行流程
获取请求携带的参数
/** * @author: crush * @date: 2021-05-11 16:52 * version 1.0 */ @webservlet("/request1") public class requesttest1 extends httpservlet { @override protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { // 这里是请求的参数名 必须是同名的 string username = req.getparameter("username"); string password = req.getparameter("password"); printwriter writer = resp.getwriter(); writer.print("<h1>"+username+":"+password+"</h1>"); } @override protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { doget(req, resp); } }
获取多个参数的值
@override protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { // 设置请求的编码 不然会乱码 req.setcharacterencoding("utf-8"); enumeration<string> names = req.getparameternames(); while (names.hasmoreelements()) { string name = (string) names.nextelement(); string[] values = req.getparametervalues(name); system.out.println(name+":"+ arrays.tostring(values)); } }
获得请求行的信息
/** * @author: crush * @date: 2021-05-11 16:52 * version 1.0 */ @webservlet("/request3") public class requesttest3 extends httpservlet { @override protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { // 获得请求的url stringbuffer requesturl = req.getrequesturl(); system.out.println("请求的url===>"+requesturl); // 获得请求的servlet的路径 string path = req.getservletpath(); system.out.println("请求的servlet的路径===>"+path); //返回发出此请求的http方法的名称,例如get,post或put string method = req.getmethod(); system.out.println("返回发出此请求的http方法的名称==>"+method); //返回发送请求的客户端或最后一个代理的internet协议(ip)地址 string remoteaddr = req.getremoteaddr(); system.out.println("remoteaddr==>"+remoteaddr); } @override protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { doget(req, resp); } }
request实现转发
/** * @author: crush * @date: 2021-05-11 16:52 * version 1.0 */ @webservlet("/request2") public class requesttest2 extends httpservlet { @override protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { // 访问request2 转发到 request4去 req.getrequestdispatcher("/request4").forward(req,resp); } @override protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { doget(req, resp); } }
request是一个域对象
request对象也是一个存储数据的区域对象,所以也具有如下方法:
setattribute(string name, object o)
getattribute(string name)
removeattribute(string name)
servletcontext 作用域:
创建:启动web应用程序的时候创建
销毁:关闭web应用程序的时候销毁
域的作用范围:整个web应用的启动周期
request作用域:
创建:访问时创建request
销毁:响应结束request销毁
域的作用范围:一次请求中
总结
本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注的更多内容!