码迷,mamicode.com
首页 > Web开发 > 详细

jsp内置对象(二):response

时间:2015-07-31 20:21:05      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:jsp

一:session获取用户的ID

<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">

<html>

 <head>

   <title>My JSP ‘Demo1.jsp‘ starting page</title>

 </head>

 <body>

  <h1>获取用户的ID</h1>

   <%

      String id=session.getId();

   %>

   <h3>id:<%=id %></h3>

   <h3>id的长度:<%=id.length() %></h3>

 </body>

</html>

 

二:session编写的用户登录程序

Login.jsp

<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">

<html>

 <head>

   <title>My JSP ‘Login.jsp‘ starting page</title>

 </head>

 <body>

   <form action="Login.jsp" method="post">

      用户名:<input type="text" name="uname"><br>

      密&nbsp;&nbsp;码:<input type="password"name="pname"><br>

      <input type="submit"value="登录">

      <input type="reset"  value="重置">

   </form>

   <%

      Stringname=request.getParameter("uname");

      Stringpassword=request.getParameter("pname");

      if(!(name==null||"".equals(password))){

         if("Spring".equals(name)&&"123".equals(password)){

            response.setHeader("refresh","2;URL=welcome.jsp");

            session.setAttribute("userid",name);

   %>

   <h3>登录成功,两秒跳到欢迎页</h3>

   <h3>如果没有跳转,请点击<a href="welcome.jsp">这里</a></h3>

   <%

         }else{

   %>

   <h3>程序发生了错误</h3>

   <%

         }

      }

   %>

 </body>

</html>

 

welcome.jsp

<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">

<html>

 <head>

   <title>My JSP ‘welcome.jsp‘ starting page</title>

 </head>

 <body>

  <%

     if(session.getAttribute("userid")!=null){

  %>

  <h3>欢迎进入本页:<%=session.getAttribute("userid")%></h3>

  <%

     }else{

  %>

  <h3>登录错误,<a href="Login.jsp">重新登录</a></h3>

  <%

     }

  %>

 </body>

</html>

 

loginout.jsp

<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">

<html>

 <head>

   <title>My JSP ‘loginout.jsp‘ starting page</title>

 </head>

 <body>

   <%

      response.setHeader("refresh","2;url=Login.jsp");

      session.invalidate();//注销

   %>

   <h3>成功退出本系统</h3>

   <h3><a href="Login.jsp">没有跳转点击这里</a></h3>

 </body>

</html>

 

三:判断新老用户

<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">

<html>

 <head>

   <title>My JSP ‘is_New.jsp‘ starting page</title>

 </head>

 

 <body>

  <%

     if(session.isNew()){

  %>

  <!-- 用户第一次登录 -->

  <h3>欢迎光临</h3>

  <%

     }else{

  %>

  <h3>欢迎回来</h3>

  <%

     }

  %>

 </body>

</html>

四:获取用户在线时间

<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">

<html>

 <head>

   <title>My JSP ‘getTime.jsp‘ starting page</title>

 </head>

 

 <body>

 <%

  //获取创建时间

  long start=session.getCreationTime();

   //获取最后操作时间

  long end=session.getLastAccessedTime();

  long time=(end-start)/1000;

 %>

 <h3>在线时间:<%=time %></h3>

 </body>

</html>

 

版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21

jsp内置对象(二):response

标签:jsp

原文地址:http://blog.csdn.net/dzy21/article/details/47174539

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!