JSP實(shí)現(xiàn)簡(jiǎn)單的登錄和注冊(cè)界面詳細(xì)全過(guò)程
1、login.jsp

- login.jsp中
username和password在LoginSelect.jsp驗(yàn)證是否一致 - 使用
session.setAttribute("login_msg","用戶名或密碼為空")設(shè)置login_msg的值 - 使用
session.getAttribute("login_msg")獲取對(duì)象的值,判斷輸入框是否為空,如果為空,則提示用戶名或密碼為空。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登錄界面</title>
</head>
<body>
<div align="center">
<h1>歡迎登錄</h1>
<form action="LoginSelect.jsp" method="post" id="form">
<p>用戶名: <input id="username" name="username" type="text">  </p>
<p>密碼: <input id="password" name="password" type="password"></p>
<input type="submit" class="button" value="登錄" onclick="">
<button><a href="register.jsp" rel="external nofollow" >注冊(cè)</a></button>
</form>
<div id="errorMsg" value="null"><%=session.getAttribute("login_msg")%></div>
</div>
<script>
if(document.getElementById("errorMsg").innerText==="null"||document.getElementById("errorMsg").innerText===""){
document.getElementById("errorMsg").setAttribute('style',"display:none")
} else {
document.getElementById("errorMsg").setAttribute('style',"display:block")
}
</script>
</body>
</html>
2、 loginSelect.jsp
- 利用Map集合存儲(chǔ)賬戶和密碼信息,模擬數(shù)據(jù)庫(kù)
map.put("20201234","123456")設(shè)置初始數(shù)據(jù)map.put(username,session.getAttribute(username).toString())這里是將注冊(cè)的賬戶和密碼添加到數(shù)據(jù)庫(kù)中,username為鍵,session.getAttribute(username).toString()為值,兩者都為字符串類型
<%@ page import="java.util.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>判斷登錄界面</title>
</head>
<body>
<%!
Map<String,String> map = new HashMap<String,String>();
public boolean compare(String username,String password){
String pwd = map.get(username);
if(pwd!=null&&password.equals(pwd)){
return true;
}
else{
return false;
}
}
%>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
//設(shè)置初始值
map.put("20201234","123456");
//注冊(cè)后的值存入map集合
if (session.getAttribute(username)!=null){
map.put(username,session.getAttribute(username).toString());
}
System.out.println(map);
//判斷輸入內(nèi)容是否正確,給出提示信息
if (username==null||username =="" || password==null || password==""){
session.setAttribute("login_msg","用戶名或密碼為空");
response.sendRedirect("login.jsp");
return;
}
boolean compare = compare(username, password);
if (compare){
session.setAttribute("username",username);
session.setAttribute("password",password);
response.sendRedirect("index.jsp");
}
else {
session.setAttribute("login_msg","用戶名或密碼錯(cuò)誤或用戶名不存在");
response.sendRedirect("login.jsp");
}
%>
</body>
</html>
3、register.jsp

- register.jsp中
username和password在RegisterSelect.jsp驗(yàn)證是否一致 - 使用
session.setAttribute("register_msg","用戶名或密碼為空")設(shè)置register_msg的值 - 使用
session.getAttribute("register_msg")獲取對(duì)象的值,判斷輸入框是否為空,如果為空,則提示用戶名或密碼為空。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注冊(cè)界面</title>
</head>
<div align="center">
<h1>歡迎注冊(cè)</h1>
<form action="RegisterSelect.jsp" method="post">
<table>
<tr>
<td>用戶名</td>
<td>
<input name="username" type="text" id="username">
<br>
</td>
</tr>
<tr>
<td>密碼</td>
<td>
<input name="password" type="password" id="password">
<br>
</td>
</tr>
</table>
<input value="注 冊(cè)" type="submit" id="reg_btn"><br>
<span>已有帳號(hào)?</span> <a href="login.jsp" rel="external nofollow" rel="external nofollow" >登錄</a>
</form>
<span id="register_msg" class="err_msg" ><%=session.getAttribute("register_msg")%></span>
</div>
</body>
</div>
<script>
if(document.getElementById("register_msg").innerText==="null"||document.getElementById("register_msg").innerText===""){
document.getElementById("register_msg").setAttribute('style',"display:none")
} else {
document.getElementById("register_msg").setAttribute('style',"display:block")
}
</script>
</html>
4、 RegisterSelect.jsp
if else語(yǔ)句,if判斷賬戶或密碼為空則提示"用戶或密碼為空",else使用session.setAttribute(username,password)創(chuàng)建對(duì)象存儲(chǔ)新的賬戶和密碼信息。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
session.setAttribute("register_msg","null");
if (username==null||username =="" || password==null || password==""){
session.setAttribute("register_msg","用戶名或密碼為空");
response.sendRedirect("register.jsp");
return;
}
else {
session.setAttribute(username,password);
response.sendRedirect("login.jsp");
}
%>
<html>
<head>
<title>Title</title>
</head>
<body>
</body>
</html>
5、 index.jsp

session.getAttribute("username")動(dòng)態(tài)獲取賬戶名稱
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登錄成功</title>
</head>
<body>
<div align="center">
<h1>JSP管理系統(tǒng)</h1>
<h1><%=session.getAttribute("username")%> 歡迎您!</h1>
<a href="login.jsp" rel="external nofollow" rel="external nofollow" >退出登錄</a>
</div>
</body>
</html>總結(jié)
到此這篇關(guān)于JSP實(shí)現(xiàn)簡(jiǎn)單的登錄和注冊(cè)界面的文章就介紹到這了,更多相關(guān)JSP實(shí)現(xiàn)登錄和注冊(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
jsp中session過(guò)期設(shè)置及web.xml配置學(xué)習(xí)
session的過(guò)期時(shí)間需要配置在tomcat中的web.xml中,時(shí)間以分鐘計(jì)算,另最大時(shí)間好像是24小時(shí),感興趣的朋友可以了解下,希望本文對(duì)你學(xué)習(xí)session有所幫助2013-01-01
window.top[_CACHE]實(shí)現(xiàn)多個(gè)jsp頁(yè)面共享一個(gè)js對(duì)象
兩個(gè)js頁(yè)面要共享一個(gè)就js對(duì)象,想了半天用window.top['_CACHE']來(lái)存放這個(gè)變量,即可實(shí)現(xiàn),不同Jsp頁(yè)面直接的對(duì)象共享2014-08-08
JAVA POST與GET數(shù)據(jù)傳遞時(shí)中文亂碼問(wèn)題解決方法
最近亂忙活弄了一個(gè)企業(yè)家宣傳網(wǎng)站遇到了中文字符集亂碼問(wèn)題,在此分享一下即簡(jiǎn)單又實(shí)用的解決方法,感興趣的朋友可以參考下哈2013-06-06
jsp只在首次加載時(shí)調(diào)用action實(shí)現(xiàn)代碼
如何只在首次加載時(shí)調(diào)用action,實(shí)現(xiàn)很簡(jiǎn)單只需判斷l(xiāng)ist==null即可,感興趣的朋友可以參考下2013-10-10
Spring AOP切面解決數(shù)據(jù)庫(kù)讀寫(xiě)分離實(shí)例詳解
這篇文章主要介紹了Spring AOP切面解決數(shù)據(jù)庫(kù)讀寫(xiě)分離實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
jsp學(xué)習(xí)之scriptlet的使用方法詳解
這篇文章主要介紹了jsp學(xué)習(xí)之scriptlet的使用方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07

