jsp九大内置对象包括request response session application out page config exception pageContent
其中 request response out page config exception pageContent对象的有效范围是当前页面的应用 session 有效范围是当前会话(当前客户端的所有页面) application 有效范围是整个应用程序,只要服务器不关闭对象就有效
====================================================================
request
====================================================================
request.setCharacterEncoding("UTF-8");设置请求编码,防止乱码
request.setAttribute("Unmae", new Object());将数据保存到request范围内的变量中
request.forward(String Url);转发
====================================================================
cookie
====================================================================
URLEncoder.decode();读取cookie信息时将信息解码
====================================================================
response
====================================================================
response.addCookie()将一个cookie对象发送到客户端
response.sendRedirect(String path); 重定向
====================================================================
application
====================================================================
application.setAttribute(key,value);给application添加属性值
====================================================================
session
====================================================================
session.setMaxInactiveInterval(int num);设置session对象的有效活动时间
session.isNew();判断是否为新用户 返回Boolean
session.setAttribute();
session.invalidate();销毁当前session
====================================================================
案例
====================================================================
1:防止表单在网站外部提交 使用request
String addressRequestURL().toString();//获得当前的IE地址
String addresstwoHeader("referer");//获得请求地址
String pathadd=null;//当前服务器主机
String pathaddtwo=null;//访问服务器主机
//获得访问主机名称
try {
if(address!=null&&address!=""){
URL url=new URL(address);
pathaddHost();
}
if(addresstwo!=null&&addresstwo!=""){
URL url1=new URL(addresstwo);
pathaddtwoHost();
}
if(pathadd.equals(pathaddtwo)){
}else{
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
2:网站计数器 application
synchronized java关键字 使用中一个线程未完成锁定下一个线程
int i=0;
synchronized (application) {
Attribute("times")==null){//服务器启动后的第一位访问者
i=1;
}else{
i=Integer.Attribute("times"));
i++;//访问次数加一
}
application.setAttribute("times",String(i)); //将访问次数存入到application中
}
====================================================================
本文发布于:2024-01-31 01:06:39,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170663440024230.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |