表单POST后,收到的中文是乱码解决办法

对于这个问题,在网上有很多答案,但是始终没有找到自己要的,其实有时只是一个小小的问题,但自己如果不知道,那就是大问题,我的解决方案如下:

1.将JSP中所有的charSe设置成:GB2312

2.数据库url:String url = "jdbc:mysql://localhost/message?useUnicode=true&characterEncoding=GB2312";

3.提交数据页面程序如下:

<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=GB2312"
    pageEncoding="GB2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<%request.setCharacterEncoding("gb2312");%>   
<jsp:useBean id="words" class="com.words" scope="page"></jsp:useBean>
<jsp:setProperty property="*" name="words"/>
<title>Insert title here</title>
</head>
<body>
<div> <img src = "E:/JSP/worksapce/Message/Img/title_img.jpg"></div>
<div style="margin-top: 30px;">
<div style = "float:left; width:15%;height:300px; border-width: thin;border-style: solid;"> <%@include file="nav_left.jsp"%> </div>
<div style = "float:left; margin-left: 30px">
<form action="" method="post" style="border-style:solid;border-width: 1px;width: 800px;height: 600px">
<div style="margin-top: 6px;"><label style="margin-left: 5px">标题:</label><input type="text" name="wordsTitle" border="1"><br> <hr></div>
<label style="margin-left: 5px">内容:<br><textarea name = "wordsContent" rows="30" cols="105" style="margin-left: 5px;margin-top: 5px;"></textarea><br>
<div style="margin-top: 10px;margin-left: 30px">
<input type="submit" name="sumbin" value="提交 ">
</div>
</form>
</div>
<%

if(words.getWordsTitle().length()>1){

Object ret = session.getAttribute("userid");
if(ret == null) out.print("<script>alert('您还未登录,请先登录');</script>");
else{
int id = (int) ret;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
words.setWordsTime(format.format(new Date()));
words.setWordsId(2);
words.setUserId(id);
words.addWords();
}
}
//int userId = (int)session.getAttribute("userid");

%>
</div>
</body>
</html>

关键是:<%request.setCharacterEncoding("gb2312");%>   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值