當前位置:首頁 » 編程語言 » java驗證登陸

java驗證登陸

發布時間: 2023-09-19 15:21:48

java登錄模塊驗證出現問題求解答

前期准備
首先要先明確有個大體的思路,要實現什麼樣的功能,了解完成整個模塊要運用到哪些方面的知識,以及從做的過程中去發現自己的不足。技術方面的進步大都都需要從實踐中出來的。
功能:用戶注冊功能+系統登錄功能+生成驗證碼
知識:窗體設計、資料庫設計、JavaBean封裝屬性、JDBC實現對資料庫的連接、驗證碼(包括彩色驗證碼)生成技術,還有就些比如像使用正則表達式校驗用戶注冊信息、隨機獲得字元串、對文本可用字元數的控制等
設計的模塊預覽圖:

使用BoxLayout布局,將控制項排列方式設置從上至下:

復制代碼代碼如下:

contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.PAGE_AXIS));

窗體使用了標簽、文本域、密碼域和按鈕等控制項
實現代碼:

public class login extends JFrame{
private static final long serialVersionUID = -4655235896173916415L;
private JPanel contentPane;
private JTextField usernameTextField;
private JPasswordField passwordField;
private JTextField validateTextField;
private String randomText;
public static void main(String args[]){
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable(){
public void run(){
try{
login frame=new login();
frame.setVisible(true);
}catch(Exception e){
e.printStackTrace();
}
}
});

}
public login(){
setTitle("系統登錄");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane=new JPanel();
setContentPane(contentPane);
contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.PAGE_AXIS));

JPanel usernamePanel=new JPanel();
contentPane.add(usernamePanel);

JLabel usernameLable=new JLabel("u7528u6237u540DuFF1A");
usernameLable.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
usernamePanel.add(usernameLable);

usernameTextField=new JTextField();
usernameTextField.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
usernamePanel.add(usernameTextField);
usernameTextField.setColumns(10);

JPanel passwordPanel = new JPanel();
contentPane.add(passwordPanel);
JLabel passwordLabel = new JLabel("u5BC6 u7801uFF1A");
passwordLabel.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
passwordPanel.add(passwordLabel);
passwordField = new JPasswordField();
passwordField.setColumns(10);
passwordField.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
passwordPanel.add(passwordField);
JPanel validatePanel = new JPanel();
contentPane.add(validatePanel);
JLabel validateLabel = new JLabel("u9A8Cu8BC1u7801uFF1A");
validateLabel.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
validatePanel.add(validateLabel);
validateTextField = new JTextField();
validateTextField.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
validatePanel.add(validateTextField);
validateTextField.setColumns(5);
randomText = RandomStringUtils.randomAlphanumeric(4);
CAPTCHALabel label = new CAPTCHALabel(randomText);//隨機驗證碼
label.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
validatePanel.add(label);

JPanel buttonPanel=new JPanel();
contentPane.add(buttonPanel);

JButton submitButton=new JButton("登錄");
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
do_submitButton_actionPerformed(e);
}
});
submitButton.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
buttonPanel.add(submitButton);

JButton cancelButton=new JButton("退出");
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
do_cancelButton_actionPerformed(e);
}
});
cancelButton.setFont(new Font("微軟雅黑",Font.PLAIN,15));
buttonPanel.add(cancelButton);

pack();// 自動調整窗體大小
setLocation(com.lixiyu.util.SwingUtil.centreContainer(getSize()));// 讓窗體居中顯示

}

窗體居中顯示:

public class SwingUtil {
/*
* 根據容器的大小,計算居中顯示時左上角坐標
*
* @return 容器左上角坐標
*/
public static Point centreContainer(Dimension size) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();// 獲得屏幕大小
int x = (screenSize.width - size.width) / 2;// 計算左上角的x坐標
int y = (screenSize.height - size.height) / 2;// 計算左上角的y坐標
return new Point(x, y);// 返回左上角坐標
}
}

1.2獲取及繪制驗證碼

public class CAPTCHALabel extends JLabel {
private static final long serialVersionUID = -963570191302793615L;
private String text;// 用於保存生成驗證圖片的字元串
public CAPTCHALabel(String text) {
this.text = text;
setPreferredSize(new Dimension(60, 36));// 設置標簽的大小
}
@Override
public void paint(Graphics g) {
super.paint(g);// 調用父類的構造方法
g.setFont(new Font("微軟雅黑", Font.PLAIN, 16));// 設置字體
g.drawString(text, 5, 25);// 繪制字元串
}
}

