网站制作宣传,化妆品网站建设的维护,做亚马逊运营要看哪些网站,百度一下16881.完成重定向
重定向代码实现
1.重定向资源跳转的方法
//1.设置状态码为302
response.setStatus(302);
//2.设置响应头location
response.setHeader(location,/xxx/aaa);
2.简单的方法
response.sendRedirect(/xxx/aaa);重定向特点数据(…1.完成重定向
重定向代码实现
1.重定向资源跳转的方法
//1.设置状态码为302
response.setStatus(302);
//2.设置响应头location
response.setHeader(location,/xxx/aaa);
2.简单的方法
response.sendRedirect(/xxx/aaa);重定向特点数据(redirect)
1.浏览器地址栏发生变化
2.重定向可以访问其他站点的资源
3.重定向是两次请求,不能使用request对象来共享数据转发的特点(forward)
1.浏览器地址不发生变化
2.转发只能是当前服务器下的资源
3.转发请求只有一次,可以通过request对象共享路径写法
1.路径分类1.相对路径: 通过相对路径不可以确定唯一资源不以/开头如: ./index.html2.绝对路径: 通过绝对路径可以确定唯一资源以/开头如:http://localhost/xyz/xxx /xyz/xxx2.服务器输出字符数据到浏览器
经典方式
//0.设置字符集(防止乱码)
//response.setCharacterEncoding(utf-8);
//1.建议浏览器使用对应编码
response.setHeader(content-type,text/html;charsetutf-8);
//2.获取字符输出流
PrintWriter pw response.getWriter();
//3.输出数据到浏览器
pw.write(你好 World!);简单方式
//1.设置编码
response.setContentType(text/html;charsetutf-8);
//2.获取字符输出流
PrintWriter pw response.getWriter();
//3.输出数据到浏览器
pw.write(你好 World!);3.服务器输出字节数据到浏览器
//0.设置编码
response.setContentType(text/html;charsetutf-8);
//1.获取字节流数据
ServletOutputStream sos response.getOutputStream();
//2.输出数据
sos.write(hello-你好.getBytes());4.验证码
int width 100;
int height 50;
//1.创建一个图片对象
BufferedImage image new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//2.美化图片(绘制)
//2.1填充背景色
Graphics g image.getGraphics();//画笔
g.setCollor(Color.PINK);
g.fillRect(0,0,width,height);
//2.2绘制边框
g.setCollor(Color.BLUE);
g.drawRect(0,0,width-1,height-1);
//2.3写验证码
String strABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghikjlmnopqrstuvwxyz._012345678;
Random ran new Random();
for(int i0;i4;i){int index ran.nextInt(str.length());char ch str.charAt(index);g.drawString(ch,width/5*i,height/2);}
//2.4绘制干扰线
for(int i0;i5;i){g.setColor(Color.Green);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);
}//3.将图片传输给浏览器
ImageIO.write(image,jpg,response.getOutputStream());