zoukankan      html  css  js  c++  java
  • 模拟淘宝使用cookie记录登录名,

    <%@ 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>
    <%
    String name=null;
    Cookie[] cks = request.getCookies() ;//获得cookie的集合
    
    if(cks != null)
    {

        // 如果已经设置了cookie , 则得到它的值,将该值放在登入名文本框的value中
        //遍历
        for(Cookie c:cks)
        {
          if(c.getName().equals("name"))

          name = c.getValue();
        }

    }
    %>
    <form  action="ShoppCheck.jsp" method="post">
    用户名:<input type="text"name="name"  value="<%=name%>"><br>
    密码:&nbsp&nbsp&nbsp&nbsp<input type= "password"name="password"><br>
    <input type="submit" value="登入">
    
    </form>
    </body>
    </html>
    

      

    <%@page import="com.hanqi.web.ShoppDAO"%>
    <%@ 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>
    <%
    //设置不缓存页面
    //response.setHeader("Cache_Control", "no-cache");
    
    //接受数据
    String name =request.getParameter("name");
    String password =request.getParameter("password");
    
    if(name==null || password == null||name.equals("")||password.equals(""))
    {
    	//排除用户名和密码为null或 为空字符串的情况
    out.write("请正确登录系统");
    
    }
    else
    {
    	//检查登录信息
    	ShoppDAO  sd= new ShoppDAO ();//导包
    	if(sd.checkLogin(name, password))
    	{
    		//out.write("登录成功");
    		//无缓存的直接发送
    		//response.getWriter().write("登入成功");
    		
    		//创建cookie
    		Cookie ck= new Cookie("name", name);
    		//设置过期时间
    		ck.setMaxAge(10*24*60*60);
    		//发送
    		response.addCookie(ck);
    		
    		//创建session
    		session.setAttribute("name", name);
    		//页面跳转
    		response.sendRedirect("xtMain.jsp");
    	}
    	else
    	{
    		out.write("登入失败");
    		//2秒后跳回登入页面
    		response.setHeader("refresh", "2;URL=shoppMain.jsp");
    	}
    	
    }
    
    %>
    
    
    </body>
    </html>
    

      

    <%@page import="java.net.URLDecoder"%>
    <%@ 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>
    系统主页面
    <br>
    <%
    //检查cookie
    //获得cookie集合
    Cookie[] cks=  request.getCookies();
    for(Cookie ck: cks)
    {
    	//解码
    	out.write(ck.getName()+"="+URLDecoder.decode(ck.getValue()) +"<br>");
    }
    
    
    //判断session

        Object yonghuming = session.getAttribute("name");
        if(yonghuming==null)
         {
          out.print("回话超时或为登入");
          response.setHeader("refresh", "2;URL=shoppMain.jsp");
         }
        else
        {
          //out.print("name="+yonghuming);
          out.print(yonghuming+",欢迎登入");
     %>
      <br>
      <a href="tuichu.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.invalidate();
    out.print("退出成功");
    response.setHeader("refresh", "6;URL=shoppMain.jsp");
    %>

    </body>
    </html>
    

      

    如果两秒钟未任何操作,就会被退出,并自动返回主界面,防止利用URL访问网站。

    注意:在JSP中,所有的架包要放在WEB_INF下的lib 文件夹下,如图:

  • 相关阅读:
    MySQL客户端执行外部sql文件命令
    Java nextInt()函数
    JSP
    托管和非托管的区别。
    FTP软件Filezilla出现“读取目录列表失败”的解决办法
    Fiddler 抓包工具总结
    wampserver:Could not execute menu item.
    重装系统怎么恢复wampserver数据
    同时安装Xcode6和Xcode7导致出现N多UUID 模拟器解决办法
    打印沙漏
  • 原文地址:https://www.cnblogs.com/liuyanzeng/p/6013057.html
Copyright © 2011-2022 走看看