如上图所示:
浏览器打开默认的字符集跟当前操作系统的语言和环境有关系,我们现在用的windows操作系统默认的字符集编码表是GBK(gb2312)
服务器用的不是GBK,否则不会乱码。
字符输出流的编码是ISO的,该编码被GBK解码一定会乱码。
找到原因之后,解决这个乱码就会变得很简单:我们可以设置流的编码是GBK就可以了。
package cn.itcast.web.servlet;
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("/responseDemo4")
public class ResponseDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取流对象之前去设置流的默认编码:ISO-8859-1 设置为:GBK
response.setCharacterEncoding("GBK");
//1.获取字符输出流
PrintWriter pw = response.getWriter();
//2.输出数据
pw.write("你好,response");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
设置完流的默认编码后刷新一下服务器就发现,可以识别中文了:
但是仅仅是一种巧合,为什么?
因为我们明确的知道客户端用的是GBK才可以这样设置。将来有的浏览器就是utf-8,就意味着设置GBK不好使。
我们还需要告诉浏览器,服务器发送的消息体数据的编码,建议浏览器使用该编码解码。如何完成代码如下:
package cn.itcast.web.servlet;
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;
import java.nio.charset.Charset;
@WebServlet("/responseDemo4")
public class ResponseDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取流对象之前去设置流的默认编码:ISO-8859-1 设置为:GBK
//response.setCharacterEncoding("utf-8");
//告诉浏览器,服务器发送的消息体数据的编码。建议浏览器使用该编码解码
response.setHeader("content-type","text/html;Charset=utf-8");
//1.获取字符输出流
PrintWriter pw = response.getWriter();
//2.输出数据
pw.write("你好,response");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
但是这段代码写起来还是挺麻烦的,因为头信息是固定的,也就是说每一次都要写这行代码。所以response对象给我们提供的一个简单的形式来设置编码:
package cn.itcast.web.servlet;
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;
import java.nio.charset.Charset;
@WebServlet("/responseDemo4")
public class ResponseDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取流对象之前去设置流的默认编码:ISO-8859-1 设置为:GBK
response.setCharacterEncoding("utf-8");
//告诉浏览器,服务器发送的消息体数据的编码。建议浏览器使用该编码解码
//response.setHeader("content-type","text/html;Charset=utf-8");
//简单的形式,设置编码
response.setContentType("text/html;Charset=utf-8");
//1.获取字符输出流
PrintWriter pw = response.getWriter();
//2.输出数据
pw.write("你好啊啊啊啊,response");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
以后我们要记住,在获取字符输出流并且写中文数据之前,就要把这行代码写到最前面:
response.setContentType("text/html;Charset=utf-8");
案例3:输出字节数据
接下来说,输出字节数据,代码如下:
package cn.itcast.web.servlet;
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;
@WebServlet("/responseDemo5")
public class ResponseDemo5 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取字节输出流
ServletOutputStream sos = response.getOutputStream();
//2.输出数据
sos.write("hello".getBytes());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
hello就被访问到了:
当我们想输出中文,并且设置的是utf-8就会乱码:
//2.输出数据
sos.write("你好".getBytes("utf-8"));
解决方式:
response.setHeader("content-type","text/html;Charset=utf-8");
案例4:验证码
创建验证码步骤非常简单,我们直接代码演示:
package cn.itcast.web.servlet;
import javax.imageio.ImageIO;
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.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.Buffer;
import java.security.DigestOutputStream;
import java.util.Random;
@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
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.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 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
// 生成随机角标
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 = 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);
}
// 3.将图片输出到页面展示
ImageIO.write(image,"jpg",response.getOutputStream());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
代码效果如下,重新加载会呈现不同的验证码:
学习完了使用代码的方式来实现验证码,接下来,说一下真正将来在程序开发过程中如何使用验证码,当然不是在页面上显示一个验证码,是配合注册表单一起来显示验证码。(只能是一张图片)
接下来说一下验证码切换效果:
我们写一个HTML页面,代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
/*
分析:
点击超链接或者图片,需要换一张
1.给超链接和图片绑定单击事件
2.重新设置图片的src属性值
*/
window.onload = function () {
// 1.获取图片对象
let img = document.getElementById("checkCode");
// 2.绑定单击事件
img.onclick = function () {
// 加时间戳
var date = new Date().getTime();
img.src = "/day15/checkCodeServlet?" date;
}
}
</script>
</head>
<body>
<img id="checkCode" src="/day15/checkCodeServlet" alt="">
<a id="change" href="">看不清换一张?</a>
</body>
</html>
最后
感谢你看到这里,文章有什么不足还请指正,觉得文章对你有帮助的话记得给我点个赞,每天都会分享java相关技术文章或行业资讯,欢迎大家关注和转发文章!