*彩色驗證碼:

public class ColorfulCAPTCHALabel extends JLabel {
private static final long serialVersionUID = -963570191302793615L;
private String text;// 用於保存生成驗證圖片的字元串
private Color[] colors = { Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,
Color.PINK, Color.RED, Color.WHITE, Color.YELLOW };// 定義畫筆顏色數組
public ColorfulCAPTCHALabel(String text) {
this.text = text;
setPreferredSize(new Dimension(60, 36));// 設置標簽的大小
}
@Override
public void paint(Graphics g) {
super.paint(g);// 調用父類的構造方法
g.setFont(new Font("微軟雅黑", Font.PLAIN, 16));// 設置字體
for (int i = 0; i < text.length(); i++) {
g.setColor(colors[RandomUtils.nextInt(colors.length)]);
g.drawString("" + text.charAt(i), 5 + i * 13, 25);// 繪制字元串
}
}
}

1

㈡ java 怎麼驗證用戶名和密碼

1,建一個資料庫:是有用戶名與密碼的。
2,java中,通過建立一個類,比如說:DatabaseConnector,用於與資料庫進行連接。
3,類建好後,如果你用的是mysql資料庫的話,還需要再java中載入相應的jar包(mysql-connector-java-3.0.jar),其他資料庫也需要載入相應的包。
4.用戶名與密碼,放於資料庫中一個表中,當用戶登錄時,在資料庫該表中查找是否有用戶名與密碼與所給相同的欄位。如有,登錄成功,跳轉到相應頁面。如無,登錄失敗。

㈢ java怎樣實現登錄驗證

  • 1.打開編程工具:

    打開java編程的界面,採用的是eclipse軟體;

㈣ java web 驗證用戶是否已登錄有哪些方式。

首先,你的web需要有登入後將登錄信息保存到session的過程,在用戶訪問那些需要登錄的頁面或者action的時候(可以用過過濾器,或者直接在相應的頁面判斷),從session裡面獲取登錄信息,如果沒有,則說明沒有登錄.跳轉到登錄頁面後,登陸成功,保存登錄信息到session,這個時候再訪問原先的,就可以通過驗證..一整個流程大概就是這樣.重點就是登錄信息保存在session裡面,驗證用戶是否登錄,就依據這個了

㈤ java 登陸時的驗證碼怎麼做

後台寫一個生成圖片隨機的代碼,生成圖片給前台。切換圖片的時候,使用ajax獲取圖片數據就行。
附上生成圖片的代碼
public class ValidateCode {

private int width=180;
private int height=60;
private int codeCount = 4;
private int x = 0;
private int codeY;
private String Code;
private BufferedImage buffImg;
static char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
private int fontHeight;

public ValidateCode() {
x = width / (codeCount + 2);
fontHeight = height - 2;
codeY = height - 4;
CreateCode();
}

public void CreateCode(){

// 定義圖像buffer
BufferedImage buffImg = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
// 創建一個隨機數生成器類
Random random = new Random();

// 將圖像填充為白色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);

// 創建字體,字體的大小應該根據圖片的高度來定。
Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);
// 設置字體。
g.setFont(font);

// 畫邊框。
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1, height - 1);

// randomCode用於保存隨機產生的驗證碼,以便用戶登錄後進行驗證。
StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0;

// 隨機產生codeCount數字的驗證碼。
for (int i = 0; i < codeCount; i++) {
// 得到隨機產生的驗證碼數字。
String strRand = String.valueOf(codeSequence[random.nextInt(62)]);
// 產生隨機的顏色分量來構造顏色值,這樣輸出的每位數字的顏色值都將不同。
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);

// 用隨機產生的顏色將驗證碼繪制到圖像中。
g.setColor(new Color(red, green, blue));
g.drawString(strRand, (i ) * x+20, codeY);

// 將產生的四個隨機數組合在一起。
randomCode.append(strRand);
}
this.Code=randomCode.toString().toUpperCase();
this.buffImg=buffImg;

}

public String getCode() {
return Code;
}

public void setCode(String code) {
Code = code;
}

public BufferedImage getBuffImg() {
return buffImg;
}

