我的程序与测试如下:
<%@page import="java.sql.*" contentType="text/html;charset=gb2312"%>
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="news.util.*"%>
<html>
<body>
<jsp:useBean id="mana1" scope="page" class="dataexec.dataexec"/>
<%
CodeFilter codefilter=new CodeFilter();
String id=null;
String content=null;
int myid=Integer.parseInt(request.getParameter("myid"));
String context=request.getParameter("context");
context=new String(context.getBytes("ISO8859_1"),"GB2312");
context=codefilter.unHtml(context); //测试输出为中文
String sql1="insert into test values('"+myid+"','"+context+"')";
mana1.executeQuery(sql1);
%>
<%
String sql="select * from test";
ResultSet rs=mana1.executeQuery(sql);
while(rs.next())
{
id=rs.getString(1);
content=rs.getString(2);
out.println(content); //测试输出为中文
content=codefilter.change(content); //测试输出为数据码,如输入"斩草除根"则为斩草除根
}
rs.close();
%>
</body>
</html>
转换用的javabean如下:
package news.util;
import java.io.*;
public class CodeFilter{
public CodeFilter() {}
public static String change(String s) {
s = toHtml(s);
return s;
}
//特殊字符转为Html
public static String toHtml(String s) {
s = Replace(s,"&","&");
s = Replace(s,"<","<");
s = Replace(s,">",">");
s = Replace(s,"\t"," ");
s = Replace(s,"\r\n","\n");
s = Replace(s,"\n","<br>");
s = Replace(s," "," ");
s = Replace(s,"'","'");
s = Replace(s,"\\","\");
return s;
}
//逆
public static String unHtml(String s){
s = Replace(s,"<br>","\n");
s = Replace(s," "," ");
return s;
}
//Replace
public static String Replace(String source,String oldString,String newString) {
if(source == null) return null;
StringBuffer output = new StringBuffer();
int lengOfsource = source.length();
int lengOfold = oldString.length();
int posStart = 0;
int pos;
while((pos = source.indexOf(oldString,posStart)) >= 0) {
output.append(source.substring(posStart,pos));
output.append(newString);
posStart = pos + lengOfold;
}
if(posStart < lengOfsource) {
output.append(source.substring(posStart));
}
return output.toString();
}
}
<%@page import="java.sql.*" contentType="text/html;charset=gb2312"%>
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="news.util.*"%>
<html>
<body>
<jsp:useBean id="mana1" scope="page" class="dataexec.dataexec"/>
<%
CodeFilter codefilter=new CodeFilter();
String id=null;
String content=null;
int myid=Integer.parseInt(request.getParameter("myid"));
String context=request.getParameter("context");
context=new String(context.getBytes("ISO8859_1"),"GB2312");
context=codefilter.unHtml(context); //测试输出为中文
String sql1="insert into test values('"+myid+"','"+context+"')";
mana1.executeQuery(sql1);
%>
<%
String sql="select * from test";
ResultSet rs=mana1.executeQuery(sql);
while(rs.next())
{
id=rs.getString(1);
content=rs.getString(2);
out.println(content); //测试输出为中文
content=codefilter.change(content); //测试输出为数据码,如输入"斩草除根"则为斩草除根
}
rs.close();
%>
</body>
</html>
转换用的javabean如下:
package news.util;
import java.io.*;
public class CodeFilter{
public CodeFilter() {}
public static String change(String s) {
s = toHtml(s);
return s;
}
//特殊字符转为Html
public static String toHtml(String s) {
s = Replace(s,"&","&");
s = Replace(s,"<","<");
s = Replace(s,">",">");
s = Replace(s,"\t"," ");
s = Replace(s,"\r\n","\n");
s = Replace(s,"\n","<br>");
s = Replace(s," "," ");
s = Replace(s,"'","'");
s = Replace(s,"\\","\");
return s;
}
//逆
public static String unHtml(String s){
s = Replace(s,"<br>","\n");
s = Replace(s," "," ");
return s;
}
//Replace
public static String Replace(String source,String oldString,String newString) {
if(source == null) return null;
StringBuffer output = new StringBuffer();
int lengOfsource = source.length();
int lengOfold = oldString.length();
int posStart = 0;
int pos;
while((pos = source.indexOf(oldString,posStart)) >= 0) {
output.append(source.substring(posStart,pos));
output.append(newString);
posStart = pos + lengOfold;
}
if(posStart < lengOfsource) {
output.append(source.substring(posStart));
}
return output.toString();
}
}
String ss=request.getParameter("context");
byte[] by;
by=ss.getBytes("iso-8859-1");
ss=new String(by);
然后,输出一下:
out.print(ss);看看是不是中文