zoukankan      html  css  js  c++  java
  • 使用Cookie保存用户名密码,再次登陆时将Cookie用户名密码取出来并直接放置到用户名密码文本框中

         如下分为两份代码:登陆页“test_6.11_login.jsp”,登陆成功页“test_6.11_page.jsp”

         登陆页中,用户输入用户名密码,点击提交,后台对照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>

  • 相关阅读:
    微服务迁移记(七):使用docker发布 springcloud应用
    intelliJ IDEA docker部署springboot项目
    docker部署应用时超时解决
    centos下docker安装
    freemarker自定义分页(springboot、bootstrap4)
    微服务迁移记(六):集成jwt保护微服务接口安全
    微服务迁移记(五):WEB层搭建(5)-集成ueditor编辑器,伪分布式图片上传
    微服务迁移记(五):WEB层搭建(4)-简单的权限管理
    微服务迁移记(五):WEB层搭建(3)-FreeMarker集成
    WebView使用input file控件打开相册上传图片
  • 原文地址:https://www.cnblogs.com/67lmq/p/3981395.html
Copyright © 2011-2022 走看看