java代碼怎么登錄,java登錄代碼怎么寫

Java如何登錄token的代碼是什么?

首先是Token主類。類很簡單

創(chuàng)新互聯(lián)公司是一家專注于網(wǎng)站制作、網(wǎng)站設計與策劃設計,赤坎網(wǎng)站建設哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設十載,網(wǎng)設計領(lǐng)域的專業(yè)建站公司;建站業(yè)務涵蓋:赤坎等地區(qū)。赤坎做網(wǎng)站價格咨詢:028-86922220

package com.company.util;

import java.util.ArrayList;

import javax.servlet.http.HttpSession;

public class Token {

private static final String TOKEN_LIST_NAME = "tokenList";

public static final String TOKEN_STRING_NAME = "token";

private static ArrayList getTokenList(HttpSession session) {

Object obj = session.getAttribute(TOKEN_LIST_NAME);

if (obj != null) {

return (ArrayList) obj;

} else {

ArrayList tokenList = new ArrayList();

session.setAttribute(TOKEN_LIST_NAME, tokenList);

return tokenList;

}

}

private static void saveTokenString(String tokenStr, HttpSession session) {

ArrayList tokenList = getTokenList(session);

tokenList.add(tokenStr);

session.setAttribute(TOKEN_LIST_NAME, tokenList);

}

private static String generateTokenString(){

return new Long(System.currentTimeMillis()).toString();

}

/** *//**

* Generate a token string, and save the string in session, then return the token string.

*

* @param HttpSession

* session

* @return a token string used for enforcing a single request for a particular transaction.

*/

public static String getTokenString(HttpSession session) {

String tokenStr = generateTokenString();

saveTokenString(tokenStr, session);

return tokenStr;

}

/** *//**

* check whether token string is valid. if session contains the token string, return true.

* otherwise, return false.

*

* @param String

* tokenStr

* @param HttpSession

* session

* @return true: session contains tokenStr; false: session is null or tokenStr is id not in session

*/

public static boolean isTokenStringValid(String tokenStr, HttpSession session) {

boolean valid = false;

if(session != null){

ArrayList tokenList = getTokenList(session);

if (tokenList.contains(tokenStr)) {

valid = true;

tokenList.remove(tokenStr);

}

}

return valid;

}

}

在jsp頁面端。

首先import該類:

%@ page import="com.company.util.Token" %

表單包含隱藏的token字符串:

form

input type="hidden" name="%=Token.TOKEN_STRING_NAME %" value="%=Token.getTokenString(session) %"

/form

在Servlet中添加如下代碼。

if(Token.isTokenStringValid(request.getParameter(Token.TOKEN_STRING_NAME), request.getSession())){

//進行正常業(yè)務流程

用JAVA代碼實現(xiàn)登錄名字與密碼

1.寫一個操作數(shù)據(jù)庫的通用DAO(假設為Dao.java),提供

·加載數(shù)據(jù)庫驅(qū)動和獲取數(shù)據(jù)庫連接的方法:void getConnection();

·執(zhí)行查詢的方法:ResultSet executeQuery(String sql);

·執(zhí)行更新的方法:Integer executeUpdate(String sql);

·關(guān)閉資源的方法:void releaseResource(Connection con);

2.在你的界面中把用戶名和密碼取出來

·username = yourTextField.getText();

·password = yourPasswordField.getText();

3.匹配

·驗證username、password是否是有效輸入值(例如:不能包含特殊字符,不能有注入嫌疑等)

·String sql = "select * from yourTable where username='"+username+"' and password='"+password+"'" ;

·Dao.executeQuery(sql):如果有結(jié)果說明合法,否則不合法。

不懂Connect 我

求JAVA實現(xiàn)用戶登錄界面代碼?

你要先學會截圖哦,你發(fā)的看不清楚,重新寫了一個你參考參考!

import java.awt.GridLayout;

import javax.swing.ButtonGroup;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JRadioButton;

import javax.swing.JTextField;

public class Day30A extends JFrame {

private static final long serialVersionUID = 1L;

private JLabel labelName,labelId,labelPass,labelMoney,labelSelect,labelCar;

private JComboBoxString jcb;

private JPanel jp1,jp2,jp3,jp4,jp5,jp6,jp7;

private ButtonGroup btg;

private JRadioButton jr1,jr2;

Day30A(){

this.setTitle("注冊賬戶");

this.setLayout(new GridLayout(7,1));

this.setSize(300,280);

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

init();

this.setVisible(true);

}

private void init() {

String str="卡片類型1,卡片類型2,卡片類型3,卡片類型4,卡片類型5";

jcb=new JComboBox(str.split(","));

labelId=new JLabel("賬號: ");

labelName=new JLabel("姓名: ");

labelPass=new JLabel("密碼: ");

labelMoney=new JLabel("開戶金額:");

labelSelect=new JLabel("存款類型:");

labelCar=new JLabel("卡片類型:");

addFun1();

addFun2();

}

private void addFun2() {

this.add(jp1);

this.add(jp2);

this.add(jp3);

this.add(jp4);

this.add(jp5);

this.add(jp6);

this.add(jp7);

}

private void addFun1() {

jp1=new JPanel();

jp1.add(labelId);

jp1.add(new JTextField(15));

jp2=new JPanel();

jp2.add(labelName);

jp2.add(new JTextField(15));

jp3=new JPanel();

jp3.add(labelPass);

jp3.add(new JTextField(15));

jp4=new JPanel();

jp4.add(labelMoney);

jp4.add(new JTextField(13));

jp5=new JPanel();

jp5.add(labelSelect);

btg=new ButtonGroup();

jr1=new JRadioButton("定期");

jr2=new JRadioButton("活期",true);

btg.add(jr1);

btg.add(jr2);

jp5.add(jr1);

jp5.add(jr2);

jp6=new JPanel();

jp6.add(labelCar);

jp6.add(jcb);

jp7=new JPanel();

jp7.add(new JButton("確定"));

jp7.add(new JButton("取消"));

}

public static void main(String[] args) {

new Day30A();

}

}

登陸界面的java代碼怎么寫?

概述

具體框架使用jframe,文本框組件:JTextField;密碼框組件:JPasswordField;標簽組件:JLabel;復選框組件:JCheckBox;單選框組件:JRadioButton;按鈕組件JButton。

登錄界面:

代碼實例

import javax.swing.*;

import java.awt.*; ? //導入必要的包

public class denglu extends JFrame{

JTextField jTextField ;//定義文本框組件

JPasswordField jPasswordField;//定義密碼框組件

JLabel jLabel1,jLabel2;

JPanel jp1,jp2,jp3;

JButton jb1,jb2; //創(chuàng)建按鈕

public denglu(){

jTextField = new JTextField(12);

jPasswordField = new JPasswordField(13);

jLabel1 = new JLabel("用戶名");

jLabel2 = new JLabel("密碼");

jb1 = new JButton("確認");

jb2 = new JButton("取消");

jp1 = new JPanel();

jp2 = new JPanel();

jp3 = new JPanel();

//設置布局

this.setLayout(new GridLayout(3,1));

jp1.add(jLabel1);

jp1.add(jTextField);//第一塊面板添加用戶名和文本框

jp2.add(jLabel2);

jp2.add(jPasswordField);//第二塊面板添加密碼和密碼輸入框

jp3.add(jb1);

jp3.add(jb2); //第三塊面板添加確認和取消

// ? ? ? ?jp3.setLayout(new FlowLayout()); ?//因為JPanel默認布局方式為FlowLayout,所以可以注銷這段代碼.

this.add(jp1);

this.add(jp2);

this.add(jp3); ?//將三塊面板添加到登陸框上面

//設置顯示

this.setSize(300, 200);

//this.pack();

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

this.setTitle("登陸");

}

public static void main(String[] args){

new denglu();

}

}

拓展內(nèi)容

java swing包

Swing 是一個為Java設計的GUI工具包。

Swing是JAVA基礎類的一部分。

Swing包括了圖形用戶界面(GUI)器件如:文本框,按鈕,分隔窗格和表。

Swing提供許多比AWT更好的屏幕顯示元素。它們用純Java寫成,所以同Java本身一樣可以跨平臺運行,這一點不像AWT。它們是JFC的一部分。它們支持可更換的面板和主題(各種操作系統(tǒng)默認的特有主題),然而不是真的使用原生平臺提供的設備,而是僅僅在表面上模仿它們。這意味著你可以在任意平臺上使用JAVA支持的任意面板。輕量級組件的缺點則是執(zhí)行速度較慢,優(yōu)點就是可以在所有平臺上采用統(tǒng)一的行為。

概念解析:

JFrame?– java的GUI程序的基本思路是以JFrame為基礎,它是屏幕上window的對象,能夠最大化、最小化、關(guān)閉。

JPanel?– Java圖形用戶界面(GUI)工具包swing中的面板容器類,包含在javax.swing 包中,可以進行嵌套,功能是對窗體中具有相同邏輯功能的組件進行組合,是一種輕量級容器,可以加入到JFrame窗體中。。

JLabel?– JLabel 對象可以顯示文本、圖像或同時顯示二者??梢酝ㄟ^設置垂直和水平對齊方式,指定標簽顯示區(qū)中標簽內(nèi)容在何處對齊。默認情況下,標簽在其顯示區(qū)內(nèi)垂直居中對齊。默認情況下,只顯示文本的標簽是開始邊對齊;而只顯示圖像的標簽則水平居中對齊。

JTextField?–一個輕量級組件,它允許編輯單行文本。

JPasswordField?– 允許我們輸入了一行字像輸入框,但隱藏星號(*) 或點創(chuàng)建密碼(密碼)

JButton?– JButton 類的實例。用于創(chuàng)建按鈕類似實例中的 "Login"。

用java程序編寫一個簡單的登錄界面怎么寫?

程序如下:

mport java.awt.HeadlessException;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.ImageIcon;

import javax.swing.JButton;

@SuppressWarnings("serial")

public class MainFrame extends JFrame {

JLabel lbl1 = new JLabel("用戶名:");

JLabel lbl2 = new JLabel("密 ? ? 碼:");

JTextField txt = new JTextField("admin",20);

JPasswordField pwd = new JPasswordField(20);

JButton btn = new JButton("登錄");

JPanel pnl = new JPanel();

private int error = 0;

public MainFrame(String title) throws HeadlessException {

super(title);

init();

}

private void init() {

this.setResizable(false);

pwd.setEchoChar('*');

pnl.add(lbl1);

pnl.add(txt);

btn.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

if ("admin".equal花憨羔窖薏忌割媳公顱s(new String(pwd.getPassword()))){

pnl.removeAll();

JLabel lbl3 = new JLabel();

ImageIcon icon = new ImageIcon(this.getClass().getResource("pic.jpg"));

lbl3.setIcon(icon);

pnl.add(lbl3);

}

else{

if(error 3){

JOptionPane.showMessageDialog(null,"密碼輸入錯誤,請再試一次");

error++;

}

else{

JOptionPane.showMessageDialog(null,"對不起,您不是合法用戶");

txt.setEnabled(false);

pwd.setEnabled(false);

btn.setEnabled(false);

}

}

}

});

}

public static void main(String[] args) {

MainFrame frm = new MainFrame("測試");

frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frm.setBounds(100, 100, 300, 120);

frm.setVisible(true);

}

}

編程的注意事項:

1、Java是一門面向?qū)ο缶幊陶Z言,不僅吸收了C++語言的各種優(yōu)點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特征。Java語言作為靜態(tài)面向?qū)ο缶幊陶Z言的代表,極好地實現(xiàn)了面向?qū)ο罄碚摚试S程序員以優(yōu)雅的思維方式進行復雜的編程。

2、 Java具有簡單性、面向?qū)ο蟆⒎植际?、健壯性、安全性、平臺獨立與可移植性、多線程、動態(tài)性等特點。Java可以編寫桌面應用程序、Web應用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應用程序等。

3、2006年11月13日,Java技術(shù)的發(fā)明者Sun公司宣布,將Java技術(shù)作為免費軟件對外發(fā)布。Sun公司正式發(fā)布的有關(guān)Java平臺標準版的第一批源代碼,以及Java迷你版的可執(zhí)行源代碼。從2007年3月起,全世界所有的開發(fā)人員均可對Java源代碼進行修改。

java用數(shù)組的登錄的代碼怎么寫

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

int num = 5;

int[] n = new int[num];

Scanner sc = new Scanner(System.in);

System.out.println("請輸入"+num+"位會員的積分");

for(int i=0;inum;i++){

System.out.print("第"+(i+1)+"位會員積分:");

n[i]=sc.nextInt();

}

System.out.println("\n序號\t歷史積分");

for(int i=0;in.length;i++){

System.out.println(i+1+"\t"+n[i]);

}

sc.close();

}

}

分享題目:java代碼怎么登錄,java登錄代碼怎么寫
文章路徑:http://muchs.cn/article48/phgphp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)響應式網(wǎng)站、云服務器、網(wǎng)站改版Google、全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站