偶用 MSSQL_sp4 + tomcat5.028
jsp页面向数据库中添加数据,数据库中中文显示?????
希望遇到过此问题的朋友帮帮偶。谢谢了。我尝试用过:
1.
-------------------------------------------------------------------------
<%@ page contentType="text/html; charset=gb2312" language="java" %>2.
-------------------------------------------------------------------------
<%
request.setCharacterEncoding("GB2312") ;
%>3.过滤器
-------------------------------------------------------------------------
package cn.com.minority.filter ;import java.io.* ;
import javax.servlet.* ;
public class EncodingFilter implements Filter
{
public void init(FilterConfig filterConfig)
throws ServletException
{
}
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException,
ServletException
{
try
{
request.setCharacterEncoding("GB2312") ;
}
catch (Exception e)
{
}
chain.doFilter(request,response) ;
}
public void destroy()
{
}
};
/*
下面是在web.xml中配置。
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>cn.com.minority.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
*/
jsp页面向数据库中添加数据,数据库中中文显示?????
希望遇到过此问题的朋友帮帮偶。谢谢了。我尝试用过:
1.
-------------------------------------------------------------------------
<%@ page contentType="text/html; charset=gb2312" language="java" %>2.
-------------------------------------------------------------------------
<%
request.setCharacterEncoding("GB2312") ;
%>3.过滤器
-------------------------------------------------------------------------
package cn.com.minority.filter ;import java.io.* ;
import javax.servlet.* ;
public class EncodingFilter implements Filter
{
public void init(FilterConfig filterConfig)
throws ServletException
{
}
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException,
ServletException
{
try
{
request.setCharacterEncoding("GB2312") ;
}
catch (Exception e)
{
}
chain.doFilter(request,response) ;
}
public void destroy()
{
}
};
/*
下面是在web.xml中配置。
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>cn.com.minority.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
*/
解决方案 »
- 求解一个问题 struts2返回json的问题 在线等 急
- Map按key排序,key以时间来保存,求助。
- spring+hibernate+Struts2整合出现问题??
- 有谁能够给我详细解释jquery与dwr的实现js与java之间ajax、rpc调用之间的本质区别吗
- jdbc小问题
- 求助java读取oracle中 TIMESTAMP字段
- throw Exception 的问题
- applet怎样访问宿主服务器(如jboss)所在主机上的文件??
- myEclipse中导入jar包
- 在网上商店中,不同商品的字段数不同,大家怎么应付这种变化的表结构呢?
- 关于jsf commandLink的action问题
- --------JAVA+J2EE视频教程--------
private static String url="jdbc:mysql://localhost/teach?user=root&password=stb&useUnicode=true&characterEncoding=gb2312";
characterEncoding=gb2312设置一下,看可以不mssql不用转码 转码反而容易出现乱码
网上查询了下没查询到。嘿嘿。过滤器上再加上,response.setCharacterEncoding("gb2312");
加上这句了,也不管用,发现问题是,表丹麦单提交后,中文显示就是 ????,下面是表单,设置有问题吗?是从一个BBS上截取的代码。--------------------------------------------------------------
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>用户注册</title>
</head><script language="javascript">
<!--
function check(form)
{
if (form.bbsuser.value=="")
{
alert("请填写昵称!");
form.bbsuser.focus();
return false;
}
if (form.bbsuser.value.length<1||form.bbsuser.value.length>8)
{
alert("昵称长度只能是3-8位。");
form.bbsuser.focus();
form.bbsuser.select();
return false;
}
if (form.name.value=="")
{
alert("请填写用户名!");
form.name.focus();
return false;
}
if (form.name.value.length<2||form.name.value.length>8)
{
alert("用户名长度只能是3-8位。");
form.name.focus();
form.name.select();
return false;
}
if (form.password.value=="")
{
alert("请填写密码!");
form.password.focus();
return false;
}
if (form.password.value.length<3||form.password.value.length>8)
{
alert("密码长度只能是3-8位。");
form.password.focus();
form.password.select();
return false;
}
if (form.pwdsub.value=="")
{
alert("请填写确认密码!");
form.pwdsub.focus();
return false;
}
if (form.pwdsub.value!=form.password.value)
{
alert("密码和密码确认不一致!");
form.pwdsub.focus();
return false;
}
if (form.question.value=="")
{
alert("请填写忘记密码时的问题!");
form.question.focus();
return false;
}
if (form.answer.value=="")
{
alert("请填写问题的答案!");
form.answer.focus();
return false;
}
if (form.email.value=="")
{
alert("请填写您的Email!");
form.email.focus();
return false;
}
if ((form.email.value.indexOf("@")==-1)||(form.email.value.indexOf(".")==-1))
{
alert("email格式有错!(例如:[email protected])");
form.email.focus();
return false;
}return true;
}
//-->
</script>
<body topmargin="5">
<form name="form1" method="post" action="re.jsp" onSubmit="return check(this)" style="MARGIN-TOP: 0px">
<table width=98% border=0 align=center cellpadding=2 cellspacing=1 bgcolor="#A4B6D7">
<tr>
<td width=750 height=25 background="../images/bbs_bg1.gif">
<div align="center"><font color=#ff0000><strong>注册前请先阅读以下协议并填写以下注册信息<span class=S><font color=#ff0000>(*</font><font
color=#ff6600>的为必填项)</font></span> </strong></font> </div></td>
</tr>
</table>
<table width=98% height="492" border=0 align=center cellpadding=2 cellspacing=1 bgcolor="#A4B6D7">
<tbody>
<tr>
<td
width=750 height=20 valign=top bgcolor="#F2F8FF">
<div align="center">
<table width="93%" cellspacing="1" cellpadding="2" align="center" border="0" bordercolor="#FFFFFF">
<tr >
<td colspan="2" bordercolor="#000000" height="30"><br>
互相尊重,对自己的言论和行为负责。 </td>
</tr>
</table>
<table width="93%" border="0" align="center" cellspacing="1" cellpadding="2" height="290">
<tr>
<td colspan="2" height="9">
<hr size="1">
</td>
</tr>
<tr>
<td width="20%" height="20" bgcolor="#F2F8FF">
<div align="right">昵 称:</div>
</td>
<td width="80%" height="20">
<input type="text" name="bbsuser" size="15">
<font color="#CC0000">*</font> <font color="#FF0000">汉字、a-z的英文字母、0-9的数字或下划线组成
</font></td>
</tr>
<tr>
<td width="20%" height="20" bgcolor="#F2F8FF">
<div align="right">用户名:</div>
</td>
<td width="80%" height="20">
<input type="text" name="name" size="15">
<font color="#FFFFFF">
<input type="Button" value="检测用户名是否存在" onClick="chkuser();" class="button" name="Button">
<script language="JavaScript">
<!--
function chkuser()
{
var uname;
uname=form1.name.value;
if (uname=="")
{
alert("您还没有输入用户名!");
form1.name.focus();
return false;
}
else
{
window.open("checkusername.jsp?searchid=" + uname ,"","width=2,height=2,top=1000,left=5000");
}
}
//-->
</script>
</font><font color="#CC0000">*
<input type="radio" name="clubuser_sex" value="男" checked>
男
<input type="radio" name="clubuser_sex" value="女">
女</font></td>
</tr>
<tr>
<td width="20%" height="20" bgcolor="#F2F8FF">
<div align="right">密 码:</div>
</td>
<td width="80%" height="20" >
<input type="password" name="password" size="15">
<font color="#CC0000">*(请填写3-8位密码)</font></td>
</tr>
<tr>
<td width="20%" height="20" bgcolor="#F2F8FF">
<div align="right">密码确认:</div>
</td>
<td width="80%" height="20" >
<input type="password" name="password" size="15">
<font color="#CC0000">*(请填写3-8位密码)</font></td>
</tr>
<tr>
<td width="20%" height="20" bgcolor="#F2F8FF">
<div align="right">取密码时的问题:</div>
</td>
<td width="80%" height="20" >
<input type="text" name="question" size="15">
<font color="#CC0000">*如:你的生日是什么时候?</font></td>
</tr>
<tr>
<td width="20%" height="20" bgcolor="#F2F8FF">
<div align="right">问题的答案:</div>
</td>
<td width="80%" height="20">
<input type="text" name="answer" size="15">
<font color="#CC0000">*如:5月1日</font></td>
</tr>
<tr>
<td width="20%" height="20" bgcolor="#F2F8FF">
<div align="right">你的E-mail信箱:</div>
</td>
<td width="80%" height="20">
<input type="text" name="email" size="15">
<font color="#CC0000">*格式:[email protected]</font></td>
</tr>
<tr>
<td height="20">
<div align="center"></div>
</td>
<td height="30">
<input type="submit" name="Submit2" value="注册">
<input type="reset" name="Submit2" value="重填">
</td>
</tr>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</FORM>
</body>
</html>
我是在JSP文件中加了一句编码语句:
<%@ page contentType="text/html;charset=GB2312"%>
<%request.setCharacterEncoding("GB2312"); %>
然后,在程序中对中文字符进行转码:
存入数据库:
name=request.getParameter("name").trim();
name=new String(name2.getBytes("GB2312"),"ISO-8859-1");
从数据库取出:
listname=new String(rs.getString("name").getBytes("ISO-8859-1"), "GB2312");而且数据库、Tomcat都是用默认的编码。
我转码就乱码
但是页面设置了编码后不转码就对了
不会乱码
jsp+ MS SQL Server
一个字 累++
我是在JSP文件中加了一句编码语句:
<%@ page contentType="text/html;charset=GB2312"%>
<%request.setCharacterEncoding("GB2312"); %>
然后,在程序中对中文字符进行转码:
存入数据库:
name=request.getParameter("name").trim();
name=new String(name2.getBytes("GB2312"),"ISO-8859-1");
从数据库取出:
listname=new String(rs.getString("name").getBytes("ISO-8859-1"), "GB2312");而且数据库、Tomcat都是用默认的编码。
request.setCharacterEncoding("GB2312") ;
response.setContentType("text/html");设置传入第一个 JSP 页面的是文本类型的数据
我用了一个类也解决了这个问题。
package ******;import com.sun.java.swing.plaf.windows.resources.windows;public abstract class Convert {
/**
* 把ISO-8859-1码转换成GB2312
*/
public static String ISOtoGB(String iso){
String gb;
try{
if(iso.equals("")||iso==null){
return "";
}else{
iso = iso.trim();
gb = new String(iso.getBytes("ISO-8859-1"),"GB2312");
return gb;
}
}
catch(Exception e){
System.err.print("编码转换错误:\n"+e.getMessage());
return "";
}
}
public static String gBtoISO(String gb){
String iso;
try{
if(gb.equals("")||gb==null){
return "";
}else{
gb = gb.trim();
iso = new String(gb.getBytes("GB2312"),"ISO-8859-1");
return iso;
}
}
catch(Exception e){
System.err.print("编码转换错误:\n"+e.getMessage());
return "";
}
}
}
==========================提交数据
String massage="注册成功";
massage=Convert.gBtoISO(massage);
String massagetext=":欢迎您成功登陆,点击确定按钮登陆到首页";
massagetext=Convert.gBtoISO(massagetext);
response.sendRedirect("../include/SystemMassage.jsp?pirateName="+pirateName+ "&massage="+massage+ "&massagetext="+massagetext);
=========================接收数据
<%
request.setCharacterEncoding("gb2312");
String pirateName =request.getParameter("pirateName");
pirateName=Convert.ISOtoGB(pirateName);
String massage =request.getParameter("massage");
massage=Convert.ISOtoGB(massage);
String massagetext =request.getParameter("massagetext");
massagetext=Convert.ISOtoGB(massagetext);
%>
正在努力尝试各位的方法:pikef() ( ) 信誉:100 (你这个有现成的例子没?因为不知道怎么写,麻烦了)存入数据库:
name=request.getParameter("name").trim();
name=new String(name2.getBytes("GB2312"),"ISO-8859-1");
从数据库取出:
listname=new String(rs.getString("name").getBytes("ISO-8859-1"), "GB2312");Ghost_520() ( ) 信誉:100 (这个用上了,不过没看到效果,是不是我设置有问题啊?)
request.setCharacterEncoding("GB2312") ;
response.setContentType("text/html");ceryxu() ( ) 信誉:100 (马上试你这个方法看)再次谢谢各位。
我现在在控制台输出时就是 乱码???????,这样子的。
接受代码为:
---------------------------------------------------------------------
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.util.Date"%>
<%@ page import="java.text.*"%>
<%@ include file="inc/conn.jsp"%><%!String regurl,msg,sign;%><%
String img;
String bbsuser=new String(request.getParameter("bbsuser").getBytes("iso-8859-1"));
String name=new String(request.getParameter("name").getBytes("iso-8859-1"),("gb2312"));
String clubuser_sex=new String(request.getParameter("clubuser_sex").getBytes("iso-8859-1"));
String pass=new String(request.getParameter("password").getBytes("iso-8859-1"));
String question=new String(request.getParameter("question").getBytes("iso-8859-1"));
String answer=new String(request.getParameter("answer").getBytes("iso-8859-1"));
String email=new String(request.getParameter("email").getBytes("iso-8859-1"));
String IP=request.getRemoteAddr();
System.out.println("我的测试数据是 :" + name);
Date currTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss",Locale.US);
String ftime=new String(formatter.format(currTime).getBytes("iso-8859-1"));if(clubuser_sex=="男")
{
img="cache37.gif";
}
else {
img="cache41.gif";
}String sql="select * from staff where name='"+name+"'";
ResultSet rs=stmt.executeQuery(sql); if (rs.next()){
out.println("<p>此用户名已存在,请选择其他的用户名!</p>");
out.println("<A HREF=javascript:window.history.back()>[重新注册]</A>");
out.close();
}
else {
sql="insert into staff(name,nickname,password,question,answer,sex,email,reg_date,lasttime,ip,userimg)";
sql=sql+" values('"+name+"','"+bbsuser+"','"+pass+"','"+question+"','"+answer+"','"+clubuser_sex+"','"+email+"','"+ftime+"','"+ftime+"','"+IP+"','"+img+"')"; stmt.executeUpdate(sql);
//Cookie c=new Cookie("clubusername",name);
//response.addCookie(c);
//Cookie d=new Cookie("pass",password);
//response.addCookie(d);
//Cookie e=new Cookie("bbsuser",bbsuser);
//response.addCookie(e);
//Cookie f=new Cookie("bz","0");
//response.addCookie(f);
//Cookie g=new Cookie("bbsadmin","0");
//response.addCookie(g);
//Cookie h=new Cookie("payuser","0");
//response.addCookie(h);
session.setAttribute("bbsuser",bbsuser);
session.setAttribute("name",name);
session.setAttribute("bz","0");
session.setAttribute("bbsadmin","0");
session.setAttribute("payuser","0");
msg="恭喜你,注册成功!";
sign="y";
String smscontent="欢迎您加入本站点参加交流和讨论,本站点为公共论坛,为维护网上公共秩序和社会稳定,请您自觉遵守我们的条款";
String str="insert into sms(title,content,accepter,bbssystem) values('"+msg+"','"+smscontent+"','"+name+"',2)";
stmt.executeUpdate(str);
regurl="main.jsp";
}
%>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="refresh" content="3;URL=<%=regurl%>">
<BR><BR><BR>
<TABLE border=0 align=center>
<TR>
<TD align=center>
<%=msg%><BR><BR>
<A HREF="javascript:window.history.back()">[重新注册]</A>
系统将在3秒钟后自动返回,您也可以直接手工点击<A HREF="<%=regurl%>"><FONT COLOR="blue">[返 回]</FONT></A><BR>
</TD>
</TR>
</TABLE>
<%!private String trans(String chi) {
String result=null;
byte temp[];
try{
temp=chi.getBytes("iso-8859-1");
result=new String(temp,"UTF-8");
} catch(java.io.UnsupportedEncodingException e) {
System.out.println(e.toString());
}
return result;
}
%> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><%
String op=trans(request.getParameter("test"));
%>
你试一下吧,以前我告诉过别人,有人也解决了,希望你能顺利摆平!
request.setCharacterEncoding("GB2312") ;
response.setContentType("text/html;charset=gb2312");如果这样还不行的话,那就帮不上你了!!!
这一点通过在页首添加
<%@ page contentType="text/html;charset=gb2312" pageEncoding="GB2312"%>
就可以实现了!
页面、过滤器、数据库、还有连接数据库的URL都设置成utf-8就行了