问题描述:
新增一个学生信息,添加成功则向jsp输出一段js代码,弹窗添加成功或失败。
结果,弹窗结果乱码,然后我也 设置了编码格式,也没得到解决,经过多方思考,得到解决方案!
解决:
在获取输出流之前 ,设置响应的编码格式
原代码:
int i = service.insertStu(student);
PrintWriter writer = resp.getWriter();
resp.setContentType("text/html;charset=UTF-8");
if(i>0){
writer.write("<script>alert('新增成功');location.href='/Educational/student/getStudentList'</script>");
}else{
writer.write("<script>alert('新增失败');location.href='/Educational/student/getGradeList'</script>");
}
修改之后:
int i = service.insertStu(student);
resp.setContentType("text/html;charset=UTF-8");
PrintWriter writer = resp.getWriter();
if(i>0){
writer.write("<script>alert('新增成功');location.href='/Educational/student/getStudentList'</script>");
}else{
writer.write("<script>alert('新增失败');location.href='/Educational/student/getGradeList'</script>");
}