String chn_string2=new String(platenumber.getBytes("ISO-8859-1"),"GB2312")
~~~~~~~~~~~~
~~~~~~~~~~~~
解决方案 »
- 关于jsp+tomcat7.0的问题
- tomcat6.0启动问题大神进来帮下菜鸟啊
- 我有两个超链接,用jquery写 怎么控制这两个超链接
- JDK6支持servlet2.4吗?
- [struts2]ModelDriven的一个问题
- Table的tbody里用innerHTML怎么添值啊(已经疯了)
- 请问如何让jbuilder对jsp文件编写时出现java的方法自动提示功能?
- 怎样在jb中new一个jsp文件时指定其存放路径
- 谁能告诉我bean与class的关系
- 急!毕业设计:Applet调用Servlet访问数据库的问题。
- 大家来抢分啊~~~小弟写了一个jsp注册也面,但是执行的时候总是有异常~~~急死我了,一会就要交了大家帮忙啊~~在线等~~
- 我实在找不出这个JSP文件错在哪里?
<%!String native2Unicode(String s)
{
if (s == null || s.length() == 0)
{
return null;
}
byte[] buffer = new byte[s.length()];
for (int i = 0; i < s.length(); i++)
{
buffer[i] = (byte)s.charAt(i);
}
return new String(buffer);
}
%>
--从数据库读取显示在本地的编码转换函数
<%!String unicode2Native(String s)
{
if (s == null || s.length() == 0) {
return null;
}
char[] buffer = new char[s.length() * 2];
char c;
int j = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= 0x100) {
c = s.charAt(i);
byte []buf = (""+c).getBytes();
buffer[j++] = (char)buf[0];
buffer[j++] = (char)buf[1];
}
else {
buffer[j++] = s.charAt(i);
}
}
return new String(buffer, 0, j);
}
%>此外你所有的方法中的"ISO8859-1"是错误的,应该是"ISO-8859-1"
<html>
<body>
以下是从MS SQL SERVER 数据库读取的数据:<hr>
<table border=1>
<tr>
<td>图书ID号</td>
<td>书 名</td>
<td>出 版 社</td>
<td>价 格</td>
</tr>
<%!
String parseChinese(String in)
{
String s = null;
byte temp [];
if (in == null)
{
System.out.println("Warn:Chinese null founded!");
return new String("");
}
try
{
temp=in.getBytes("iso-8859-1");
s = new String(temp);
}
catch(UnsupportedEncodingException e)
{
System.out.println (e.toString());
}
return s;
}
%>
<%! String trans(String chi)
{
String result=null;
byte temp[];
try
{
temp=chi.getBytes("iso-8859-1");
result=new String(temp);
}
catch(UnsupportedEncodingException e)
{
System.out.println(e.toString());
}
return result;
}
%>
<%!String native2Unicode(String s)
{
if (s == null || s.length() == 0)
{
return null;
}
byte[] buffer = new byte[s.length()];
for (int i = 0; i < s.length(); i++)
{
buffer[i] = (byte)s.charAt(i);
}
return new String(buffer);
}
%>
<%!String unicode2Native(String s)
{
if (s == null || s.length() == 0) {
return null;
}
char[] buffer = new char[s.length() * 2];
char c;
int j = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= 0x100) {
c = s.charAt(i);
byte []buf = (""+c).getBytes();
buffer[j++] = (char)buf[0];
buffer[j++] = (char)buf[1];
}
else {
buffer[j++] = s.charAt(i);
}
}
return new String(buffer, 0, j);
}
%><%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=jspdev";
//jspdev为你的数据库名称
String user="jiayu";
String password="jiayu";
Connection conn= DriverManager.getConnection(url,user,password);//Connection con=java.sql.DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=jspdev","LocalSystem","LocalSystem");
Statement stmt=conn.createStatement();
ResultSet rst=stmt.executeQuery("select * from book order by price");
while(rst.next())
{
out.println("<tr>");
out.println("<td>"+native2Unicode(rst.getString("bookid"))+"</td>");
out.println("<td>"+rst.getString("bookName")+"</td>");
out.println("<td>"+rst.getString("publisher")+"</td>");
out.println("<td>"+rst.getFloat("price")+"</td>");
out.println("</tr>");
}
//关闭连接,释放资源
rst.close();
stmt.close();
conn.close();
%>
</table>
</body>
</html>这是我很久以前的一个例子,看看能否对你有帮助
<mime-mapping>
<extension>vnd.wap.wml</extension>
<mime-type>text/vnd.wap.wml;charset=GB2312</mime-type>
</mime-mapping>
试试看vnd.wap.wml是你访问你的文件时的后缀名
一、JSP页面显示乱码二、表单提交中文时出现乱码三、数据库连
大家在JSP的开发过程中,经常出现中文乱码的问题,可能一至困扰着您,我现在把我在JSP开发中遇到的中文乱码的问题及解决办法写出来供大家参考。 一、JSP页面显示乱码
下面的显示页面(display.jsp)就出现乱码:
<html>
<head>
<title>JSP的中文处理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head> <body>
<%
out.print("JSP的中文处理");
%>
</body>
</html>
对不同的WEB服务器和不同的JDK版本,处理结果就不一样。原因:服务器使用的编码方式不同和浏览器对不同的字符显示结果不同而导致的。解决办法:在JSP页面中指定编码方式(gb2312),即在页面的第一行加上:<%@ page contentType="text/html; charset=gb2312"%>,就可以消除乱码了。完整页面如下:
<%@ page contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>JSP的中文处理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head> <body>
<%
out.print("JSP的中文处理");
%>
</body>
</html> 二、表单提交中文时出现乱码
下面是一个提交页面(submit.jsp),代码如下:
<html>
<head>
<title>JSP的中文处理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head> <body>
<form name="form1" method="post" action="process.jsp">
<div align="center">
<input type="text" name="name">
<input type="submit" name="Submit" value="Submit">
</div>
</form>
</body>
</html>
下面是处理页面(process.jsp)代码:
<%@ page contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>JSP的中文处理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head> <body>
<%=request.getParameter("name")%>
</body>
</html>
如果submit.jsp提交英文字符能正确显示,如果提交中文时就会出现乱码。原因:浏览器默认使用UTF-8编码方式来发送请求,而UTF-8和GB2312编码方式表示字符时不一样,这样就出现了不能识别字符。解决办法:通过request.seCharacterEncoding("gb2312")对请求进行统一编码,就实现了中文的正常显示。修改后的process.jsp代码如下:
<%@ page contentType="text/html; charset=gb2312"%>
<%
request.seCharacterEncoding("gb2312");
%>
<html>
<head>
<title>JSP的中文处理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head> <body>
<%=request.getParameter("name")%>
</body>
</html> 三、数据库连接出现乱码
只要涉及中文的地方全部是乱码,解决办法:在数据库的数据库URL中加上useUnicode=true&characterEncoding=GBK就OK了。 四、数据库的显示乱码
在mysql4.1.0中,varchar类型,text类型就会出现中文乱码,对于varchar类型把它设为binary属性就可以解决中文问题,对于text类型就要用一个编码转换类来处理,实现如下:
public 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("编码转换错误:"+e.getMessage());
return "";
}
}
}
把它编译成class,就可以调用Convert类的静态方法ISOtoGB()来转换编码。
数据库是oracle8
当时开发时显示正常
但是部署上线后数据库出来的文字是乱码
后来更改了oracle的数据库jar包解决了问题
你可以试试看SQLSERVER2000有没有最新的jar包
试试这个
jdbc:microsoft:sqlserver://ip:14433;DatabaseName=db;useUnicode=true;characterEncoding=ISO8859_1
看看
然后输入出时String name=getString("name").trim(),out.pring(name);或
String chn_name=new String(name.getBytes("GB2312"));out.print(chn_name)或
String chn_name=new String(name.getBytes("GB2312"));out.print(chn_name)
都不行!头部是这样设的
<%@ page contentType="text/vnd.wap.wml;charset=gb2312"%>
<%request.setCharacterEncoding("UTF-8");%>
或
<%@ page contentType="text/vnd.wap.wml;charset=gb2312"%>
<%request.setCharacterEncoding("GB2312");%>也都不行,晕死我了,现在我真的怀疑是系统问题了,或JDBC版本问题了,请各各位谁有solaris下的最新JDBC驱动给我一个,我就差这个没试了!谢谢各位的帮助!!!:)
jdbc:microsoft:sqlserver://ip:14433;DatabaseName=db;useUnicode=true;characterEncoding=UTF-8
每个页面顶上加上:
<%@ page contentType="text/vnd.wap.wml;charset=UTF-8"%>
<%request.setCharacterEncoding("UTF-8");%>
String temp=new String(temp_t);
alert("ding");
</script>