jsp 内置对象response,request,session 以及 cookie 对象的综合例子 和 include指令
2017-05-10 14:06
956 查看
<%@ 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>
相关文章推荐
- jsp 内置对象response,request,session 以及 cookie 对象的综合例子 和 include指令
- JSP九大内置对象---Request,Response,Out,Session,Application,Cookie,Config,Page,Exception
- jsp内置对象out、request、response、session、application五大常用对象以及Page、pageContext、exception、config
- JSP九大内置对象(Request,Response,Out,Session,Application,Cookie,Config,Page,Exception)
- JSP内置对象--pageContent,request,response,session,application,config,out,page,exception
- jsp九大内置对象:request、response、session、application、out、page、pageContext、config、exception
- C#-WebForm-★内置对象简介★Request-获取请求对象、Response相应请求对象、Session全局变量(私有)、Cookie全局变量(私有)、Application全局公共变量、ViewState
- JSP内置对象的使用 request,response,session(一)
- 关于JSP中request,response,session三个内置对象的常用方法
- jsp的九大内置对象和四大作用域以及request和session的区别
- JSP 内置对象(request response session application out pageContext)
- 关于jsp内置对象Session和Cookie使用的肤浅理解,如何使用Session来统计网站在线人数
- JSP内置对象:request和response
- jsp中的内置对象Cookie与session的比较
- JSP九大内置对象对应的JSP四大作用域以及session的生命周期
- application、session、request、page 4个JSP内置对象详细介绍
- JSP三大常用对象request、response、session
- JSP内置对象session和request中setAttribute方法
- JSP页面中的标签以及内置对象request等
- JSP内置对象response与session 基础知识复习