requst.getContextPath() 取得上下文信息(重点 )
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*" %>
<html>
<head><title>www.baidu.com,这是一个学习的好网站</title></head>
<body>
<%=request.getContextPath()%>/image/001.jpg
<img src= "<%=request.getContextPath()%>/image/001.jpg">
</body>
</html>
问题:何时用request中getArrtibute(),何时getParameter()?
答: 如果要使用getAttribute()则之前一定会存在setAttribute()的设置操作,否则无法取得
如果使用getParameter() 则表示接受参数,那么参数的来源有以下几种:
1.表单提交
2.地址提交
3,。通过<jsp:inclucde>,<jsp:forward>传递而来的参数
getParameter()是无法接受setAttribute() 设置的内容的
*********************************************************************************
response对象:用于对客户端的请求进行回应,将WEB服务器处理后的结果发回给客户端,response对象属于
javax.servlet.http.HttpServletResponse接口的实例,
HttpServletResponse接口的定义如下:
public interface HttpServletResponse extends ServletResponse
用例1:定时刷新:
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*" %>
<html>
<head><title>www.baidu.com,这是一个学习的好网站</title></head>
<body>
<%! //定义全局变量
int count = 0;
%>
<%
response.setHeader("refresh","2"); //页面2秒一刷新
%>
<h3>已经访问了<%=count++%>次!</h3>
</body>
</html>
用例2:定时跳转
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*" %>
<html>
<head><title>www.baidu.com,这是一个学习的好网站</title></head>
<body>
<h3>3秒后跳转到hello.htm页面</h3>
<%
response.setHeader("refresh","3;URL=hello.htm"); //3秒后跳转
%>
</body>
</html>
此种跳转并非万能的!!譬如后退后请再刷新
跳转并且地址栏改变的跳转是客户端跳转,
问题2 : <jsp:forward>跳转和response.sendRedirect()两种跳转语句的却别是什么?
答:<jsp:forward>语句跳转的特点:
1,服务器端跳转,跳转之后地址栏不改变
2,属于无条件的跳转,执行之后立刻跳转,跳转之前的语句会执行,跳转之后的语句不会执行
response.sendRedirect()特点:
1,客户端跳转,跳转之后地址栏改变,不可以传递request属性
2,是在所有的语句都执行完之后才完成的跳转操作,
--------------------------------------########################-----------------------------
操作Cookie,
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*" %>
<html>
<head><title>www.baidu.com,这是一个学习的好网站</title></head>
<body>
<%
Cookie c[] = request.getCookies(); //取得客户端的全部Cookie
for(int x = 0;x<c.length;x++){
%>
<h3><%=c[x].getName()%> --> <%=c[x].getValue()%></h3>
<%
}
%>
</body>
</html>
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*" %>
<html>
<head><title>www.baidu.com,这是一个学习的好网站</title></head>
<body>
<%
Cookie c1 = new Cookie("baidu","www.baidu.com");
Cookie c2 = new Cookie("liuddhua","www.hefdfsd.com");
c1.setMaxAge(100); //设置时间
c1.setMaxAge(100);
response.addCookie(c1); //加入Cookie
response.addCookie(c2);
%>
</body>
</html>
**************************************************************************************
Session部分
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*" %>
<html>
<head><title>www.baidu.com,这是一个学习的好网站</title></head>
<body>
<%
String id = session.getId();
%>
<h3>SESSION ID:<%=id%></h3>
<h3>SESSION ID长度:<%=id.length()%></h3>
</body>
</html>
session在进行处理的时候使用到了cookie的处理机制
2,服务器如果关闭了,则session id肯定需要重新分配,原本的session id根本就不保留,服务器关闭之后session id 就没
有了。
3,session序列化:(理解)
<Context path="/web" docBase="D:\webdemo">
<Manager
className="org.apache.catalina.session.PersistentManager" >
debug=0 saveOnRestart = "true"
maxActiveSession="-1" minIdleSwap = "-1"
maxIdleSwap="-1" maxIdleBackup= "-1"
<Store classname="org.apache.catalina.session.FileStore"
directory="d:\temp"/>
</Manager>
</Context>
注销session:
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*" %>
<html>
<head><title>www.baidu.com,这是一个学习的好网站</title></head>
<body>
<%
response.setHeader("refresh","2;URL=login.jsp");
session.invalidate(); //注销,表示所有的session失效
%>
<h3>您已成功退出本系统,两秒后跳转回首页</h3>
<h3>如果没有跳转,请按<a href="login.jsp">这里</a>!</h3>
</body>
</html>
3,会话跟踪技术:
3.1 , 通过session提供的方法保存。
3.2, 通过Cookie
3.3 通过表单的隐藏域完成
3.4 通过地址重写(Struts使用较多 )
判断是不是新用户:
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*" %>
<html>
<head><title>www.baidu.com,这是一个学习的好网站</title></head>
<body>
<%
if(session.isNew()){
%>
<h3>欢迎新用户光临!</h3>
<%
} else {
%>
<h3>您已经是老用户了</h3>
<%
}
%>
</body>
</html>
3,停留时间:
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*" %>
<html>
<head><title>www.baidu.com,这是一个学习的好网站</title></head>
<body>
<%
long start = session.getCreationTime();
long end = session.getLastAccessedTime();
long time = (end - start) / 1000; //取得秒
%>
<h3>您已经停留了<%=time%>秒</h3>
</body>
</html>