MVC model1
Bean包(Javabean类,与数据表中字段相对应,存取用户信息) 1. >adminTable.java 2. >studentTable.java
Coon包(Java类,与数据库建立连接)这个类很重要,可以服用,此处贴出代码
package com.app.snowalker.conn;
import java.sql.Connection;
import java.sql.DriverManager;
public class conn {
public Connection getCon() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/studentmanagementsystem?useUnicode=true&characterEncoding=utf-8";
String user = "snowalker";
String password = "admin";
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println("connnection success");
System.out.println(conn.getMetaData().getURL());
return conn;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
Service包(Java类,验证用户合法性)
adminservice.java
studentservice.java
searchstudentinfo.jsp(管理员查询页面,增加,删除,修改页面同理,详情请参看WebContent文件夹)
404.jsp(为404错误制定个性404页面)
<!-- 管理员验证 --> <jsp:useBean id="admin" class="com.app.snowalker.bean.adminTable"> </jsp:useBean> <jsp:useBean id="adminservice" class="com.app.snowalker.service.adminservice"> </jsp:useBean> <!-- 封装表单数据 admin --> <jsp:setProperty property="*" name="admin" />
<%--admin验证 --%>
<%
//admin.setAdminname("admintest");
out.print(admin.getAdminname());
out.print("\n" + admin.getPassword());
if (adminservice.validateAdmin(admin)) {
//将当前jsp页面中的admin实例存储在名为admin的session字段中
session.setAttribute("admin", admin);
%>
<jsp:forward page="adminpage.jsp"></jsp:forward>
<%
} else {
%>
<jsp:forward page="adminIndex.jsp"></jsp:forward>
<%
}
%>
islogin.jsp(记录session值防止跨页面访问)
<% adminTable admin = (adminTable) session.getAttribute("admin"); if (admin == null) { %> <jsp:forward page="index.jsp"></jsp:forward> <% } %>