• java web module of login


    Reffer to the book<java web整合开发王者归来>.

    It's jsp page.

    Offer the values of username and password to servlet.

    Then save the session.

    The code is simple.

    easy to read.

    Except the myLogin.jsp,also need a javabean Person.java.

    <%@ page language="java" pageEncoding="UTF-8"%>
    <jsp:directive.page import="com.helloweenvsfei.sessionWeb.bean.Person" />
    <jsp:directive.page import="java.text.SimpleDateFormat" />
    <jsp:directive.page import="java.text.DateFormat" />
    <jsp:directive.page import="java.util.Date" />
    <%!
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    %>
    <%
        response.setCharacterEncoding("UTF-8");
    
        Person[] persons = {
            new Person("linghuchong", "linghuchong", 34, dateFormat.parse("1982-01-01")), 
            new Person("Hello Kitty", "hellokitty", 23, dateFormat.parse("1984-02-25")),
            new Person("renyingying", "renyingying", 23, dateFormat.parse("1994-09-12")),
        };
        
        String message = "";
        
        if(request.getMethod().equals("POST"))
        {
            for(Person person : persons)
            {
                if(person.getName().equalsIgnoreCase(request.getParameter("username"))
                        && person.getPassword().equals(request.getParameter("password")))
                {
                    session.setAttribute("person", person);
                    session.setAttribute("loginTime", new Date());
                    response.sendRedirect(request.getContextPath() + "/welcome.jsp");
                    return;
                }
            }
            // login failed
            message = "wrong username or pwd,login failed";
        }
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Please login first</title>
    </head>
    <body>
        <form action="myLogin.jsp" method="post">
            <table>
                <% if( ! message.equals("")){ %>
                <tr>
                    <td></td>
                    <td>login failed!</td>
                </tr>
                <% } %>
                <tr>
                    <td>username:</td>
                    <td><input type="text" name="username" style="200px; ">
                    </td>
                </tr>
                <td>password:</td>
                <td><input type="password" name="password" style="200px; ">
                </td>
                <tr>
                    <td></td>
                    <td><input type="submit" value=" Login " class="button">
                    </td>
                </tr>
            </table>
        </form>
        Hello Kitty, hellokitty
    </body>
    </html>
  • 相关阅读:
    SHELL
    Docker
    RHCE内容记要
    mysql基本知识的总结
    Linux启动提示Kernel panic
    配置tomcat、nginx实现反向代理(需操作)
    linux下nginx的安装和配置
    linux下安装mysql5.7(centos6.0)
    linux打包解压包(.tar .gz .tar.gz .zip)
    多重继承下的类型转换
  • 原文地址:https://www.cnblogs.com/rixiang/p/5014163.html
Copyright © 2020-2023  润新知