public void setBuffImg(BufferedImage buffImg) {
this.buffImg = buffImg;
}
}

㈥ java鏈接mysql資料庫實現登陸如何驗證

//這是我以前寫的核對資料庫實現登陸的方法,你只看jdbc部分就好,我還特地給你加了點注釋x0dx0aString sql = "select username,password from account";x0dx0aString user = request.getParameter("user");x0dx0aString pass = request.getParameter("password");x0dx0aint j = 0;x0dx0aConnection conn = null;x0dx0aPreparedStatement ps = null;x0dx0aResultSet rs = null;x0dx0atry {x0dx0aconn = JDBCTools1.getConnection();x0dx0aps = conn.prepareStatement(sql);x0dx0ars = ps.executeQuery();x0dx0a//從表中查詢獲取所有賬戶的用戶名&密碼的ResultSet 對象x0dx0awhile(rs.next()){x0dx0aint i = 0;x0dx0ax0dx0aString username[] = new String[10];//用戶名數組x0dx0aString password[] = new String[10];//密碼數組x0dx0ausername[i] = rs.getString(1);x0dx0apassword[i] = rs.getString(2);x0dx0aif(user.equals(username[i])&&pass.equals(password[i])){//比對x0dx0aresponse.getWriter().print("you are welcome!");x0dx0aj++;x0dx0a}else if(user.equals(username[i])&&!pass.equals(password[i])){x0dx0aresponse.getWriter().println("the realy password is :"+ username[i] +","+password[i]+"\r\n");x0dx0aresponse.getWriter().println("and you password is :"+user +","+pass+" :so the username or password may not right");x0dx0aj++;x0dx0a}else{x0dx0acontinue;x0dx0a}x0dx0ai++;x0dx0a}x0dx0aif(j == 0){x0dx0aresponse.getWriter().println("Your username may not be properly");x0dx0a}x0dx0a} catch (Exception e) {x0dx0ae.printStackTrace();x0dx0a}finally{x0dx0aJDBCTools1.release(rs, ps, conn);x0dx0a}x0dx0a//這是我JDBCTools的getConnection方法x0dx0agetConnection{x0dx0aString driverClass = oracle.jdbc.driver.OracleDriver;x0dx0aString jdbcUrl = jdbc:oracle:thin:@localhost:1521:orcl;x0dx0a//你的資料庫的用戶名密碼x0dx0aString user = null;x0dx0aString password = null;x0dx0a// 通過反射創建Driver對象x0dx0aClass.forName(driverClass);x0dx0areturn DriverManager.getConnection(jdbcUrl, user, password);}x0dx0a//這是我JDBCTools的release方法x0dx0apublic static void release(ResultSet rs, Statement statement,x0dx0aConnection conn) {x0dx0aif (rs != null) {x0dx0atry {x0dx0ars.close();x0dx0a} catch (SQLException e) {x0dx0ae.printStackTrace();x0dx0a}x0dx0a}x0dx0ax0dx0aif (statement != null) {x0dx0atry {x0dx0astatement.close();x0dx0a} catch (Exception e2) {x0dx0ae2.printStackTrace();x0dx0a}x0dx0a}x0dx0ax0dx0aif (conn != null) {x0dx0atry {x0dx0aconn.close();x0dx0a} catch (Exception e2) {x0dx0ae2.printStackTrace();x0dx0a}x0dx0a}x0dx0a}

熱點內容
安卓什麼軟體測試手機電池 發布:2025-02-02 04:28:52 瀏覽:991
手機上傳快 發布:2025-02-02 04:27:46 瀏覽:305
電腦配置詳解圖解都有哪些 發布:2025-02-02 04:26:27 瀏覽:713
景區應該有什麼配置 發布:2025-02-02 04:09:08 瀏覽:119
c語言與java工作 發布:2025-02-02 03:59:57 瀏覽:282
qq買什麼不要支付密碼 發布:2025-02-02 03:50:29 瀏覽:495
android讀取視頻 發布:2025-02-02 03:46:57 瀏覽:826
手機號序列碼的密碼在哪裡 發布:2025-02-02 03:29:34 瀏覽:878
安卓怎麼換回鴻蒙系統 發布:2025-02-02 03:24:35 瀏覽:513
完美國際鄰水鎮箱子密碼是多少 發布:2025-02-02 03:17:04 瀏覽:625