java web登入介面 原始碼

來源:互聯網
上載者:User

標籤:round   width   bdc   ref   util   base   otf   password   int   

大概流程:

在java web項目中 匯入sqljdbc4的包

在java Resources中完成java代碼

在webContent 下建立一個存放自己寫jsp的檔案夾

將sqljdbc4和jstl-1.2 包粘貼載WEB-INF檔案下的lib檔案下。

 

java web 登入介面原始碼:

 

1.Data_uil.java

import java.sql.*;public class Data_uil {    public  Connection getConnection()    {        try{            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");        }catch(ClassNotFoundException e)        {            e.printStackTrace();        }        String user="***";        String password="***";        String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=***";        Connection con=null;        try{            con=DriverManager.getConnection(url,user,password);        }catch(SQLException e)        {            e.printStackTrace();        }        return con;    }    public  String selectPassword(String username)    {        Connection connection=getConnection();        String sql="select *from login where username=?";        PreparedStatement preparedStatement=null;        ResultSet result=null;        String password=null;        try{            preparedStatement=connection.prepareStatement(sql);            preparedStatement.setString(1,username);                        result=preparedStatement.executeQuery();//可執行檔     查詢            if(result.next())                password=result.getString("password");                        }catch(SQLException e){            e.printStackTrace();        }finally        {            close(preparedStatement);            close(result);            close(connection);        }        System.out.println("找到的資料庫密碼為:"+password);        return password;        }    public  void close (Connection con)    {        try{            if(con!=null)            {                con.close();            }        }catch(SQLException e)            {                e.printStackTrace();            }    }    public  void close (PreparedStatement preparedStatement)    {        try{            if(preparedStatement!=null)            {                preparedStatement.close();            }        }catch(SQLException e)        {            e.printStackTrace();        }    }    public  void close(ResultSet resultSet)    {        try{            if(resultSet!=null)            {                resultSet.close();            }        }catch(SQLException e)        {            e.printStackTrace();        }    }}

 

 2.login_check.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><jsp:useBean id="util" class="util.Data_uil" scope="page" /><%        String username=(String)request.getParameter("username");    String password=(String)request.getParameter("password");    if(username==null||"".equals(username))    {        out.print("<script language=‘javaScript‘> alert(‘使用者名稱不可為空‘);</script>");        response.setHeader("refresh", "0;url=user_login.jsp");    }    else    {        System.out.println("輸入的使用者名稱:"+username);        String passwordInDataBase=util.selectPassword(username);        System.out.println("密碼:"+passwordInDataBase);                if(passwordInDataBase==null||"".equals(passwordInDataBase))        {            out.print("<script language=‘javaScript‘> alert(‘使用者名稱不存在‘);</script>");            response.setHeader("refresh", "0;url=user_login.jsp");        }        else if(passwordInDataBase.equals(password))                {                   out.print("<script language=‘javaScript‘> alert(‘登入成功‘);</script>");                   response.setHeader("refresh", "0;url=loginSucces.jsp");                }        else        {             out.print("<script language=‘javaScript‘> alert(‘密碼錯誤‘);</script>");             response.setHeader("refresh", "0;url=user_login.jsp");        }    }%></body></html>

 

3.loginSucces.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=ISO-8859-1"><title>Insert title here</title></head><body><hr size="10" width="26%" align="left" color="green">  <font size="6" color="red" >登入成功 </font>  <hr size="10" width="26%" align="left" color="green"></body></html>

 

4.user_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=ISO-8859-1"><title>登入介面</title></head><body  background="C:\Users\win8\workspace\Login\image\9dcbdc339e72a5663b5c289fb5573c13_10.jpg" >    <center>    <br><br><br><br><br><br>    <h1 style="color:yellow">Login</h1>    <br>    <form name="loginForm" action="login_check.jsp" method="post">             <table Border="0" >                    <tr >                                            <td>帳號</td>                        <td><input type="text" name="username"></td>                    </tr>                    <tr>                        <td>密碼</td>                        <td><input type="password" name="password">                        </td>                    </tr>               </table>               <br>                <input type="submit" value="登入" style="color:#BC8F8F">    </form>    </center></body></html>

 

運行結果:

 

 

 

 

 

java web登入介面 原始碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.