學生信息管理系統----登錄後的主界面設計

登錄成功後頁面的跳轉

  這一部分基本是在處理JSP頁面,但是也有部分需要修改的後端代碼。
  便於後續對JSP操作,本部分操作會新增兩個JSP頁面,即我們登錄通過過濾器之後要訪問的頁面,system.jsp和welcom.jsp,我們從素材文件中找到這兩個文件,複製到項目的view文件夾下。

  打開system.jsp。對頁面做一些改動,只保留我們需要完成的部分。

// 改動後
var _menus = {"menus":[
				{"menuid":"2","icon":"","menuname":"學生信息管理",
					"menus":[
							{"menuid":"21","menuname":"學生列表","icon":"icon-user-student","url":"StudentServlet?method=toStudentListView"},
						]
				},
				{"menuid":"4","icon":"","menuname":"班級信息管理",
					"menus":[
							{"menuid":"41","menuname":"年級列表","icon":"icon-world","url":"GradeServlet?method=toGradeListView"},
							{"menuid":"42","menuname":"班級列表","icon":"icon-house","url":"ClazzServlet?method=toClazzListView"},
						]
				},
				{"menuid":"3","icon":"","menuname":"教師信息管理",
					"menus":[
							{"menuid":"31","menuname":"教師列表","icon":"icon-user-teacher","url":"TeacherServlet?method=toTeacherListView"},
						]
				},
				{"menuid":"5","icon":"","menuname":"系統管理",
					"menus":[
					        {"menuid":"51","menuname":"系統設置","icon":"icon-set","url":"SystemServlet?method=toAdminPersonalView"},
					        {"menuid":"51","menuname":"用戶管理","icon":"icon-user-student","url":"SystemServlet?method=toAdminPersonalView"},
					        {"menuid":"51","menuname":"修改密碼","icon":"icon-set","url":"SystemServlet?method=toAdminPersonalView"},
						]
				}
		]};

  在這個頁面中還存在一處代碼,使用了<jsp:include page="welcome.jsp" />,在system.jsp頁面被請求時,將welcom.jsp頁面的輸出包含進來。

<div id="mainPanle" region="center" style="background: #eee; overflow-y:hidden">
    <div id="tabs" class="easyui-tabs"  fit="true" border="false" >
		<jsp:include page="welcome.jsp" />
	</div>
</div>

  我們先回到login.jsp,對部分JS代碼進行修改,即對"loginSuccess" == msg後面的代碼進行修改。
  當後臺傳給前端的的返回信息爲“loginSuccess”,那麼頁面就跳轉到SystemServlet?method=toAdminView這個鏈接。

$.ajax({
	type: "post",
	url: "LoginServlet?method=Login",
	data: data, 
	dataType: "text", //返回數據類型
	success: function(msg){
		if("vcodeError" == msg){
			....
		} else if("loginError" == msg){
			....
		} else if("loginSuccess" == msg){
			window.location.href = "SystemServlet?method=toAdminView";
		} else{
			alert(msg);
		} 
	}			
});

  第二步,我們回到LoginServlet中,對代碼進行修改。先建立一個記錄登錄狀態的變量loginStatus,默認爲loginFailed,即登錄失敗。

// 驗證碼驗證通過,對比用戶名密碼是否爭取
String loginStatus = "loginFailed";
switch(type){
	case 1:{
		AdminDao adminDao = new AdminDao();
		Admin admin = adminDao.login(name, password);
		adminDao.closeCon();   // 關閉數據庫連接
		if(admin == null){     // 沒查詢到
			res.getWriter().write("loginError");	// 返回給前端一個登錄失敗的信息,便於JSP做處理
			return;
		}
		HttpSession session = req.getSession();		// 將登錄信息存入session
		session.setAttribute("user", admin);
		session.setAttribute("userType", type);
		loginStatus = "loginSuccess";				// 登錄狀態改爲登錄成功
		break;
	}
	default:
		break;
}
res.getWriter().write(loginStatus);					// 將登錄成功傳給JSP,便於JSP做處理,跳轉到新的鏈接地址(SystemServlet?method=toAdminView)

  第三步,在對前面的代碼進行修改之後,我們需要對我們即將要跳轉到的Servlet,即SystemServlet再做出進一步的修改。

public class SystemServlet extends HttpServlet {
	private static final long serialVersionUID = -7258264317769166483L;
	public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException{
		doPost(req,res);
	}
	public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException{
		HttpSession session = req.getSession();
		try {
			req.getRequestDispatcher("view/system.jsp").forward(req, res);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

  這段代碼中修改了一個地方,req.getRequestDispatcher("view/system.jsp").forward(req, res);
  對於這個方法,request.getRequestDispatcher(url).forward(request,response),是採用請求轉發方式,跳轉到指定的url,這個例子中是跳轉到view/system.jsp。在跳轉頁面的時候是帶着原來頁面的requestresponse跳轉的,request對象始終存在,不會重新創建。

  完成上述步驟後,經過測試能夠正常跳轉到我們想要的頁面中。
在這裏插入圖片描述

登錄註銷

  前面我們已經將登錄跳轉到主頁面完成了,接下來我們的需求是點擊安全退出實現用戶的退出。
  退出請求的處理我打算在LoginServlet中完成。先打開LoginServlet.java,創建一個退出登錄的方法,清除掉用戶的登錄信息,並且重定向到index.jsp。

private void logout(HttpServletRequest req,HttpServletResponse res) throws IOException{
	req.getSession().removeAttribute("user");
	req.getSession().removeAttribute("userType");
	res.sendRedirect("index.jsp");
}

  接下來我們在LoginServlet中的doPost()方法中最前面添加幾行代碼。

public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException{
	// 新添加的代碼
	String method = req.getParameter("method");
	if("LoginOut".equals(method)){
		logout(req, res);
		return;
	}
	String vcode = req.getParameter("vcode");
	....

  添加代碼的目的是,從system.jsp接受來自用戶的註銷登錄的請求進行處理。如果我們接受到的參數是LoginOut,那麼我們就調用註銷的方法,清除用戶的數據,實現了註銷功能。註銷之後如果想要再通過在地址欄填入想要訪問的地址鏈接進行訪問,也是不可能的。
  完成功能後測試的情況如下:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章