zoukankan      html  css  js  c++  java
  • JSP(二)

    ==========================cookie=================================
    Web服务器保存在【客户端】的一些系列【文本】信息

    作用:
        1.对特定对象的追踪
        2.统计网页浏览次数
        3.简化登录
        
    安全性:
        容易信息泄露

    语法:
        1.导包
            import="javax.servlet.http.Cookie"    【servlet是JSP的真身】
        2.创建Cookie
            Cookie newCookie=new Cookie(String key, Object value);
        3.写入Cookie:创建Cookie,★使用response的addCookie方法保存Cookie★
            response.addCookie(newCookie)
    范例1:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <%@page import="javax.servlet.http.Cookie"%>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     8 <title>演示Cookie的基本用法</title>
     9 </head>
    10 <body>
    11 <%
    12     //创建Cookie对象    Cookie 中文 jar包
    13     Cookie cookie = new Cookie("name","xijingp");
    14     //设置Cookie的生命周期为1小时
    15     cookie.setMaxAge(60*60);
    16     //将Cookie对象写入客户端
    17     response.addCookie(cookie);
    18 %>
    19 </body>
    20 </html>


    范例2:
    login.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10 <%
    11     //获得Cookie,以数组方式保存
    12     Cookie[] cookies = request.getCookies();
    13     String username = "";
    14     String password = "";
    15     //循环遍历数组,得到key为"username"和"password"
    16     for(Cookie cookie:cookies){
    17         if(cookie.getName().equals("userInfo")){//判断cookie的名称
    18             username = cookie.getValue().split("&")[0];
    19             password = cookie.getValue().split("&")[1];
    20             //实现自动登:要记得把用户名和密码保存到request对象中
    21             //request.getRequestDispatcher("doLogin.jsp");
    22         }
    23     }
    24 %>
    25     <a href="xx.jsp?ReturnUrl=http%3A%2F%2><%=request.getRequestURI()%>">当前页连接<%=request.getRequestURI()%></a>
    26     <form action="doLogin.jsp" method="post">
    27         用户名:<input type="text" name="txtName" value="<%=username %>"/><br/>
    28         密&nbsp;&nbsp;码:<input type="password" name="txtPwd" value="<%=password %>"/><br/>
    29         <input type="checkbox" value="1" name="savePass"/>记住密码<br/>
    30         <button type="submit" value="Login">登录</button>
    31     </form>
    32 </body>
    33 </html>


    doLogin.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10 <%
    11     String name = request.getParameter("txtName");
    12     String pass = request.getParameter("txtPwd");
    13     
    14     String[] savePass = request.getParameterValues("savePass");
    15     if(null ==savePass || savePass.length == 0){
    16         response.sendRedirect("login.jsp");
    17         return;
    18     }
    19     //省略判断
    20     //构建Cookie
    21     //name + "&" + pass
    22     String value = String.format("%s&%s", name, pass);
    23     Cookie cookie = new Cookie("userInfo",value);
    24     cookie.setMaxAge(60*5);
    25     response.addCookie(cookie);
    26 %>
    27 </body>
    28 </html>        

       
    ===========================session===============================
    会话:浏览器和服务器之间的通话,包含浏览器和服务器之间多次的请求、响应过程
            随着浏览器的关闭,而结束
    session(内存)对象用来存储有关用户会话的所有信息【服务器】

    session和窗口的关系
        只要浏览器不关,session始终是一个,不管存在多少窗口
        如果重开另一个浏览器,session就是另外一个

    ===========================Cookie和session区别===============================
    1.session:服务器端保存用户信息        Cookie:客户端保存用户信息
    2.session:保存对象                        Cookie:保存字符串
    3.session:随会话结束而关闭            Cookie:长期保存在客户端
    4.session:重要信息                        Cookie:不重要

    ===========================application===============================
    ”全局变量“,用于实现用户之间的数据共享

        
    ==========================include指令==================================
    将共性的内容写入一个单独的文件中,通过include指令引用该文件    








  • 相关阅读:
    postman接口测试01_下载安装
    Jenkins持续集成_05_参数化构建
    IntelliJ 中设置与Eclipse中 Ctrl+1 功能类似的快捷键
    JAVA 内部类
    Adobe Air 现在可支持X86的移动设备
    开发一些自动编写代码的工具可以增加效率吗?
    制作 Flex 悬停提示帮助文档(转)
    没什么好说的
    JDK,Tomcat,Maven,Mysql环境变量配置
    神器三、Android端神器东皇钟:天界之门
  • 原文地址:https://www.cnblogs.com/ivy-xu/p/5479540.html
Copyright © 2011-2022 走看看