中文问题,怎么都不行

hzqq 2003-07-09 09:00:59
代码如下,总是出现???
用的是mysql
<%@ page language="Java" import="java.sql.*" %>
<%@ page contentType="text/html; charset=GBK" %>
<jsp:useBean id="sqlbean" scope="session" class="mysqlbean.mysqlbean"/>
<%


String uid=(String)session.getAttribute("username");
if (uid==null)response.sendRedirect("login.jsp");

String detail=request.getParameter("detail");
String state=sqlbean.toChinese(request.getParameter("state"));
String comment=sqlbean.toChinese(request.getParameter("comment"));
if (detail!="")
{
String sql="INSERT INTO `event` ( `id` , `userid` , `when` , `detail` , `comment` , `state` ) VALUES ('', '"+uid+"',NOW(),'"+detail+"', '"+comment+"','"+state+"')";
sqlbean.execNoResult(sql);
response.sendRedirect("page.jsp");
}
else
{%>
<script language="JavaScript">
alert("请填写详细内容");
window.location="page.jsp";
</script>
<%
}
%>
toChinese代码
public static String toChinese(String strvalue)
{
try{
if(strvalue==null)
return null;
else
{
strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK");
return strvalue;
}
}
catch(Exception e)
{
return null;
}
}

...全文
38 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
01cyl 2003-07-09
  • 打赏
  • 举报
回复
在getAttribute,getParameter之前加上一句request.setCharacterEncoding("gb2312");
hzqq 2003-07-09
  • 打赏
  • 举报
回复
就是不行啊

同志们有空帮我测试一下
http://218.193.178.125:8083/examples/jsp/project/login.htm
用hzqq密123456
源代码在
ftp://jsp:jsp@218.193.178.125:2121/
好人做到底吧,我第一次编jsp就遇到这种问题,哎
Tongates 2003-07-09
  • 打赏
  • 举报
回复
怎么还没有搞定?不会吧。
些个测试的页面。
<html>
<%@ page language="java" %>
<%@ page contentType="text/html;charset=gb2312" %>
public static String parseToChinese(String str) {

if( str == null ){
return null ;
}

String out = str.trim();

if( out.equalsIgnoreCase("")){
return out ;
}

try{
out = new String( out.getBytes("ISO8859_1") );
}catch ( java.io.UnsupportedEncodingException e ){
e.printStackTrace();
}

return( out );
}
</head>
<body>
<%
String strTest = "这是中文" ;
strTest = parseToChinese( strTest ) ;
out.println(strTest) ;
%>
</body>
</html>
accp 2003-07-09
  • 打赏
  • 举报
回复
真不行的话把你驱动杀了,去网上下个新的
lovelanzhi716 2003-07-09
  • 打赏
  • 举报
回复
不是吧,我的JSP里面也有这一句啊,
当我用上面哪个方法转换一下后就可以了

这个好像与tomcat没有什么关系呢
accp 2003-07-09
  • 打赏
  • 举报
回复
在你的连接字符串后面加上
jdbc:mysql://192.168.0.2:3306/database?useUnicode=true&characterEncoding=GBK

这样还不行我去自杀
hzqq 2003-07-09
  • 打赏
  • 举报
回复
我用phpmyadmin修改数据库成中文,若没有
<%@page contentType="text/html;charset=GBK"%>
则可以正常显示,有也是??
hzqq 2003-07-09
  • 打赏
  • 举报
回复
还是不行啊……
我要自杀了
accp 2003-07-09
  • 打赏
  • 举报
回复
再在每个页面开头加上
<%@page contentType="text/html;charset=GBK"%>
<%request.setCharacterEncoding("GBK");%>
就没问题了,不用转

hzqq 2003-07-09
  • 打赏
  • 举报
回复
我用的是tomcat要不要什么设置?
hzqq 2003-07-09
  • 打赏
  • 举报
回复
还是老样子……
怎么会这样?
Tongates 2003-07-09
  • 打赏
  • 举报
回复
public static String parseToChinese(String str) {

if( str == null ){
return null ;
}

String out = str.trim();

if( out.equalsIgnoreCase("")){
return out ;
}

try{
out = new String( out.getBytes("ISO8859_1") );
}catch ( java.io.UnsupportedEncodingException e ){
e.printStackTrace();
}

return( out );
}
lovelanzhi716 2003-07-09
  • 打赏
  • 举报
回复
public String getUnicode(String str) throws Exception{
if(!str.equals("")){
byte[] inputBytes = str.getBytes("8859_1");
str=new String(inputBytes,"GB2312");}
else
str="";
return str;
}//end

把这个方法写进你的BEAN,

再这样调用:
String sql="INSERT INTO `event` ( `id` , `userid` , `when` , `detail` , `comment` , `state` ) VALUES ('', '"+uid+"',NOW(),'"+detail+"', '"+comment+"','"+state+"')";
sql=getUnicode(sql);
sqlbean.execNoResult(sql);

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