一个简单的登陆页面实现

来源:互联网 发布:爱因斯坦厉害知乎 编辑:程序博客网 时间:2024/05/28 15:51

 显示页面 index.jsp

 

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
--%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%
String username = (String)session.getAttribute("login");
if(session.getAttribute("login")==null){
    response.sendRedirect("login.jsp");
}

%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

    <h1>welcome <%=username%></h1>
    
    </body>
</html>

 

登陆页面login.jsp

<%@page contentType="text/html"%>
<%@page 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>JSP Page</title>
    </head>
    <body>

    <h1>请登陆...</h1>
<form name="logonForm" action="loginaction.jsp" method="post">

 <table border=0 cellpadding=5 cellspacing=0 width="100%" height="100%" >
    <tr>
         <td VALIGN="top" WIDTH="100%" class="login-background" >
        

                &nbsp;
               
                <table border=0 cellpadding=0 cellspacing=0 width="65%"  bgColor="#e8e8e8">
               
                  <tr>
                    <TD class="wpsLoginHeadText" noWrap>
                    &nbsp;欢迎,请输入您的信息。<BR><BR>
                    </TD>
                  </tr>

                      <tr>
                        <td valign=top width="33%" class="wpsLoginText" nowrap >
                          <br>
                          <label for="username" CLASS="noIndentLabel">用户标识:</label>
                          <BR>
                          <input TYPE="text" name="username" class="noIndentTextEntry" id="username"/> 
                          <BR>
                        </td>
                      </tr>
                      <tr >
                        <td valign=top width="33%" class="wpsLoginText" nowrap >
                          <br>
                          <label for="password" CLASS="noIndentLabel">密码:</label>
                          <BR>
                          <input TYPE="password" name="password" class="noIndentTextEntry" id="password"/> 
                          <BR>
                          <BR>
                          <BR>
                        </td>
                      </tr>                     
                      <tr>
                        <td valign=top class="wpsLoginText" nowrap >
                          <input TYPE="submit" name="action" class="buttons" id="other" VALUE="登录">  
                         
                        </td>
                      </tr>
                     
                </table>

          </td>
          </tr>
        </table>

</form>
   
    </body>
</html>

loginaction.jsp页面

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8" import="com.yourcom.util.UsernameAndPassword,javax.servlet.http.*" %>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
--%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<%
String username = request.getParameter("username");
String password = request.getParameter("password");
UsernameAndPassword uad = new UsernameAndPassword();
boolean hasUser = uad.getUsername(username);
if(!hasUser){
%>
<script>
    alert("没有这个用户");
    window.history.go("-1");
   
</script>   
   
<%
}else{
   
    boolean hasUsernameAndPassword = uad.getUsernameAndPassword(username,password);
    if(hasUsernameAndPassword){
        session.setAttribute("login",username);
        response.sendRedirect("index.jsp");
    }else{
%>
<script>
    alert("密码不正确!");
    window.history.go("-1");
   
</script>   
   
<%       
    }
}

%>

连接数据库DBConnection.java

public class DBConnection {
   
    /** Creates a new instance of DBConnection */
    public DBConnection() {
    }
   
    public Connection getConnection(){
        Connection conn = null;
        String username = "username";
        String password = "password";
        String url = "jdbc:oracle:thin:@localhost:1521:sid";
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            try {
                conn = DriverManager.getConnection(url,username,password);
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
       
       
                return conn;
    }
    public static void main(String args[]){
        DBConnection d = new DBConnection();
        Connection conn = d.getConnection();
        if(conn!= null){
            System.out.println("get connection successful!");
        }
    }
}

判断登陆数据库的 类 UsernameAndPassword.java

public class UsernameAndPassword {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    /** Creates a new instance of UsernameAndPassword */
    public UsernameAndPassword() {
    }
    public boolean getUsername(String username){
      
       
        String sql = "select * from login where username='"+username+"'";
        boolean hasUser = false;
        DBConnection dbc = new DBConnection();
 
       
        conn = dbc.getConnection();
        try {
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            if(rs!=null&&rs.next())
                hasUser = true;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }finally{
            close();
        }
       
        return hasUser;
    }
   
    public boolean getUsernameAndPassword(String username,String password){
        boolean hasUsernameAndPassword = false;
        String sql = "select * from login where username = '"+username+"'and password = '"+password+"'";
        DBConnection dbc = new DBConnection();
        conn = dbc.getConnection();
        try {
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            if(rs!=null&&rs.next()){
                hasUsernameAndPassword = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }finally{
            close();
        }

       
       
        return hasUsernameAndPassword;
    }
   
   public void close(){
        try {
                if(rs!=null){
                    rs.close();
                }
                if(stmt!=null){
                    stmt.close();
                }
                if(conn!=null&&!conn.isClosed()){
                    conn.close();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
        }

    }
}