response 常用方法
addCookie(Cookie cookie) 向客户端增加COOKIE
setHeader(String name,Strin value)设置回应的头信息
senRedirect(String location) 页面跳转
设置头信息:
所有 的头信息都 随前瞻请求和回应自动发送到服务器端(客户端)
response.setHeader("refresh","3;URL=HELLO.jsp"); 3秒后定时跳转到HELLO页面
html 头信息设置跳转:<meta http-equiv="refresh" content="3;url=hello.jsp"> 客户端跳转
response.sendRedirect("HELLO.jsp");
response.sendRedirect 与forward 跳转
1.response.sendRedirect 客户端跳转,request无法传值。sendRedirct后代码继续执行
forward 服务端跳转,request可以传值后面代码不执行。
Cookie
Cookie c1=new Cookie("wadenin","001");
Cookie c2=new Cookie("l00","002");
c1.setMaxAge(100);
Cookie c[]=request.getCookies();
for(int i=0;i<c.length;i++){
<h3><%=c[i].getName()%>-----><%=c[i].getValue()%></h3>
}
session接口方法
getId() 获取session ID
getCreationTime() 获取seeson创建时间
getLastAccessedTime() 取得session最后一次操作时间。
isNew() 判断是不是新SESSION
INVALIDATE() 让session失效。
getAttributeNames() 得到全部属性的名称。
session用得最多的地方,登录注销
判断新用户。
application 方法
掌握application 和getServletContext()方法联系;网站计数器
getRealPath()得到虚拟目录对应的绝对路径
Enumeration getAttributeNames() 得到所有属性的名称
getContextPath() 取得当前的虚拟路径名称
addCookie(Cookie cookie) 向客户端增加COOKIE
setHeader(String name,Strin value)设置回应的头信息
senRedirect(String location) 页面跳转
设置头信息:
所有 的头信息都 随前瞻请求和回应自动发送到服务器端(客户端)
response.setHeader("refresh","3;URL=HELLO.jsp"); 3秒后定时跳转到HELLO页面
html 头信息设置跳转:<meta http-equiv="refresh" content="3;url=hello.jsp"> 客户端跳转
response.sendRedirect("HELLO.jsp");
response.sendRedirect 与forward 跳转
1.response.sendRedirect 客户端跳转,request无法传值。sendRedirct后代码继续执行
forward 服务端跳转,request可以传值后面代码不执行。
Cookie
Cookie c1=new Cookie("wadenin","001");
Cookie c2=new Cookie("l00","002");
c1.setMaxAge(100);
Cookie c[]=request.getCookies();
for(int i=0;i<c.length;i++){
<h3><%=c[i].getName()%>-----><%=c[i].getValue()%></h3>
}
session接口方法
getId() 获取session ID
getCreationTime() 获取seeson创建时间
getLastAccessedTime() 取得session最后一次操作时间。
isNew() 判断是不是新SESSION
INVALIDATE() 让session失效。
getAttributeNames() 得到全部属性的名称。
session用得最多的地方,登录注销
判断新用户。
application 方法
掌握application 和getServletContext()方法联系;网站计数器
getRealPath()得到虚拟目录对应的绝对路径
Enumeration getAttributeNames() 得到所有属性的名称
getContextPath() 取得当前的虚拟路径名称
<%
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
String info=request.getParameter("info");
String path=this.getServletContext().getRealPath("/")+"text"+name;
File file=new File(path);
PrintStream ps=null;
ps=new PrintStream(new FileOutputStream(file));
ps.println(info);
ps.close();
%>
<%
Scanner scan=new Scanner(new FileInputStream(path));
StringBuffer sb=new StringBuffer();
while(scan.hasNext())
{
sb.append(scan.next()).append("</br>");
}
scan.close();
%>
<%=sb%>