对于这个问题,在网上有很多答案,但是始终没有找到自己要的,其实有时只是一个小小的问题,但自己如果不知道,那就是大问题,我的解决方案如下:
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");%>