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

来源:互联网 发布:淘宝账号异常申诉 编辑:程序博客网 时间:2024/05/16 08:05
<%@ 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