登录验证和EasyUI的初识
今天开始试着做项目:学生信息管理系统.网上也有好多源码,于是,参照源码我就开始学习如何做项目
今天就做了登录验证,看了点EasyUI
* 登录验证页面
1 package org.ylfeiu.web; 2 3 import java.io.IOException; 4 import java.sql.Connection; 5 import java.sql.SQLException; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 import javax.servlet.http.HttpSession; 12 13 import org.ylfeiu.dao.UserDao; 14 import org.ylfeiu.entity.User; 15 import org.ylfeiu.utils.DbUtil; 16 import org.ylfeiu.utils.StringUtil; 17 18 public class LoginServlet extends HttpServlet { 19 20 DbUtil dbUtil = new DbUtil(); 21 22 UserDao userDao = new UserDao(); 23 24 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 25 throws ServletException, IOException { 26 this.doPost(req, resp); 27 } 28 29 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 30 throws ServletException, IOException { 31 String name = req.getParameter("username"); 32 String passwd = req.getParameter("passwd"); 33 // 服务器端验证 34 if (StringUtil.isEmpty(name) || StringUtil.isEmpty(passwd)) { 35 req.setAttribute("err", "用户名或密码不能为空!");//表单数据不合法,给请求设置错误参数 36 req.getRequestDispatcher("login.jsp").forward(req, resp);//并且让其转发到登录页面,服务器端跳转 37 return; 38 } 39 User user = new User(name, passwd);//把当前登录用户实例化 40 Connection conn = null; 41 42 try { 43 conn = dbUtil.getCon(); 44 User currentUser = userDao.login(conn, user);//通过构造将当前用户信息传到userDao中的login方法,该方法返回一个user对象 45 //若当前用户与数据库取出的数据一致,则返回一个对象,否则返回的是null 46 if (currentUser == null) { 47 req.setAttribute("err", "用户名或密码错误!"); 48 req.getRequestDispatcher("login.jsp").forward(req, resp); 49 return; 50 } else { 51 52 HttpSession session = req.getSession(); 53 54 session.setAttribute("currentUser", currentUser);//给session设置参数,用于验证用户是否登录 55 56 resp.sendRedirect("main.jsp");//客户端跳转,重定向到登录成功页面 57 } 58 59 } catch (Exception e) { 60 61 e.printStackTrace(); 62 } finally { 63 try { 64 conn.close(); 65 } catch (SQLException e) { 66 67 e.printStackTrace(); 68 } 69 } 70 71 } 72 73 }
* 后台管理主页面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>学生信息管理系统主界面</title>
<%
// 权限验证
if (session.getAttribute("currentUser") == null) {
response.sendRedirect("index.jsp");
return;
}
%>
<link rel="stylesheet" type="text/css"
href="jquery-easyui-1.3.3/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="jquery-easyui-1.3.3/themes/icon.css">
<script type="text/javascript" src="jquery-easyui-1.3.3/jquery.min.js"></script>
<script type="text/javascript"
src="jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
<script type="text/javascript"
src="jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">
$(function() {
// 数据,树型菜单,一个对象(json)数组,子菜单又是对象数组
var treeData = [ {
text : "根",
children : [ {
text : "班级信息管理",
attributes : {
url : "gradeInfoManage.jsp" //ajax异步通信
}
}, {
text : "学生信息管理",
attributes : {
url : "studentInfoManage.jsp"
}
} ]
} ];
// 实例化树菜单
$("#tree").tree({
data : treeData,
lines : true,
onClick : function(node) {//node是点击菜单后传过来的节点(不包含根节点)
if (node.attributes) {
openTab(node.text, node.attributes.url);
}
}
});
// 新增Tab
function openTab(text, url) {
if ($("#tabs").tabs('exists', text)) {
$("#tabs").tabs('select', text);
} else {
var content = "<iframe frameborder='0' scrolling='auto' style='width:100%;height:100%' src="
+ url + "></iframe>";
$("#tabs").tabs('add', {
title : text,
closable : true,
content : content
});
}
}
});
</script>
</head>
<body class="easyui-layout">
<div region="north" style="height: 80px; background-color: #E0EDFF">
<div align="left" style="width: 80%; float: left">
<img src="images/main.jpg">
</div>
<div style="padding-top: 50px; padding-right: 20px;">
当前用户: <font color="red">${currentUser.userName }</font><!--通过了session验证后的用户 --->
</div>
</div>
<div region="center">
<div class="easyui-tabs" fit="true" border="false" id="tabs">
<div title="首页">
<div align="center" style="padding-top: 100px;">
<font color="red" size="10">欢迎使用</font>
</div>
</div>
</div>
</div>
<div region="west" style="width: 150px;" title="导航菜单" split="true">
<ul id="tree"></ul>
</div>
<div region="south" style="height: 25px;" align="center">
版权所有<a href="http://www.baidu.com">www.baidu.com</a>
</div>
</body>
</html>
ps:个人觉得Javascript比较灵活,又不失强大,需要好好学习它

浙公网安备 33010602011771号