zoukankan      html  css  js  c++  java
  • Cookie的应用

    作用:在浏览器当中用cookie来保存参数,比如实现登录功能时用来保存账号

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'Login.jsp' starting page</title>
        
    	<meta http-equiv="pragma" content="no-cache">
    	<meta http-equiv="cache-control" content="no-cache">
    	<meta http-equiv="expires" content="0">    
    	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    	<meta http-equiv="description" content="This is my page">
    	<!--
    	<link rel="stylesheet" type="text/css" href="styles.css">
    	-->
    
      </head>
      
      <body>
      <%
      String loginname = "";
      Cookie[] cookies = request.getCookies();
      		if(cookies!=null){
      			for(Cookie cookie : cookies){
      	  			if(cookie.getName().equals("loginname")){
      	  				loginname=cookie.getValue();
      	  				break;
      	  			}
      	  		}
      		}
      		
      %>
      
       <form action="loginapp.jsp" method="post">
       
       用户名 :<input type=text name="username" value="<%=loginname%>" > <br><br>
                    <input type="submit" value="提交">
       </form>
      </body>
    </html>
    
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'Loginapp.jsp' starting page</title>
        
    	<meta http-equiv="pragma" content="no-cache">
    	<meta http-equiv="cache-control" content="no-cache">
    	<meta http-equiv="expires" content="0">    
    	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    	<meta http-equiv="description" content="This is my page">
    	<!--
    	<link rel="stylesheet" type="text/css" href="styles.css">
    	-->
    
      </head>
      
      <body>
       
       <%
       String username = request.getParameter("username");
       
       Cookie c = new Cookie("loginname",username);
       
       response.addCookie(c);
       %>
       
      </body>
    </html>
    
  • 相关阅读:
    SpringMVC中请求路径参数使用正则表达式
    SpringBoot单元测试示例2
    数据结构与算法之——八大排序算法
    linux学习之centos(二):虚拟网络三种连接方式和SecureCRT的使用
    linux学习之centos(一):在VMware虚拟机中安装centos6.5
    网易云课堂学习之VS相关
    emplace_back减少内存拷贝和移动
    Lepus经历收获杂谈(一)——confirm features的小工具
    MDM平台学习笔记
    四大开源协议:BSD、Apache、GPL、LGPL
  • 原文地址:https://www.cnblogs.com/houjiie/p/6221155.html
Copyright © 2011-2022 走看看