• JSP实现用户登录样例


      业务描述

      用户在login.jsp页面输入用户名密码登录:

      如果用户名为xingoo,密码为123,则跳转到成功界面login_success.jsp,并显示用户登录的名字;

      如果用户名密码错误,则跳转到失败界面login_failure.jsp,并提示返回登录界面。

      login.jsp代码

    <%@ 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>用户登录</title>
    </head>
    <body>
        <h1>用户登录</h1>
        <hr>
        <form name="regForm" action="doLogin.jsp" method="post">
            <table>
                <tr>
                    <td>username</td>
                    <td><input type="text" name="username"/></td>
                </tr>
                <tr>
                    <td>password</td>
                    <td><input type="password" name="password"/></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" value="submit"/></td>
                </tr>
            </table>
        </form>
    </body>
    </html>

      dologin.jsp处理代码

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <%
        String username = "";
        String password = "";
        
        request.setCharacterEncoding("utf-8");
        
        username = request.getParameter("username");
        password = request.getParameter("password");
        
        if("xingoo".equals(username)&&"123".equals(password)){
            session.setAttribute("loginUser",username);
            request.getRequestDispatcher("login_success.jsp").forward(request,response);
        }else{
            response.sendRedirect("login_failure.jsp");
        }
    %>

      login_success.jsp用户登录成功界面

    <%@ 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>用户登录</title>
    </head>
    <body>
        <h1>用户登录</h1>
        <hr>
        欢迎您!<%=session.getAttribute("loginUser") %>
    </body>
    </html>

      login_failure.jsp用户登录失败界面

    <%@ 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>用户登录</title>
    </head>
    <body>
        <h1>用户登录</h1>
        <hr>
        登录失败!<a href="login.jsp">返回登录</a>
    </body>
    </html>
  • 相关阅读:
    编译原理入门以及战大作业心得(2)汇编简易入门 康某
    简易聊天对话框(源码)
    用js做数字字母混合的随机四位验证码
    HTML5基础
    javascript基础
    jquery.AutoComplete 仿百度文本框感应
    DropDownlist显示树状
    利用 System.Net.Mail 实现邮件发送功能
    sql 根据字段值,查找属于哪个表中的哪个字段
    sql 查看数据库中的各表的大小
  • 原文地址:https://www.cnblogs.com/xing901022/p/4356135.html
Copyright © 2020-2023  润新知