周海媚拒绝求婚,qq水浒张清,神精榜天上人间
功能:设置响应消息
案例
完成重定向
重定向:资源跳转的方式
代码实现
//方式一 //设置状态码 resp.setstatus(302); //设置响应头location resp.setheader("location","/demo2"); //方式二 resp.sendredirect("/demo2");
重定向(redirect)的特点
重定向可以访问其他站点(服务器)的资源
重定向是两次请求,不可以使用request对象来共享数据
转发(forward)的特点
路径写法
给客户端浏览器使用:需要加虚拟目录(项目的访问路径)
建议虚拟目录动态获取:request.getcontextpath()
a标签和form标签 重定向。。。
给服务器使用:不需要加虚拟目录
服务器输出字符数据到浏览器
步骤
获取字符输出流
printwriter pw = respond.getwriter();
输出数据
pw.writer(string s);
乱码问题
服务器输出字节数据到浏览器
步骤
获取字节输出流
servletoutputstream sos = response.getoutputstream();
输出数据
sos.writer("你好".getbytes("utf-8"));
验证码
目的:防止恶意表单注册
代码
@webservlet("/demo3") public class checkcodeservlet extends httpservlet { @override protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { this.dopost(req,resp); } @override protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { int width = 100; int height = 50; //1.创建一对象,在内存中创建图片(验证码图片对象) bufferedimage image = new bufferedimage(width,height,bufferedimage.type_3byte_bgr); // 2.美化图片 // 2.1填充背景色 graphics g = image.getgraphics();//画笔对象 g.setcolor(color.pink);//设置画笔颜色 g.fillrect(0,0,width,height); // 2.2画边框 g.setcolor(color.blue);//设置画笔颜色 g.drawrect(0,0,width-1,height-1); string str = "abcdefghijklnmopqrstuvwxyzabcdefghijklnmopqrst1234567890"; // 生成随机角标 random ran = new random(); for (int i = 1; i <= 4; i++) { int index = ran.nextint(str.length()); // 获取字符 char ch = str.charat(index); // 2.3写验证码 g.drawstring(ch+"",width/5*i,height/2); } // 2.4画干扰线 g.setcolor(color.green); // 随机生成干扰线 for (int i = 1; i <= 10; i++) { int x1 = ran.nextint(width); int x2 = ran.nextint(width); int y3 = ran.nextint(height); int y4 = ran.nextint(height); g.drawline(x1,y3,x2,y4); } // 将图片输出到页面展示 imageio.write(image,"jpg",resp.getoutputstream()); } }
servletcontext对象
概念:代表整个web应用,可以和程序的容器(服务器)通信
获取
通过request对象获取
request.getservletcontext();
通过httpservlet获取
this.getservletcontext();
功能
获取mime类型
域对象:共享数据
获取文件的真实(服务器)路径
代码
public class servletcontextdemo1 extends httpservlet { @override protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { // 1.通过request对象获取 servletcontext context1 = req.getservletcontext(); // 2.通过httpservlet对象获取 servletcontext context2 = this.getservletcontext(); system.out.println(context1); system.out.println(context2); system.out.println(context1==context2); //获取mime类型 string filename = "haha.jpg"; string mine = context1.getmimetype(filename); system.out.println(mine); //获取文件真实路径 string realpath1 = context1.getrealpath("/a.txt");//web目录下的 system.out.println(realpath1); string realpath2 = context1.getrealpath("/web-inf/b.txt");//web-inf目录下的 system.out.println(realpath2); string realpath3 = context1.getrealpath("/web-inf/classes/c.txt");//src目录下的 system.out.println(realpath3); } @override protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { this.dopost(req,resp); } }
案例:文件下载
文件下载需求
分析
步骤
问题
代码
package com.creat.test; import com.creat.utils.downloadutils; import javax.servlet.servletcontext; 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.fileinputstream; import java.io.ioexception; import java.io.inputstream; import java.net.urldecoder; @webservlet("/downloadservlet") public class downloadservlet extends httpservlet { protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string filename = request.getparameter("filename");//获取请求下载的文件名 string decodefilename = urldecoder.decode(filename, "utf-8"); //java中相同编解码规则对应的类为urlencode和urldecode //这里使用urldecoder类静态方法decode对文件名解码(请求路径中中文已被编码) servletcontext servletcontext = getservletcontext();//获取服务器域对象 string mimetype = servletcontext.getmimetype(decodefilename);//获取传输文件类型(一般文件在传输时有对应的类型,web.xml配置文件中列举了所有对应关系) response.setheader("content-type",mimetype);//设置传输文件类型 string agent = request.getheader("user-agent");//获取请求的浏览器类型 string encodefilename = downloadutils.getfilename(agent, decodefilename);//下载弹窗中文文件名会乱码,这里使用特殊方式编码 response.setheader("content-disposition","attachment;filename="+encodefilename);//设置客户端浏览器对文件以附件处理 string realpath = servletcontext.getrealpath("/"+decodefilename);//域对象获取文件真实路径 fileinputstream inputstream=new fileinputstream(realpath);//创建输入流,加载要下载的文件进内存 servletoutputstream outputstream = response.getoutputstream();//创建输出流,写文件给客户端浏览器 byte[]bys=new byte[1024*4]; int len; while ((len=inputstream.read(bys))!=-1){ outputstream.write(bys,0,len); } inputstream.close(); } protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { this.dopost(request,response); } }
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
浅析我对 String、StringBuilder、StringBuffer 的理解
使用IDEA搭建SSM框架的详细教程(spring + springMVC +MyBatis)
Springboot整合freemarker 404问题解决方案
引入mybatis-plus报 Invalid bound statement错误问题的解决方法
网友评论