中文处理方法

来源:互联网 发布:淘宝c店导出库存 编辑:程序博客网 时间:2024/05/17 02:22

<% String title="你好";
byte[] tmpbyte=title.getBytes("ISO8859_1");
title=new String(tmpbyte);
out.print(title); %>
或者<%=title%>

。在asp中经常使用到字符串判断语句如 if state="真是傻瓜" then.....
在java中String变量不是一个简单的变量而是一个类实例,不同的方法会得到不同的结果
a.
String str1="我是傻瓜";
String str2="我是傻瓜"; (or String str2="我是"+"傻瓜"; )
if (str1==str2)
out.print("yes");
else
out.print("no");
结果是"yes"。
大概是编译优化,str1,str2指向同一个类实例;

b.
String str1,str2,str3;
str1="我是傻瓜";
str2="我是";
str3=str2+"傻瓜";
if (str1==str3)
out.print("yes");
else
out.print("no");
结果是"no"。

String str1=new String("我是傻瓜");
String str2=new String("我是傻瓜");
if (str1==str2)
out.print("yes");
else
out.print("no");
结果是"no"。

String str1=new String("我是傻瓜");
String str2=new String("我是傻瓜");
if (str1.compareTo(str2)==0)
out.print("yes");
else
out.print("no");
结果是"yes"。

所以在jsp中判断字符串要使用compareTo方法,用惯传统语言还真一下子适应不过来,熟
悉java的朋友应该没这个问题

getAttribute()是session对象的方法,与session.setAttribute()对应getParameter()是request对象的方法,是用来取得上一页面传过来的表单或querystring的
如下:
a.jsp里
session.setAttribute("aaa","hello");
到b.jsp里就用
String aaaa=session.getAttribute("aaa");来取得hello这个String.
在aa.html里
<form method=post action=bb.jsp>
<input type=text name=id>
<input type=submit name=to>
</form>
bb.jsp就要用request.getParameter("id");来取得
表单中<input type=text name=id>里面输入的数据

原创粉丝点击