jsp 内置对象response,request,session 以及 cookie 对象的综合例子 和 include指令

<%@ 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>Insert title here</title>  
</head>  
<body>  
     <%  
    Cookie[] cook=request.getCookies();  
     if(cook!=null){  
         for(Cookie item:cook){  
             if(item.getName().equals("uname")){  
                 //跳转  
                 session.setAttribute("uname",item.getValue() );  
                 response.sendRedirect("/Day_02shili/session/welcon.jsp");  
             }  
         }  
     }  
     %>  
    <form action="/Day_02shili/session/do.jsp" method="post">  
        用户名<input name="uname" />   
        密码 <input name="upwd" type="password" />  
        <input type="submit"/>  
    </form>  
  
</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">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>Insert title here</title>  
</head>  
<body>   
  <%  
    //设置编码格式  
    request.setCharacterEncoding("utf-8");  
  //获取 值  
     String uname=request.getParameter("uname");  
     String upwd=request.getParameter("upwd");  
     //判断 是否 有数据进来  
     if("1".equals(uname)&&"1".equals(upwd)){  
         //跳转  
         //用 session 保存值  
          session.setAttribute("uname", uname);  
           
         //使用 cookie  
         Cookie cook=new Cookie("uname",uname);  
         Cookie cookpwd=new Cookie("upwd",upwd);  
           
         cook.setMaxAge(60);  
         //用 cook 进行 响应  
         response.addCookie(cook);  
         response.addCookie(cookpwd);  
           
         request.getRequestDispatcher("/session/welcon.jsp").forward(request,response);  
           
     }else{  
         //如果失败 返回 首页  
          response.sendRedirect("/Day_02shili/session/login.jsp");  
     }  
  %>  
  
</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">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>Insert title here</title>  
</head>  
<body>  
   欢迎你<%=session.getAttribute("uname") %>  
   <hr/>  
   <a href="/Day_02shili/session/loginout.jsp">注销</a>  
</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">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>Insert title here</title>  
</head>  
<body>  
 <%  
     //清除 指定的 session 值  
     session.removeAttribute("uname");  
   //清除后 跳转到 登录页面  
   response.sendRedirect("/Day_02shili/session/login.jsp");  
 %>  
  
</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">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>Insert title here</title>  
</head>  
<body>  
   <%  
   //先判断有没有值  
   Object  uname= session.getAttribute("uname");  
  if(uname==null){  
      response.sendRedirect("/Day_02shili/session/login.jsp");  
  }  
   %>  
</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">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>图书列表 测试 include 方法</title>  
</head>  
<body>  
 <%@include file="yanZhen.jsp" %>  
  <h1>图书列表 必须登录</h1>  
</body>  
</html>  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值