out.print()输出乱码的问题
作者:互联网
PrintWriter out = response.getWriter();
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
String contextPath = request.getContextPath();
// 确定提交的参数
RainManageIService rainManageIService = new RainManageIServiceImpl();
String name = request.getParameter("a");
String date1 = request.getParameter("date");
// 实现字符串和日期类型的转换
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(date1);
String rainString = request.getParameter("b");
int rains = Integer.parseInt(rainString);
String address = request.getParameter("c");
String mobile = request.getParameter("d");
RainManage rain = new RainManage();
rain.setDistrictname(name);
rain.setMonitorTime(date);
rain.setRain(rains);
rain.setMonitoringStation(address);
rain.setMonitoringAddress(mobile);
try {
int result = rainManageIService.insert(rain);
if (result == -1) {
out.print("<script type=\"text/javascript\">");
out.print("alert(\"添加没有成功\");");
out.print("</script>");
} else if (result == 0) {
out.print("<script type=\"text/javascript\">");
out.print("alert(\"未找到相关信息\");");
out.print("</script>");
} else {
out.print("<script type=\"text/javascript\">");
out.print("alert(\"已经成功添加信息,点击确认返回首页\");");
out.print("location.href=\"" + contextPath
+ "/ListServlet?opr=list\";");
out.print("</script>");
}
} catch (Exception e) {
e.printStackTrace();
}
明明都已经设置了
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
可是为什么在浏览器上输出的还是乱码呢?经过一番百度之后才发现原来是PrintWriter out = response.getWriter();的问题,这是输出流,电脑读取代码的方式是从上到下,从左到右的,所以应该把设置的utf-8放在PrintWriter out = response.getWriter();上面。否则无效!
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
String contextPath = request.getContextPath();
PrintWriter out = response.getWriter();
// 确定提交的参数
RainManageIService rainManageIService = new RainManageIServiceImpl();
String name = request.getParameter("a");
String date1 = request.getParameter("date");
// 实现字符串和日期类型的转换
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(date1);
String rainString = request.getParameter("b");
int rains = Integer.parseInt(rainString);
String address = request.getParameter("c");
String mobile = request.getParameter("d");
RainManage rain = new RainManage();
rain.setDistrictname(name);
rain.setMonitorTime(date);
rain.setRain(rains);
rain.setMonitoringStation(address);
rain.setMonitoringAddress(mobile);
try {
int result = rainManageIService.insert(rain);
if (result == -1) {
out.print("<script type=\"text/javascript\">");
out.print("alert(\"添加没有成功\");");
out.print("</script>");
} else if (result == 0) {
out.print("<script type=\"text/javascript\">");
out.print("alert(\"未找到相关信息\");");
out.print("</script>");
} else {
out.print("<script type=\"text/javascript\">");
out.print("alert(\"已经成功添加信息,点击确认返回首页\");");
out.print("location.href=\"" + contextPath
+ "/ListServlet?opr=list\";");
out.print("</script>");
}
} catch (Exception e) {
e.printStackTrace();
}
^Truth
发布了105 篇原创文章 · 获赞 45 · 访问量 3万+
私信
关注
标签:String,request,getParameter,rain,乱码,print,out 来源: https://blog.csdn.net/qq_44739706/article/details/104611703