<% 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>里面输入的数据
博客主要介绍了Java中字符串判断的相关内容,指出String变量是类实例,不同创建和判断方式结果不同,在JSP中判断字符串要用compareTo方法。还讲解了JSP里session对象的getAttribute()方法和request对象的getParameter()方法,用于获取数据。
1901

被折叠的 条评论
为什么被折叠?



