JSP内置对象二(response和session对象)

来源:互联网 发布:程序员写代码的软件 编辑:程序博客网 时间:2024/06/05 08:38

一、response对象

<%@page import="java.util.Date"%><%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>response对象示例</title></head><body><%response.setHeader("Cache-Control", "no-cache");// 设置网页数据内容不会被存储,服务器读完数据后不会存储在缓冲区;response.setIntHeader("Refresh", 2);//设置网页每隔两秒刷新一次out.println("today is "+ new Date().toString());%></body></html>


<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%response.sendRedirect("http://www.baidu.com");%>


<%@page import="com.sun.org.apache.bcel.internal.generic.NEW"%><%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%   Cookie myCookie=new Cookie("CookieName","CookieValue");   myCookie.setMaxAge(3600);//设置cookie最大存活时间   response.addCookie(myCookie);%>

二、session对象

<%@page import="java.util.Date"%><%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Session 示例</title></head><body>session的唯一标识符:<%= session.getId() %><br>session的创建时间:<%=new Date(session.getCreationTime()).toString() %><br>session的最后访问时间:<%=new Date(session.getLastAccessedTime()) %><br>session的实效时间:<%=session.getMaxInactiveInterval() %><br></body></html>

session的默认实效时间为30分钟,不过可以人为修改,打开Web-Content ->WEB_INF 里面的web.xml文件  如果没有的话就选择项目名然后右键选择JavaEE tools 目录下的Generate Development Descpritor stub 就可以看到xml 文件了,在文件中可以通过输入如下代码改变session的实效时间

<session-config>      <session-timeout>10</session-timeout>  </session-config>

session使用实例

注册register.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>登录表单</title></head><body><form action="do_register.jsp" method="post">用户名:<input type="text" name="username"><br>密码: <input type="password" name="password"><br><input type="submit" value="提交"><input type="reset" value="重置"></form></body></html>


do_register.jsp页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%String username=request.getParameter("username");String password=request.getParameter("password");if(username!=null && password!=null){session.setAttribute("username", username);response.setHeader("refresh", "1;URL=welcome.jsp");}%>

欢迎welcome.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>欢迎页面</title></head><body><%   if(session.getAttribute("username")!=null){%>   欢迎:<%=session.getAttribute("username") %>   <a href="logout.jsp">注销</a><br>   <%}else{%>   请先<a href="register.jsp" color="red">登录</a><br>   <% }%>  <% if(session.isNew()){ %>  欢迎新用户<br/><%}else{%>   欢迎老用户  <% }  %></body></html>

注销logout.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%    session.invalidate();//彻底清除session对象    response.setHeader("refresh", "1;URL=welcome.jsp");%>




0 0
原创粉丝点击