新聞中心
java注冊(cè)用戶名不能重復(fù)代碼
首先,我們得明白用戶登錄使用什么登陸的,即用戶在線的原理。這只是將用戶的對(duì)象存放在了session中而已,然后再frame中進(jìn)行調(diào)用,其他特定頁(yè)面也進(jìn)行直接引用就行。那么實(shí)現(xiàn)“擠下來”的功能就是讓新生成的session有效,讓原來存放用戶的session失效就行。到此,大體思路已經(jīng)有了。那怎么實(shí)現(xiàn)呢?
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站建設(shè)、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的赤城網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
想要知道怎么實(shí)現(xiàn),就必須要明白session存放用戶對(duì)象的過程了。在用戶登錄之后,我們可以得到用戶的對(duì)象user,而存放到session中需要執(zhí)行session.setAttribute(key,value); 我們將用戶的userId或是其他的唯一標(biāo)識(shí)存為key,將用戶對(duì)象存為值。這樣就能隨時(shí)隨地調(diào)用唯一用戶了。user存放的問題解決了,那相同 登錄 時(shí)session廢除的問題呢?
?
這個(gè)其實(shí)也不難,我們可以更具session的特性一樣,用map進(jìn)行存貯,將用戶的標(biāo)識(shí)存為key,而將其所對(duì)應(yīng)的session存為value,那么當(dāng)重復(fù)用戶登錄時(shí),只需要取出對(duì)應(yīng)的session,將其invalidate就行了。
至此,實(shí)現(xiàn)思路已經(jīng)明了,聒噪了這么久,大家都急不可耐地想看代碼了吧?以下是代碼:
前置準(zhǔn)備,jsp界面
界面很簡(jiǎn)單,只是一個(gè)簡(jiǎn)單的登錄界面
form action ="%=request.getContextPath()%/UserWXPServlet" method = "post"
用戶名?input type = "text" name = "username"/br/
密碼?input type = "text" name = "password"/br/
input type = "submit" value ="提交"/
/form
成功后跳轉(zhuǎn)頁(yè)面
歡迎:${sessionScope.user.username}登陸!br/
我這沒有寫失敗頁(yè)面,大家可以自己寫,失敗頁(yè)面也沒什么好說的了
entity和登錄的實(shí)現(xiàn)
user的javabean
private String username;
private String password;
public User() {
}
public User(String user, String password) {
super();
this.username = user;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
登錄user的service實(shí)現(xiàn)方法,這里就不寫dao和接口了,一切以簡(jiǎn)單為
public boolean dologin(User user){
Properties pro = new Properties();
InputStream is = UserWXPServlet.class.getClassLoader().getResourceAsStream("user_wxp.properties");
String password = null;
System.out.println(is+"---------"+pro);
if(user==null){
return false;
}
try {
pro.load(is);
password = pro.getProperty(user.getUsername());
if(user.getPassword()!=nulluser.getPassword().equals(password)){
System.out.println("登陸成功");
return true;
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
登錄成功返回true,失敗則返回false。
用JAVA編寫一個(gè)用戶或注冊(cè)登錄界面。請(qǐng)哪位高手能夠?qū)懴戮唧w的代碼,謝謝
效果圖
代碼
!DOCTYPE?html
html
head
meta?charset="UTF-8"
title先鋒圖書館管理系統(tǒng)-登錄/title
style
*{
margin:?0;
padding:?0;
list-style:?none;
}
#top{
width:?1000px;
height:?95px;
margin:?0?auto;
margin-top:?25px;
}
#top_top{
width:?1000px;
height:?65px;
background:?deepskyblue;
}
#top_top_left{
width:?300px;
height:?65px;
float:?left;
}
#top_top_leftlabel{
width:?200px;
height:?65px;
color:?white;
float:?right;
}
#top_top_left#a2{
padding-left:?10px;
padding-top:?20px;
font-size:?16px;
}
#top_bottom{
width:?1000px;
height:?30px;
}
#top_bottom_left{
width:?340px;
height:?30px;
line-height:?30px;
font-size:?12px;
background:?skyblue;
color:?white;
text-indent:?2em;
float:?left;
}
#top_bottom_right{
width:?660px;
height:?30px;
line-height:?30px;
font-size:?12px;
color:?blueviolet;
text-align:?center;
float:?right;
background:?lightskyblue;
}
#content{
width:?1000px;
height:?600px;
margin:?0?auto;
background:#587FBA;
}
#content#text{
width:?1000px;
height:?50px;
line-height:?50px;
padding-top:?100px;
font-size:?36px;
font-family:"楷體";
font-weight:?bold;
text-align:?center;
}
#content#login{
width:?480px;
height:?210px;
margin-top:?20px;
margin-left:?260px;
background:?#85A0CB;
}
#content#loginimg{
float:?left;
}
#content#login#select{
width:?305px;
height:?210px;
float:?right;
}
#content#login#selectdiv{
width:?230;
height:?30px;
margin-left:?30px;
}
#content#login#select#d1{
margin-top:30px;
margin-bottom:?3px;
}
#content#login#selectp{
font-size:?14px;
margin-left:?95px;
}
#bottom{
width:?1000px;
height:?35px;
line-height:?35px;
margin:?0?auto;
background:?deepskyblue;
text-align:?center;
color:?white;
}
/style
/head
body
div?id="top"
div?id="top_top"
div?id="top_top_left"
img?src="img/test/a13.png"?width="78px"?height="65px"label?id="a2"先鋒圖書館系統(tǒng)管理平臺(tái)/label
/div
/div
div?id="top_bottom"
div?id="top_bottom_left"當(dāng)前位置?:?首頁(yè)??系統(tǒng)管理??登錄/div
div?id="top_bottom_right"當(dāng)前時(shí)間?:?label?id="lable"/label/div
/div
/div
div?id="content"
div?id="text"歡迎登錄先鋒圖書館管理系統(tǒng)/div
div?id="login"
img?src="img/test/a14.png"??width="175px"?height="210px"/
form?id="select"
div?id="d1"用戶名:?nbsp;nbsp;input?type="text"?//div
div密????nbsp;?碼:?nbsp;nbsp;input?type="password"?//div
p
input?type="radio"?name="user"?value="read"/讀者nbsp;nbsp;nbsp;nbsp;
input?type="radio"?name="user"?value="admin"/管理員
/pbr/
p
input?type="button"?value="確定"?style="width:?50px;"?onclick="put()"/nbsp;nbsp;nbsp;nbsp;
input?type="reset"?value="重置"?style="width:?50px;"/
/p
/form
/div
/div
div?id="bottom"欣欣科技有限公司版權(quán)所有/div
/body
script?type="text/javascript"?src="JQuery/jquery.js"/script
script?type="text/javascript"?src="js/GetCurrentTime.js"/script
script
//驗(yàn)證用戶名和密碼
function?put(){
var?d?=?$("#selectdivinput");//獲取用戶名和密碼
var?name?=?d[0].value;
var?pass?=?d[1].value;
var?user?=?null;
var?r?=?document.getElementsByName("user");//獲取用戶類型
for(i=0;ir.length;i++){
if(r[i].checked){
user=r[i].value;
}
}
//console.log(name?+?","?+pass?+?","?+user);//輸出測(cè)試
if(user==null){
window.alert("請(qǐng)選擇用戶類型!");
}else?if(user=="admin"??name!="admin"){
window.alter("用戶名錯(cuò)誤!");
}else?if(user=="admin"??name=="admin"??pass!="123456"){
window.alert("密碼錯(cuò)誤!");
}else?if(name=="admin"??pass=="123456"??user=="admin"){
window.location.href="work_02_welcome.html";//在js中在本頁(yè)面中打開新鏈接
}else{
window.alert("用戶名錯(cuò)誤");
}
}
/script
/html
java代碼寫注冊(cè),注冊(cè)完成后,怎么寫提示注冊(cè)成功
Java寫提示注冊(cè)成功的方法如下:
1、首先用戶注冊(cè)完成后,返回一個(gè)boolean值的變量;
2、利用Servlet類判斷這個(gè)變量,如果為true,跳轉(zhuǎn)到提示界面,提示用戶注冊(cè)成功,如果為false,跳轉(zhuǎn)到提示界面,提示用戶注冊(cè)失敗;
3、具體代碼如下所示:
public?class?DemoServlet?extends?HttpServlet?{
public?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)
throws?ServletException,?IOException?{
String?username?=?request.getParameter("username");
String?usepwd=?request.getParameter("usepwd");
boolean?flag?=?Dao.register(username,usepwd);//注冊(cè)方法
if(flag){
//提示注冊(cè)成功
request.getRequestDispatcher("/success.jsp").forward(request,?response);
}else{
//提示注冊(cè)失敗
request.getRequestDispatcher("/success.jsp").forward(request,?response);
}
}
public?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)
throws?ServletException,?IOException?{
doGet(request,?response);
}
}
4、至此,就完成了提示注冊(cè)成功的功能。
用java寫一個(gè)手機(jī)商城注冊(cè)界面代碼
這篇文章主要介紹了java通過JFrame做一個(gè)登錄系統(tǒng)的界面完整代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。
在java的JFrame內(nèi)通過創(chuàng)建匿名對(duì)象的方式做登錄界面
package com.sxt;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginFrame extends JFrame{
JTextField txtname=new JTextField();
JPasswordField txtpass=new JPasswordField();
JButton bl=new JButton("登錄");
JButton bg=new JButton("關(guān)閉");
//構(gòu)造無參構(gòu)造器把主要的方法放在構(gòu)造器里,然后在main方法里面調(diào)
public LoginFrame(){
setBounds(25,25,250,250);
Container c = getContentPane();
c.setLayout(new GridLayout(4,2,10,10));
c.add(new JLabel("用戶名"));
c.add(txtname);
c.add(new JLabel("密碼"));
c.add(txtpass);
c.add(bl);
c.add(bg);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
//注意:此處是匿名內(nèi)部類
bg.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
}
);
//注意:此處是匿名內(nèi)部類
bl.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
分享標(biāo)題:java中用戶注冊(cè)的代碼 java用戶注冊(cè)登錄界面代碼
瀏覽路徑:http://ef60e0e.cn/article/ddeojjd.html