编写一个javaBean,专门进行内码转换,这样重用性比较好,不要直接写到jsp文件中。/**
* 标题:字符转换
* 说明:用于解决中文问题
* Copyright: Copyright (c) 2003
* Company:SinfoTech
* @author: 王辉
* @version: 1.0
* @time:2003.2.17
*/
public abstract class strOper {
public strOper() {
}
public static String toChinese(String str){
try{
String temp_s=str;
byte[] temp_b=temp_s.getBytes("ISO8859-1");
String temp=new String(temp_b);
return temp;
}
catch(Exception e){
System.out.print("中文转换失败!");
}
return null;
}
}
以上是javaBean
* 标题:字符转换
* 说明:用于解决中文问题
* Copyright: Copyright (c) 2003
* Company:SinfoTech
* @author: 王辉
* @version: 1.0
* @time:2003.2.17
*/
public abstract class strOper {
public strOper() {
}
public static String toChinese(String str){
try{
String temp_s=str;
byte[] temp_b=temp_s.getBytes("ISO8859-1");
String temp=new String(temp_b);
return temp;
}
catch(Exception e){
System.out.print("中文转换失败!");
}
return null;
}
}
以上是javaBean
开头:
<%@ page contentType="text/html charset=gb2312"%>
<%@ page import="strOper"%>
中间调用时要得到上页传过来的中文(英文字母用这个函数转化也无所谓)
<%
String name=strOper.toChinese(request.getParameter("name"));
%>
是不是可以通用,以后每编一个程序都要加三句话?
我现在是编了个showtime.jsp(第一个程序)的时候出现的,
2.刚学的话,那可以简单一些,将那个转换文件直接写入jsp文件。如下
<%@ page contentType="text/html; charset=GB2312" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%!
public String toChinese(String str){
try{
String temp_s=str;
byte[] temp_b=temp_s.getBytes("ISO8859-1");
String temp=new String(temp_b);
return temp;
}
catch(Exception e){
System.out.print("中文转换失败!");
}
return null;
}
%><%
String name=strOper.toChinese(request.getParameter("name"));
......
%>
</body>
</html>
<%@ page contentType="text/html; charset=GB2312" %>
<html>
<head></head>
<body bgcolor="#000000"><%!
public String toChinese(String str){
try{
String temp_s=str;
byte[] temp_b=temp_s.getBytes("ISO8859-1");
String temp=new String(temp_b);
return temp;
}
catch(Exception e){
System.out.print("中文转换失败!");
}
return null;
}
%>
<%
String username=toChinese(request.getParameter("username"));
String password=toChinese(request.getParameter("password"));
%>
<table border=1 bgcolor="#ffffff">
<form action="test.jsp" method="post">
<tr>
<td>用户名</td>
<td><input type=text id="username" name="username" value="<%=username%>"></td>
</tr>
<tr>
<td>密码</td>
<td><input type=text id="password" name="password" value="<%=password%>"></td>
</tr>
<tr>
<td colspan=2><input type=submit></td>
</tr>
</form>
</table>
</body>
</html>
<html>
<head>
<title>无标题文档</title>
</head><body bgcolor="#FFFFFF" text="#000000">
今天是:
<%=new java.util.Date()%><br>
</body>
</html>这样可以吗?有什么问题?
那样当然可以,不过他只解决了本页面的中文显示问题,而传过来的参数仍然是乱码,不信你试一下。而最有用的是从上页取中文参数。所以在tomcat中必须进行手工中文转换(即上面那个函数)。