怎么样在JSP页面中调用这个类?先谢了!

guxinglou 2004-11-01 03:34:34
package com.charset;

public class Charset
{
//转码GBK转ISO
public String toISO(String input) {
try {
byte[] bytes = input.getBytes("GBK");
return new String(bytes,"ISO8859-1");
}catch(Exception ex) {
}
return input;

}

//转码IS0转GBK
public String toGBK(String input) {
try {
byte[] bytes = input.getBytes("ISO8859-1");
return new String(bytes,"GBK");
}catch(Exception ex) {
}
return input;
}
}

编译之后,在JSP页面中用<%@ page import="com.charset.Charset" %>导入
但用什么方法调用toGBK()呢?

比如我提交了一个 name变量,怎么在JSP页面转换呢?
...全文
117 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
guxinglou 2004-11-01
  • 打赏
  • 举报
回复
谢谢大家,由于刚从asp,php转到jsp,才问了这么初级的问题。

问题解决了,谢谢!!
chenxb1980 2004-11-01
  • 打赏
  • 举报
回复
package com.charset;

public class Charset
{
//转码GBK转ISO
public static String toISO(String input) {
try {
byte[] bytes = input.getBytes("GBK");
return new String(bytes,"ISO8859-1");
}catch(Exception ex) {
}
return input;

}

//转码IS0转GBK
public static String toGBK(String input) {
try {
byte[] bytes = input.getBytes("ISO8859-1");
return new String(bytes,"GBK");
}catch(Exception ex) {
}
return input;
}
}

编译之后,在JSP页面中用<%@ page import="com.charset.Charset" %>导入

fbysss 2004-11-01
  • 打赏
  • 举报
回复
当然, fengfengjunjun(fengfengjunjun)说的把public String toGBK(String input) { 改成
public static String toGBK(String input) {
也是可以的
fbysss 2004-11-01
  • 打赏
  • 举报
回复
如果你用<%@ page import="com.charset.Charset" %>的方式,需要初始化一个类实例。
加入如下语句Charset charSet = new Charset();
然后调用charSet.toGBK();

另一种方式,就是使用JavaBean,见 icy_csdn() 的方法。
fengfengjunjun 2004-11-01
  • 打赏
  • 举报
回复
定义成静态方法就好了嘛
guxinglou 2004-11-01
  • 打赏
  • 举报
回复
我使用<%@ page import="com.charset.Charset" %>,再用Charset.toGBK(param);


系统提示出错啊:
org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 5 in the jsp file: /bbs/admin/system/role_add.jsp
Generated servlet error:
C:\Tomcat 5.0\work\Catalina\localhost\_\org\apache\jsp\bbs\admin\system\role_005fadd_jsp.java:79: non-static method toGBK(java.lang.String) cannot be referenced from a static context
role_name = Charset.toGBK(role_name);
^

jinannanhai79 2004-11-01
  • 打赏
  • 举报
回复
Charset.toGBK(param);
icy_csdn 2004-11-01
  • 打赏
  • 举报
回复
<jsp:useBean id="charset" scope="session" class="com.charset.Charset"></jsp:useBean>
<%
String strTest = request.getParameter("test");
String strNew = charset.toGBK(strTest);


%>

81,122

社区成员

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

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