笔记-JavaWeb学习之旅11
请求转发:一种在服务器内部的资源跳转方式
使用步骤
1.通过request对象获取请求转发器对象:requestdispatcher getrequestdispatcher(string path)参数是转发资源的路径
2.使用requestispatcher对象进行转发
特点:浏览器地址栏路径不会发生变化,只能转发当前服务器内部资源,转发是一次请求
共享数据
域对象:一个有作用范围的对象,可以在范围内共享数据
request域:代表一次请求的范围,一般用于请求转发的多个资源*享数据
方法:
- void setattribute(string name,object obj):存储数据
- object getattiude(stirng name):通过键获取值
- void removeattribute(string name):通过键移除键值对
package com.data.httpservlet; import javax.servlet.requestdispatcher; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import java.io.ioexception; @webservlet("/requestdemo1") public class requestdemo1 extends httpservlet { protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { } protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { //使用request对象的方法setattribute将数据存储到request域中 request.setattribute("msg","hello"); system.out.println("requestdemo1被访问了。。。。"); //转发到requestdemo2 requestdispatcher requestdispatcher = request.getrequestdispatcher("/requestdemo2"); requestdispatcher.forward(request,response); } }
package com.data.httpservlet; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import java.io.ioexception; @webservlet("/requestdemo2") public class requestdemo2 extends httpservlet { protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { } protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { //访问共享数据,通过键获取值 system.out.println(request.getattribute("msg")); system.out.println("requestdemo2被访问了。。。"); } }
requestdemo1被访问了。。。。 hello requestdemo2被访问了。。。
获取servletcontext:
servletcontext getservletcontext()
http响应消息
服务器端发送给客户端的数据
五种数据格式
1.响应行
1:组成:协议/版本 响应状态吗 状态码描述
2.响应状态码:服务器告诉客户端浏览器本次请求和响应的一个状态,状态码都是3位数字有5种状态码
- 1xx :服务器接收客户端消息,但没有接收完成,等待一段时间后,发送1xx多状态码
- 2xx :成功 。代表数字:200
- 3xx :重定向。代表:302(重定向),304(访问缓存)
- 4xx :客户端错误。代表(404)请求路径没有对应的资源,405(请求方式没有对应的doxxx方法)
- 5xx : 服务端错误。代表:500(服务器内部出现异常,比如代码有错)
2.响应头
格式:头名称 :值
常见的响应头
content-type:服务器告诉客户端本次响应体数据格式以继编码格式
content-dispostition:服务器告诉客户端以什么格式打开响应体数据
3.响应空行
4响应体:传输的数据
重定向:资源跳转方式
package com.data; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import java.io.ioexception; @webservlet("/demo1") public class responseservlet extends httpservlet { protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { system.out.println("demo1...."); //完成重定向 //设置状态码为302 //response.setstatus(302); //设置响应头,头的名称,头的值 //response.setheader("location","/demo2"); //重定向可以访问其他站点资源 //response.sendredirect("https://www.baidu.com");//访问了百度 //重定向 response.sendredirect("/demo2"); //设置共享数据 request.setattribute("msg","hello"); } }
package com.data; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import java.io.ioexception; @webservlet("/demo2") public class responseservlet2 extends httpservlet { protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { //重定向可以访问其他站点的资源 system.out.println("demo2....."); //重定向不能访问共享数据 object o1 = request.getattribute("msg"); system.out.println(o1); } }
输出结果 demo1.... demo2..... null
重定向的特点:redirect
- 地址栏发生变化
- 重定向可以访问其它站点服务器的资源
- 重定向是两次请求,,不拿使用request对象来共享数据
路径写法
相对路径:通过相对路径不可以确定唯一资源
绝对路径: 通过绝对路径可以确定唯一资源,给客户端浏览器使用需要加虚拟目录,给服务器使用,不需要加虚拟目录
服务器输出字符数据到浏览器
package com.data; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import java.io.ioexception; import java.io.printwriter; @webservlet("/demo3") public class responseservlet3 extends httpservlet { protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { //使用响应头,告诉浏览器我使用的编码 //浏览器收到响应,就会按照指定字符集解码 response.setcontenttype("text/html;charset=utf-8"); //response.setheader("content-type","text/html;charset=utf-8"); //向客户端输出数据,先要获取字符输出流 printwriter writer = response.getwriter(); writer.write("<h1>你好,world</h1>"); } }
服务器输出字节数据到浏览器
package com.data; import javax.servlet.servletexception; import javax.servlet.servletoutputstream; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import java.io.ioexception; import java.io.printwriter; @webservlet("/demo3") public class responseservlet3 extends httpservlet { protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html;charset=utf-8"); servletoutputstream outputstream = response.getoutputstream(); outputstream.write("你好,客户端".getbytes("utf-8")); } }
验证码
package com.data; import javax.imageio.imageio; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import java.awt.*; import java.awt.image.bufferedimage; import java.io.ioexception; import java.util.random; @webservlet("/demo4") public class checkcodeservlet extends httpservlet { @override protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { int width = 100; int height = 50; //创建对象,该对象可以在内存中生成图片 bufferedimage image = new bufferedimage(width, height, bufferedimage.type_int_rgb); //美化图片 //填充背景色 graphics g = image.getgraphics(); g.setcolor(color.pink); g.fillrect(0,0,width,height); //画边框 g.setcolor(color.blue); g.drawrect(0,0,width-1,height-1); string str="abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789"; // 写验证码 random ran = new random(); for(int i = 1;i<=4;i++){ int index = ran.nextint(str.length()); char ch = str.charat(index); g.drawstring(ch+"",width/5*i,height/2); } g.setcolor(color.green); for (int i = 0 ; i< 10 ;i++){ int x1 = ran.nextint(width); int x2 = ran.nextint(width); int y1 = ran.nextint(height); int y2 = ran.nextint(height); g.drawline(x1,y1,x2,y2); } //将图片输出到页面上 imageio.write(image,"jpg",resp.getoutputstream()); } @override protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { } }
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>title</title> <script> // 分析: // 点击超链接或者图片,需要换一张, // 给图片和超链接绑定单击事件 window.onload=function () { //获取图片对象 var img=document.getelementbyid("change"); //绑定单击事件 img.onclick=function () { var date =new date().gettime(); img.src="/day14/demo4"+date; } } </script> </head> <body> <img id="checkcode" src="/day14/demo4" /> <a id="change" href="">看不清?换一张</a> </body> </html>
servletcontext对象:
1.概念:代表整个web应用,可以和程序的容器来通信
获取方式:
1.request.getservletcontext();
2.this.getservletcontext();
功能:获取mime类型(在互联网通信过程种定义的一种文件数据类型,不如text/html,image/jpeg)
package com.data; import javax.servlet.servletcontext; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import java.io.ioexception; @webservlet("/demo5") public class servletdemo5 extends httpservlet { protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { //获取mime类型 servletcontext context=this.getservletcontext(); //定义文件名称 string filename = "a.jpg"; //获取mime类型 string mimetype = context.getmimetype(filename); system.out.println(mimetype); } } //image/jpeg
功能:域对象,共享数据
域对象的方法
setarrtibute(string name,object value)
getattribute(string name)
removeattribute(string name)
功能:获取文件服务器路径
文件在项目目录下,可以直接文件名获取路径 在web-inf目录下,需要加上web-inf才可以获取 src目录下的所有文件将来都存放在/web-inf/classes目录下
package com.data; import javax.servlet.servletcontext; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import java.io.ioexception; @webservlet("/demo5") public class servletdemo5 extends httpservlet { protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { //获取web目录下的资源访问 servletcontext context = this.getservletcontext(); string realpath = context.getrealpath("/b.txt");//b.txt在项目目录下,可以直接文件名获取路径 system.out.println(realpath); //获取web-inf目录下的资源访问 string realpath1 = context.getrealpath("/web-inf/c.txt");//c.txt在web-inf目录下,需要加上web-inf才可以获取到路径 system.out.println(realpath1); //获取src目录下的资源能路径 string realpath2 = context.getrealpath("/web-inf/classes/a.txt");//src目录下的所有文件将来都存放在/web-inf/classes目录下 system.out.println(realpath2); } }