2007年1月31日星期三

jsp中使用cookie

<%
Cookie cookie1=new Cookie("cookie name", "cookie_content");
cookie1.setMaxAge(60*60*24*365);
cookie1.setPath("/");
response.addCookie(cookie1);
%>

调用cookie:
<%
Cookie [] cookies=request.getCookies();
for(int i=0;i<cookies.length;i++){
if("cookie name".equals(cookies[i].getName())){
//...............
}
break;
}
}

注销cookie:
<%
Cookie c=new Cookie("cookie name", null);
c.setMaxAge(0);
c.setPath("/");
response.addCookie(c);

%>
%>

HTML替换

将一些HTML替换掉

"&","&amp"
"'","''"
"<","&lt"
">","&gt"
"chr(60)","&lt"
"chr(37)","&gt"
"\"","&quot"
"\n","<br/>"
" ","&nbsp"

AJAX开发简略

一个初步的开发框架
  总结上面的步骤,我们整理出一个初步的可用的开发框架,供以后调用;这里,将服务器返回的信息用window.alert以字符串的形式显示出来:
<script language="javascript">
var http_request = false;
function send_request(url) {//初始化、指定处理函数、发送请求的函数
http_request = false;
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest) { //Mozilla 浏览器
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {//设置MiME类别
http_request.overrideMimeType("text/xml");
}
}
else if (window.ActiveXObject) { // IE浏览器
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) { // 异常,创建对象实例失败
window.alert("不能创建XMLHttpRequest对象实例.");
return false;
}
http_request.onreadystatechange = processRequest;
// 确定发送请求的方式和URL以及是否同步执行下段代码
http_request.open("GET", url, true);
http_request.send(null);
}
// 处理返回信息的函数
function processRequest() {
if (http_request.readyState == 4) { // 判断对象状态
if (http_request.status == 200) { // 信息已经成功返回,开始处理信息
alert(http_request.responseText);
} else { //页面不正常
alert("您所请求的页面有异常。");
}
}
}
</script>




  接下来,我们利用上面的开发框架来做两个简单的应用。

A、数据校验
  在用户注册的表单中,经常碰到要检验待注册的用户名是否唯一。传统的做法是采用window.open的弹出窗口,或者window. showModalDialog的对话框。不过,这两个都需要打开窗口。采用AJAX后,采用异步方式直接将参数提交到服务器,用window.alert将服务器返回的校验信息显示出来。代码如下:
  在之间增加一段form表单代码:

<form name="form1" action="" method="post">
用户名:<input name="username">
<input onclick="userCheck()" type="button" value="唯一性检查" name="check">
<input type="submit" value="提交" name="submit">
</form>  在开发框架的基础上再增加一个调用函数:

function userCheck() {
var f = document.form1;
var username = f.username.value;
if(username=="") {
window.alert("用户名不能为空。");
f.username.focus();
return false;
}
else {
send_request('sample1_2.jsp?username='+username);
}
}
  看看sample1_2.jsp做了什么:


  运行一下,嗯,没有弹出窗口,没有页面刷新,跟预想的效果一样。如果需要的话,可以在sample1_2.jsp中实现更复杂的功能。最后,只要将反馈信息打印出来就可以了。


B、级联菜单
  我们在第五部分提到利用AJAX改进级联菜单的设计。接下来,我们就演示一下如何“按需取数据”。

  首先,在中间增加如下HTML代码:

<table cellspacing="0" cellpadding="0" width="200" border="0">
<tbody><tr>
<td height="20">
<a onclick="showRoles('pos_1')" href="javascript:void(0)">经理室</a>
</td>
</tr>
<tr style="DISPLAY: none">
<td id="pos_1" height="20"></td>
</tr>
<tr>
<td height="20">
<a onclick="showRoles('pos_2')" href="javascript:void(0)">开发部</a>
</td>
</tr>
<tr style="DISPLAY: none">
<td id="pos_2" height="20"></td>
</tr>
</tbody></table>
  在框架的基础上增加一个响应函数showRoles(obj):

//显示部门下的岗位
function showRoles(obj) {
document.getElementById(obj).parentNode.style.display = "";
document.getElementById(obj).innerHTML = "正在读取数据..."
currentPos = obj;
send_request("sample2_2.jsp?playPos="+obj);
}
  修改框架的processRequest函数:

// 处理返回信息的函数
function processRequest() {
if (http_request.readyState == 4) { // 判断对象状态
if (http_request.status == 200) { // 信息已经成功返回,开始处理信息
document.getElementById(currentPos).innerHTML = http_request.responseText;
} else { //页面不正常
alert("您所请求的页面有异常。");
}
}
}
  最后就是smaple2_2.jsp了:




 本文摘自网络,档版权归原作者所有