zoukankan      html  css  js  c++  java
  • cookie保存用户名及密码

      登陆页中,用户输入用户名密码,点击提交,后台对照mysq数据库中,看是否有对应的用户名,以及密码是否正确。如果正确

    则将用户名密码分两份Cookie保存。页面跳转到登陆成功页。

        用户再次访问登陆页时,先取出Cookie,判断是否存在用户名密码的Cookie,如果有,则将值保存在变量或者request中。接着

    将Cookie的变量或request赋值给用户名密码文本输入框。到此完成了保存用户名密码的功能。

       test_6.11_login.jsp

    <%@ page language="java" import="java.util.*,java.sql.*,java.net.*" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>

    <html>
    <head> 
    </head>

    <body>
    <%! 
    //定义数据库驱动程序 
    public static final String DBDRIVER="org.gjt.mm.mysql.Driver"; 
    public static final String DBURL="jdbc:mysql://localhost:3307/yunmobile"; 
    public static final String DBUSER="rood"; 
    public static final String DBPASS="234567"; 
    %>
    <%
    Connection conn=null;
    PreparedStatement pstmt=null;
    ResultSet rs=null;
    %>
    <%
    String username_ = null;
    Cookie c[]=request.getCookies();
    if(c!=null)
    {
    for(int x=0;x<c.length;x++)
    {
    if(c[x].getName().equals("username"))
    {
    //在cookie值保存时如果编码了,取cookie时就需要进行解码。
    //将cookie值取出来后,赋值给变量,用以之后的显示
    username_ = URLDecoder.decode(c[x].getValue(),"UTF-8"); 
    }
    else if(c[x].getName().equals("password"))
    {
    //将cookie值取出来后,赋值给request,用以之后的显示
    request.setAttribute("password",c[x].getValue());
    }

    }
    %>

    <form action="test_6.11_login.jsp" method="post" name="myform">
    <!-- 实话说,下面的用户名和密码value的赋值,纠缠了好久。俩值一个用变量,一个用request保存 -->
    用户名:<input type="text" id="userNameId" name="username" value="<%=username_ %>"/><br/>
    <!-- ${}是显示变量password的值,如果值是null则不显示,非null则显示具体值 -->
    密码:<input type="password" id="passWordId" name="password" value="${password}"/><br/>
    <input type="submit" value="提交"/>
    <input type="reset" value="重置"/>
    </form>


    <% 
    //若用户名密码中有中文,虽然提交给当前页,底下的设置编码方式也是必须的
    request.setCharacterEncoding("UTF-8");
    String name=request.getParameter("username");
    String password=request.getParameter("password");
    if(!(name==null||name.equals("")||password==null || password.equals("")))
    {
    try{
    Class.forName(DBDRIVER);
    conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
    String sql="SELECT * FROM member WHERE name=? AND password=?";
    pstmt=conn.prepareStatement(sql);
    pstmt.setString(1,name);
    pstmt.setString(2,password);
    rs=pstmt.executeQuery();
    if(rs.next())
    {
    if(rs.getString(3).equals(name) && rs.getString(2).equals(password))
    {
    //如果用户名密码和数据库中值匹配,则跳转到登陆成功页
    response.setHeader("refresh","3;URL=test_6.11_page.jsp");
    //登陆成功页面通过session传递用户名,用于登陆成功页面显示用户名
    session.setAttribute("username",name); 
    //cookie保存时,如果用户名或密码有汉字或者其他特殊字符需要进行编码
    Cookie c1=new Cookie("username",URLEncoder.encode(name,"UTF-8"));
    Cookie c2=new Cookie("password",password);
    c1.setMaxAge(76000);
    c2.setMaxAge(76000);
    response.addCookie(c1);
    response.addCookie(c2); 

    }
    else
    {
    out.println("<h2>用户名或密码错误哦哦哦!</h2>");
    }
    }catch(Exception e){
    out.println(e);
    }

    %>
    </body>
    </html>

    test_6.11_page.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
    <html>
    <head><body> 
    <% 
    if(!(session.getAttribute("username")==null))
    {
    %>
    <h3>欢迎<%=session.getAttribute("username") %>光临本系统</h3>
    <%
    }
    else
    {
    %>
    <h3>请先进行系统的<a href="login_6.11_login.jsp">登陆</a>!</h3>
    <%} %>
    </body>
    </html>

  • 相关阅读:
    【转载】Linux下各文件夹的含义和用途
    【转载】Linux 通过mount -o loop 配置本地.iso镜像为yum源(yum仓库)
    Fedora 和 RedHat 以及 SUSE 中 YUM 工具的使用
    【转】下载对应内核版本的asmlib
    【转】VMWare vCenter 6.0安装配置
    【转】在VMware中为Linux系统安装VM-Tools的详解教程
    【转】虚拟化(五):vsphere高可用群集与容错
    html拼接时onclick事件传递json对象
    bootstrap table 解析写死的json.并且把进度条放进列中。
    开发规范实体和值对象
  • 原文地址:https://www.cnblogs.com/frankzone/p/7881398.html
Copyright © 2011-2022 走看看