一个简单的登陆页面实现

 显示页面 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();
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值