系統功能實作-登入功能
一、功能簡介。任務釋出系統主要分為兩大部分。一是背景管理系統,主要是管理者管理任務系統,比如任務的釋出,使用者的管理,紀念品的管理等等。背景系統使用者有兩類,分别為系統管理者和部門負責人。系統管理者可以使用全部功能,部門負責人隻能使用任務管理功能。二是前台任務系統,主要是普通使用者使用,實作任務領取,任務送出,兌換紀念品等功能,由于普通使用者主要使用手機登入系統,是以采用H5開發,适配手機界面。
二、登入功能的實作。今天主要介紹登入功能的具體實作。管理者和普通使用者登入頁面是分開的,逐一介紹。首先介紹管理者登入功能實作,主要有登入,登出,修改密碼等。管理者頁面結構如圖:
登入頁面login.jsp:
視訊加載中...
核心代碼解析:
<%String loginmsg = request.getParameter("loginmsg");//接收回調參數,确定登入狀态
request.getSession(true);
session.putValue("IsLogin","false");//初始化登入狀态為未登入%>
<script language="JavaScript">//使用者名,密碼輸入檢測JS
function check_input(){
if( form.username.value.length<1 || form.username.value.length>20 ){
alert("請輸入使用者名.")
return false;}
if( form.password.value.length<1 || form.password.value.length>20 ){
alert("請輸入密碼.")
return false;}}
</script>
<form action=/TaskMS/ChkLogin.jsp method=post name=form onSubmit="return check_input()">//轉入ChkLogin.jsp頁面,驗證登入
根據回調參數loginmsg,确定登入狀态。
<%if(loginmsg != null && loginmsg.equals("userwrong"))
out.print("使用者名或密碼不正确.");
else if(loginmsg != null && loginmsg.equals("loginfirst"))
out.print("請先登入.");
else if(loginmsg != null && loginmsg.equals("logout"))
out.print("已經成功退出系統.");%>
ChkLogin.jsp頁面核心代碼解析:
String username = request.getParameter("username");
String password = request.getParameter("password");
String loginmsg;
String Unit;
boolean IsAdmin;
if(!User.ChkLogin(username,password))//調用User類的ChkLogin,确定使用者名密碼是否比對{
loginmsg="userwrong";
response.sendRedirect("login.jsp?loginmsg="+loginmsg);//如果不比對則轉向登入頁面
return;}
Unit=User.FindByName(username).getUnit();//擷取使用者機關
IsAdmin=User.FindByName(username).getIsAdmin();//擷取是否管理者
//将使用者相關資訊放入session
request.getSession(true);
session.putValue("IsLogin","true");
session.putValue("UserName",username);
session.putValue("User",Unit);
session.putValue("Unit",Unit);
if(IsAdmin)
session.putValue("IsAdmin","true");
else
session.putValue("IsAdmin","false");
if(Unit.equals("superAdmin"))//如果是超級管理者則轉向管理者首頁
{response.sendRedirect("Home/index.jsp");
session.putValue("Unit","superAdmin");
return;}
登出頁面Logout.jsp核心代碼解析:
<% //清空session并轉向登入頁面
Enumeration names = session.getAttributeNames();
while (names.hasMoreElements()){
String element =(String)names.nextElement();
session.removeAttribute(element);}
String loginmsg="logout";
response.sendRedirect("login.jsp?loginmsg="+loginmsg);%>
修改密碼頁面及核心代碼解析:
ModifyPasswordStep1.jsp:
确認兩次密碼輸入是否一緻
<script language="javascript">
function check_input(theForm){
if (theForm.oldpassword.value == ""){
alert("請輸入舊密碼.");
theForm.oldpassword.focus();
return (false);}
if (theForm.newpassword.value != theForm.confirmpassword.value){
alert("兩次輸入密碼不一緻.");
theForm.confirmpassword.focus();
return (false);}}
</script>
ModifyPasswordStep2.jsp
try{
String username = (String)session.getValue("UserName");//擷取使用者名
String oldpassword = new String(request.getParameter("oldpassword").getBytes("ISO8859_1"),"GBK");//擷取原密碼
String newpassword = new String(request.getParameter("newpassword").getBytes("ISO8859_1"),"GBK");//擷取新密碼
if(!(User.ChkLogin(username,oldpassword)))//确認使用者名和密碼是否比對{%>
<script>
alert("舊密碼輸入錯誤,請重新輸入.");
window.history.back();
</script><% }
else{
IsSucceed = User.ModifyPassword(username,newpassword);//修改密碼}}
catch(Exception e){
out.println("error:"+e.getMessage());}
Check.jsp頁面用來驗證是否是登入狀态,後續頁面都要引用,确定登入後才能通路。核心代碼解析:
if((String)session.getValue("IsLogin")!="true")//确定是否登入,如沒有則轉向登入頁面{
String loginmsg="loginfirst";
response.sendRedirect("/TaskMS/login.jsp?loginmsg="+loginmsg);}
使用者頁面登入與管理者登入實作方式基本一緻,不再詳細介紹。
視訊加載中...